diff --git a/commands/board/board_adddraft.go b/commands/board/board_adddraft.go index b983b1b2567affd9b0f813f2cbb0449def4bd2d6..378d42c37bf60016bff92568f4a9bfbdbce8447e 100644 --- a/commands/board/board_adddraft.go +++ b/commands/board/board_adddraft.go @@ -7,7 +7,7 @@ import ( "github.com/spf13/cobra" "github.com/git-bug/git-bug/cache" - buginput "github.com/git-bug/git-bug/commands/bug/input" + boardinput "github.com/git-bug/git-bug/commands/board/input" "github.com/git-bug/git-bug/commands/execenv" _select "github.com/git-bug/git-bug/commands/select" "github.com/git-bug/git-bug/entity" @@ -15,8 +15,7 @@ import ( type boardAddDraftOptions struct { title string - messageFile string - message string + titleFile string column string nonInteractive bool } @@ -40,10 +39,8 @@ func newBoardAddDraftCommand() *cobra.Command { flags.StringVarP(&options.title, "title", "t", "", "Provide the title to describe the draft item") - flags.StringVarP(&options.message, "message", "m", "", - "Provide the message of the draft item") - flags.StringVarP(&options.messageFile, "file", "F", "", - "Take the message from the given file. Use - to read the message from the standard input") + flags.StringVarP(&options.titleFile, "file", "F", "", + "Take the title from the given file. Use - to read the message from the standard input") flags.StringVarP(&options.column, "column", "c", "1", "The column to add to. Either a column Id or prefix, or the column number starting from 1.") _ = cmd.RegisterFlagCompletionFunc("column", ColumnCompletion(env)) @@ -58,19 +55,16 @@ func runBoardAddDraft(env *execenv.Env, opts boardAddDraftOptions, args []string return err } - // TODO: editor with single line, no message - - if opts.messageFile != "" && opts.message == "" { - // Note: reuse the bug inputs - opts.title, opts.message, err = buginput.BugCreateFileInput(opts.messageFile) + if opts.titleFile != "" && opts.title == "" { + opts.title, err = boardinput.BoardTitleFileInput(opts.titleFile) if err != nil { return err } } - if !opts.nonInteractive && opts.messageFile == "" && (opts.message == "" || opts.title == "") { - opts.title, opts.message, err = buginput.BugCreateEditorInput(env.Backend, opts.title, opts.message) - if err == buginput.ErrEmptyTitle { + if !opts.nonInteractive && opts.titleFile == "" && opts.title == "" { + opts.title, err = boardinput.BoardTitleEditorInput(env.Backend, opts.title) + if err == boardinput.ErrEmptyTitle { env.Out.Println("Empty title, aborting.") return nil } diff --git a/commands/board/input/input.go b/commands/board/input/input.go new file mode 100644 index 0000000000000000000000000000000000000000..efc02321b6604276c3898ec161b46247ad94b606 --- /dev/null +++ b/commands/board/input/input.go @@ -0,0 +1,69 @@ +package buginput + +import ( + "fmt" + "strings" + + "github.com/pkg/errors" + + "github.com/git-bug/git-bug/commands/input" + "github.com/git-bug/git-bug/repository" +) + +const messageFilename = "BOARD_EDITMSG" + +// ErrEmptyTitle is returned when the required title has not been entered +var ErrEmptyTitle = errors.New("empty title") + +const boardTitleTemplate = `%s + +# Please enter the title of the draft board item. Only one line will used. +# Lines starting with '#' will be ignored, and an empty title aborts the operation. +` + +// BoardTitleEditorInput will open the default editor in the terminal with a +// template for the user to fill. The file is then processed to extract the title. +func BoardTitleEditorInput(repo repository.RepoCommonStorage, preTitle string) (string, error) { + template := fmt.Sprintf(boardTitleTemplate, preTitle) + + raw, err := input.LaunchEditorWithTemplate(repo, messageFilename, template) + if err != nil { + return "", err + } + + return processTitle(raw) +} + +// BoardTitleFileInput read from either from a file or from the standard input +// and extract a title. +func BoardTitleFileInput(fileName string) (string, error) { + raw, err := input.FromFile(fileName) + if err != nil { + return "", err + } + + return processTitle(raw) +} + +func processTitle(raw string) (string, error) { + lines := strings.Split(raw, "\n") + + var title string + for _, line := range lines { + if strings.HasPrefix(line, "#") { + continue + } + trimmed := strings.TrimSpace(line) + if trimmed == "" { + continue + } + title = trimmed + break + } + + if title == "" { + return "", ErrEmptyTitle + } + + return title, nil +} diff --git a/commands/bug/input/input.go b/commands/bug/input/input.go index 93b7391e91ca46798f5f862c2bf8ee629f985c42..3159ad1e75844616d449dcd912bc0517a58c91bb 100644 --- a/commands/bug/input/input.go +++ b/commands/bug/input/input.go @@ -11,7 +11,7 @@ import ( "github.com/git-bug/git-bug/repository" ) -const messageFilename = "BUG_MESSAGE_EDITMSG" +const messageFilename = "BUG_EDITMSG" // ErrEmptyMessage is returned when the required message has not been entered var ErrEmptyMessage = errors.New("empty message") @@ -197,7 +197,7 @@ const queryTemplate = `%s # - sort:edit, sort:edit-desc, sort:edit-asc # # Notes -# +# # - queries are case insensitive. # - you can combine as many qualifiers as you want. # - you can use double quotes for multi-word search terms (ex: author:"René Descartes") diff --git a/doc/man/git-bug-board-add-draft.1 b/doc/man/git-bug-board-add-draft.1 index ae3214c1103424b14aef64b29395709aaae1e017..4dbbbddbf11d67be226f03a7a69ffcbe57d48bdf 100644 --- a/doc/man/git-bug-board-add-draft.1 +++ b/doc/man/git-bug-board-add-draft.1 @@ -17,13 +17,9 @@ Add a draft item to a board \fB-t\fP, \fB--title\fP="" Provide the title to describe the draft item -.PP -\fB-m\fP, \fB--message\fP="" - Provide the message of the draft item - .PP \fB-F\fP, \fB--file\fP="" - Take the message from the given file. Use - to read the message from the standard input + Take the title from the given file. Use - to read the message from the standard input .PP \fB-c\fP, \fB--column\fP="1" diff --git a/doc/md/git-bug_board_add-draft.md b/doc/md/git-bug_board_add-draft.md index 865fc1c9d2b51bef6e622181736332a8bf1906c3..104dc4868a1379b654ccc2946c6641583680c19e 100644 --- a/doc/md/git-bug_board_add-draft.md +++ b/doc/md/git-bug_board_add-draft.md @@ -10,8 +10,7 @@ git-bug board add-draft [BOARD_ID] [flags] ``` -t, --title string Provide the title to describe the draft item - -m, --message string Provide the message of the draft item - -F, --file string Take the message from the given file. Use - to read the message from the standard input + -F, --file string Take the title from the given file. Use - to read the message from the standard input -c, --column string The column to add to. Either a column Id or prefix, or the column number starting from 1. (default "1") --non-interactive Do not ask for user input -h, --help help for add-draft