board_adddraft.go

  1package boardcmd
  2
  3import (
  4	"fmt"
  5	"strconv"
  6
  7	"github.com/spf13/cobra"
  8
  9	"github.com/git-bug/git-bug/cache"
 10	boardinput "github.com/git-bug/git-bug/commands/board/input"
 11	"github.com/git-bug/git-bug/commands/execenv"
 12	_select "github.com/git-bug/git-bug/commands/select"
 13	"github.com/git-bug/git-bug/entity"
 14)
 15
 16type boardAddDraftOptions struct {
 17	title          string
 18	titleFile      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.titleFile, "file", "F", "",
 43		"Take the title from the given file. Use - to read the message from the standard input")
 44	flags.StringVarP(&options.column, "column", "c", "1",
 45		"The column to add to. Either a column Id or prefix, or the column number starting from 1.")
 46	_ = cmd.RegisterFlagCompletionFunc("column", ColumnCompletion(env))
 47	flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
 48
 49	return cmd
 50}
 51
 52func runBoardAddDraft(env *execenv.Env, opts boardAddDraftOptions, args []string) error {
 53	b, columnId, err := resolveColumnId(env, opts.column, args)
 54	if err != nil {
 55		return err
 56	}
 57
 58	if opts.titleFile != "" && opts.title == "" {
 59		opts.title, err = boardinput.BoardTitleFileInput(opts.titleFile)
 60		if err != nil {
 61			return err
 62		}
 63	}
 64
 65	if !opts.nonInteractive && opts.titleFile == "" && opts.title == "" {
 66		opts.title, err = boardinput.BoardTitleEditorInput(env.Backend, opts.title)
 67		if err == boardinput.ErrEmptyTitle {
 68			env.Out.Println("Empty title, aborting.")
 69			return nil
 70		}
 71		if err != nil {
 72			return err
 73		}
 74	}
 75
 76	id, _, err := b.AddItemDraft(columnId, opts.title)
 77	if err != nil {
 78		return err
 79	}
 80
 81	env.Out.Printf("%s created\n", id.Human())
 82
 83	return b.Commit()
 84}
 85
 86func resolveColumnId(env *execenv.Env, column string, args []string) (*cache.BoardCache, entity.CombinedId, error) {
 87	if column == "" {
 88		return nil, entity.UnsetCombinedId, fmt.Errorf("flag --column is required")
 89	}
 90
 91	b, args, err := ResolveSelected(env.Backend, args)
 92
 93	switch {
 94	case err == nil:
 95		// we have a pre-selected board, try to parse as column number
 96		index, err := strconv.Atoi(column)
 97		if err == nil && index-1 >= 0 && index-1 < len(b.Snapshot().Columns) {
 98			return b, b.Snapshot().Columns[index-1].CombinedId, nil
 99		}
100		fallthrough // could be an Id
101	case _select.IsErrNoValidId(err):
102		return env.Backend.Boards().ResolveColumn(column)
103
104	default:
105		// actual error
106		return nil, entity.UnsetCombinedId, err
107	}
108}