From 1d65256e3704cbdb8825300e5e466581731e0598 Mon Sep 17 00:00:00 2001 From: Bruno Meneguele Date: Thu, 10 Feb 2022 14:08:30 -0300 Subject: [PATCH] cmd/edit: check if --force-linebreak coming from CLI or config 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 --- cmd/issue_edit.go | 22 +++++++++++++++++++--- cmd/mr_edit.go | 23 +++++++++++++++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/cmd/issue_edit.go b/cmd/issue_edit.go index abdea25a..10ad387d 100644 --- a/cmd/issue_edit.go +++ b/cmd/issue_edit.go @@ -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" @@ -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) diff --git a/cmd/mr_edit.go b/cmd/mr_edit.go index 1afdeb6a..914709a0 100644 --- a/cmd/mr_edit.go +++ b/cmd/mr_edit.go @@ -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" @@ -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)