From 5986f88565e5965a07d0a4a9091abf92b5aea7f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 29 Apr 2022 18:05:39 +0200 Subject: [PATCH] issue comment: support quotes and arguments in EDITOR value The `issue comment` command would perform a `filepath.Base()` on the value of EDITOR and use the result for launching the editor, which is undesireable for both functional and display purposes: - For functional purposes, always shellsplit the value of GH_EDITOR or EDITOR before we shell out to that process; - For display purposes, extract the basename only after shellsplitting. --- pkg/cmd/pr/shared/commentable.go | 5 +---- pkg/surveyext/editor.go | 25 ++++++++++++------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/pkg/cmd/pr/shared/commentable.go b/pkg/cmd/pr/shared/commentable.go index 80ac7d984..4d19e01b9 100644 --- a/pkg/cmd/pr/shared/commentable.go +++ b/pkg/cmd/pr/shared/commentable.go @@ -144,11 +144,8 @@ func CommentableInteractiveEditSurvey(cf func() (config.Config, error), io *iost if err != nil { return "", err } - if editorCommand == "" { - editorCommand = surveyext.DefaultEditorName() - } cs := io.ColorScheme() - fmt.Fprintf(io.Out, "- %s to draft your comment in %s... ", cs.Bold("Press Enter"), cs.Bold(editorCommand)) + fmt.Fprintf(io.Out, "- %s to draft your comment in %s... ", cs.Bold("Press Enter"), cs.Bold(surveyext.EditorName(editorCommand))) _ = waitForEnter(io.In) return surveyext.Edit(editorCommand, "*.md", "", io.In, io.Out, io.ErrOut) } diff --git a/pkg/surveyext/editor.go b/pkg/surveyext/editor.go index 26db04f26..7585c3fe4 100644 --- a/pkg/surveyext/editor.go +++ b/pkg/surveyext/editor.go @@ -11,6 +11,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2/terminal" + shellquote "github.com/kballard/go-shellquote" ) var ( @@ -39,14 +40,6 @@ type GhEditor struct { lookPath func(string) ([]string, []string, error) } -func (e *GhEditor) editorCommand() string { - if e.EditorCommand == "" { - return defaultEditor - } - - return e.EditorCommand -} - // EXTENDED to change prompt text var EditorQuestionTemplate = ` {{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}} @@ -79,7 +72,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int EditorTemplateData{ Editor: *e.Editor, BlankAllowed: e.BlankAllowed, - EditorCommand: filepath.Base(e.editorCommand()), + EditorCommand: EditorName(e.EditorCommand), Config: config, }, ) @@ -127,7 +120,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int // EXTENDED to support printing editor in prompt, BlankAllowed Editor: *e.Editor, BlankAllowed: e.BlankAllowed, - EditorCommand: filepath.Base(e.editorCommand()), + EditorCommand: EditorName(e.EditorCommand), ShowHelp: true, Config: config, }, @@ -144,7 +137,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int if lookPath == nil { lookPath = defaultLookPath } - text, err := edit(e.editorCommand(), e.FileName, initialValue, stdio.In, stdio.Out, stdio.Err, cursor, lookPath) + text, err := edit(e.EditorCommand, e.FileName, initialValue, stdio.In, stdio.Out, stdio.Err, cursor, lookPath) if err != nil { return "", err } @@ -166,6 +159,12 @@ func (e *GhEditor) Prompt(config *survey.PromptConfig) (interface{}, error) { return e.prompt(initialValue, config) } -func DefaultEditorName() string { - return filepath.Base(defaultEditor) +func EditorName(editorCommand string) string { + if editorCommand == "" { + editorCommand = defaultEditor + } + if args, err := shellquote.Split(editorCommand); err == nil { + editorCommand = args[0] + } + return filepath.Base(editorCommand) }