Skip to content

Commit

Permalink
cmd/edit: check if --force-linebreak coming from CLI or config
Browse files Browse the repository at this point in the history
When --force-linebreak is set through the config file the conditional
for opening the editor will always be satisfied no matter what's the
other flag being used together, since it only requires NFlag() == 1 and
linebreak being set.

For instance:

lab.toml:
    [mr_edit]
    force-linebreak = true

cli:
    $ lab mr edit --ready
    [EDITOR IS OPENED]

This patch fixes it by checking it linebreak is being actually set
through the CLI, so in case NFlag() == 1 we're sure the user wants to
open the editor considering the "force-linebreak".

Signed-off-by: Bruno Meneguele <bmeneg@redhat.com>
  • Loading branch information
bmeneg committed Feb 10, 2022
1 parent 8ab707a commit 1d65256
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
22 changes: 19 additions & 3 deletions cmd/issue_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/MakeNowJust/heredoc/v2"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
Expand Down Expand Up @@ -133,9 +134,24 @@ var issueEditCmd = &cobra.Command{
title := issue.Title
body := issue.Description

// We only consider editing an issue with -m or when no other flag is
// passed, but --linebreak.
if len(msgs) > 0 || cmd.Flags().NFlag() == 0 || (cmd.Flags().NFlag() == 1 && linebreak) {
// We only consider opening the editor to edit the title and body on
// -m, when --force-linebreak is used alone, or when no other flag is
// passed. However, it's common to set --force-linebreak through the
// config file, so we need to check if it's being set through the CLI
// or config file.
var openEditor bool
if len(msgs) > 0 || cmd.Flags().NFlag() == 0 {
openEditor = true
} else if linebreak && cmd.Flags().NFlag() == 1 {
cmd.Flags().Visit(func(f *pflag.Flag) {
if f.Name == "force-linebreak" {
openEditor = true
return
}
})
}

if openEditor {
title, body, err = editDescription(issue.Title, issue.Description, msgs, "")
if err != nil {
log.Fatal(err)
Expand Down
23 changes: 19 additions & 4 deletions cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
gitlab "github.com/xanzy/go-gitlab"
"github.com/zaquestion/lab/internal/action"
lab "github.com/zaquestion/lab/internal/gitlab"
Expand Down Expand Up @@ -194,10 +195,24 @@ var mrEditCmd = &cobra.Command{
log.Fatal("option -F cannot be combined with -m")
}

// We only consider editing title and body on -m, -F or when no other
// flag is passed, but --linebreak.
if len(msgs) > 0 || filename != "" ||
cmd.Flags().NFlag() == 0 || (cmd.Flags().NFlag() == 1 && linebreak) {
// We only consider opening the editor to edit the title and body on
// -m, -F, when --force-linebreak is used alone, or when no other flag
// is passed. However, it's common to set --force-linebreak through the
// config file, so we need to check if it's being set through the CLI
// or config file.
var openEditor bool
if len(msgs) > 0 || filename != "" || cmd.Flags().NFlag() == 0 {
openEditor = true
} else if linebreak && cmd.Flags().NFlag() == 1 {
cmd.Flags().Visit(func(f *pflag.Flag) {
if f.Name == "force-linebreak" {
openEditor = true
return
}
})
}

if openEditor {
title, body, err = editDescription(mr.Title, mr.Description, msgs, filename)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 1d65256

Please sign in to comment.