board_adddraft.go

  1package boardcmd
  2
  3import (
  4	"fmt"
  5	"strconv"
  6
  7	"github.com/spf13/cobra"
  8
  9	buginput "github.com/git-bug/git-bug/commands/bug/input"
 10	"github.com/git-bug/git-bug/commands/execenv"
 11	_select "github.com/git-bug/git-bug/commands/select"
 12	"github.com/git-bug/git-bug/entity"
 13)
 14
 15type boardAddDraftOptions struct {
 16	title          string
 17	messageFile    string
 18	message        string
 19	column         string
 20	nonInteractive bool
 21}
 22
 23func newBoardAddDraftCommand() *cobra.Command {
 24	env := execenv.NewEnv()
 25	options := boardAddDraftOptions{}
 26
 27	cmd := &cobra.Command{
 28		Use:     "add-draft [BOARD_ID]",
 29		Short:   "Add a draft item to a board",
 30		PreRunE: execenv.LoadBackend(env),
 31		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
 32			return runBoardAddDraft(env, options, args)
 33		}),
 34		ValidArgsFunction: BoardCompletion(env),
 35	}
 36
 37	flags := cmd.Flags()
 38	flags.SortFlags = false
 39
 40	flags.StringVarP(&options.title, "title", "t", "",
 41		"Provide the title to describe the draft item")
 42	flags.StringVarP(&options.message, "message", "m", "",
 43		"Provide the message of the draft item")
 44	flags.StringVarP(&options.messageFile, "file", "F", "",
 45		"Take the message from the given file. Use - to read the message from the standard input")
 46	flags.StringVarP(&options.column, "column", "c", "1",
 47		"The column to add to. Either a column Id or prefix, or the column number starting from 1.")
 48	_ = cmd.RegisterFlagCompletionFunc("column", ColumnCompletion(env))
 49	flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
 50
 51	return cmd
 52}
 53
 54func runBoardAddDraft(env *execenv.Env, opts boardAddDraftOptions, args []string) error {
 55	b, args, err := ResolveSelected(env.Backend, args)
 56
 57	var columnId entity.CombinedId
 58
 59	switch {
 60	case err == nil:
 61		// try to parse as column number
 62		index, err := strconv.Atoi(opts.column)
 63		if err == nil {
 64			if index-1 >= 0 && index-1 < len(b.Snapshot().Columns) {
 65				columnId = b.Snapshot().Columns[index-1].CombinedId
 66			} else {
 67				return fmt.Errorf("invalid column")
 68			}
 69		}
 70		fallthrough // could be an Id
 71	case _select.IsErrNoValidId(err):
 72		b, columnId, err = env.Backend.Boards().ResolveColumn(opts.column)
 73		if err != nil {
 74			return err
 75		}
 76	default:
 77		// actual error
 78		return err
 79	}
 80
 81	if opts.messageFile != "" && opts.message == "" {
 82		// Note: reuse the bug inputs
 83		opts.title, opts.message, err = buginput.BugCreateFileInput(opts.messageFile)
 84		if err != nil {
 85			return err
 86		}
 87	}
 88
 89	if !opts.nonInteractive && opts.messageFile == "" && (opts.message == "" || opts.title == "") {
 90		opts.title, opts.message, err = buginput.BugCreateEditorInput(env.Backend, opts.title, opts.message)
 91		if err == buginput.ErrEmptyTitle {
 92			env.Out.Println("Empty title, aborting.")
 93			return nil
 94		}
 95		if err != nil {
 96			return err
 97		}
 98	}
 99
100	id, _, err := b.AddItemDraft(columnId, opts.title, opts.message, nil)
101	if err != nil {
102		return err
103	}
104
105	env.Out.Printf("%s created\n", id.Human())
106
107	return b.Commit()
108}