Skip to content

Commit 25678df

Browse files
authored
Merge pull request #791 from bmeneg/fix-edit
cmd/edit: check if --force-linebreak coming from CLI or config
2 parents bce4dad + 1d65256 commit 25678df

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

cmd/issue_edit.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/MakeNowJust/heredoc/v2"
99
"github.com/rsteube/carapace"
1010
"github.com/spf13/cobra"
11+
"github.com/spf13/pflag"
1112
gitlab "github.com/xanzy/go-gitlab"
1213
"github.com/zaquestion/lab/internal/action"
1314
lab "github.com/zaquestion/lab/internal/gitlab"
@@ -133,9 +134,24 @@ var issueEditCmd = &cobra.Command{
133134
title := issue.Title
134135
body := issue.Description
135136

136-
// We only consider editing an issue with -m or when no other flag is
137-
// passed, but --linebreak.
138-
if len(msgs) > 0 || cmd.Flags().NFlag() == 0 || (cmd.Flags().NFlag() == 1 && linebreak) {
137+
// We only consider opening the editor to edit the title and body on
138+
// -m, when --force-linebreak is used alone, or when no other flag is
139+
// passed. However, it's common to set --force-linebreak through the
140+
// config file, so we need to check if it's being set through the CLI
141+
// or config file.
142+
var openEditor bool
143+
if len(msgs) > 0 || cmd.Flags().NFlag() == 0 {
144+
openEditor = true
145+
} else if linebreak && cmd.Flags().NFlag() == 1 {
146+
cmd.Flags().Visit(func(f *pflag.Flag) {
147+
if f.Name == "force-linebreak" {
148+
openEditor = true
149+
return
150+
}
151+
})
152+
}
153+
154+
if openEditor {
139155
title, body, err = editDescription(issue.Title, issue.Description, msgs, "")
140156
if err != nil {
141157
log.Fatal(err)

cmd/mr_edit.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/rsteube/carapace"
1111
"github.com/spf13/cobra"
12+
"github.com/spf13/pflag"
1213
gitlab "github.com/xanzy/go-gitlab"
1314
"github.com/zaquestion/lab/internal/action"
1415
lab "github.com/zaquestion/lab/internal/gitlab"
@@ -194,10 +195,24 @@ var mrEditCmd = &cobra.Command{
194195
log.Fatal("option -F cannot be combined with -m")
195196
}
196197

197-
// We only consider editing title and body on -m, -F or when no other
198-
// flag is passed, but --linebreak.
199-
if len(msgs) > 0 || filename != "" ||
200-
cmd.Flags().NFlag() == 0 || (cmd.Flags().NFlag() == 1 && linebreak) {
198+
// We only consider opening the editor to edit the title and body on
199+
// -m, -F, when --force-linebreak is used alone, or when no other flag
200+
// is passed. However, it's common to set --force-linebreak through the
201+
// config file, so we need to check if it's being set through the CLI
202+
// or config file.
203+
var openEditor bool
204+
if len(msgs) > 0 || filename != "" || cmd.Flags().NFlag() == 0 {
205+
openEditor = true
206+
} else if linebreak && cmd.Flags().NFlag() == 1 {
207+
cmd.Flags().Visit(func(f *pflag.Flag) {
208+
if f.Name == "force-linebreak" {
209+
openEditor = true
210+
return
211+
}
212+
})
213+
}
214+
215+
if openEditor {
201216
title, body, err = editDescription(mr.Title, mr.Description, msgs, filename)
202217
if err != nil {
203218
log.Fatal(err)

0 commit comments

Comments
 (0)