1package boardcmd
 2
 3import (
 4	"fmt"
 5	"strings"
 6
 7	"github.com/spf13/cobra"
 8
 9	"github.com/git-bug/git-bug/commands/execenv"
10	"github.com/git-bug/git-bug/commands/input"
11	"github.com/git-bug/git-bug/entities/board"
12	"github.com/git-bug/git-bug/util/text"
13)
14
15type boardNewOptions struct {
16	title          string
17	description    string
18	columns        []string
19	nonInteractive bool
20}
21
22func newBoardNewCommand() *cobra.Command {
23	env := execenv.NewEnv()
24	options := boardNewOptions{}
25
26	cmd := &cobra.Command{
27		Use:     "new",
28		Short:   "Create a new board",
29		PreRunE: execenv.LoadBackendEnsureUser(env),
30		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
31			return runBugNew(env, options)
32		}),
33	}
34
35	flags := cmd.Flags()
36	flags.SortFlags = false
37
38	flags.StringVarP(&options.title, "title", "t", "",
39		"Provide a title to describe the issue")
40	flags.StringVarP(&options.description, "description", "d", "",
41		"Provide a message to describe the board")
42	flags.StringArrayVarP(&options.columns, "columns", "c", board.DefaultColumns,
43		fmt.Sprintf("Define the columns of the board (default to %s)",
44			strings.Join(board.DefaultColumns, ",")))
45	flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
46
47	return cmd
48}
49
50func runBugNew(env *execenv.Env, opts boardNewOptions) error {
51	var err error
52
53	if !opts.nonInteractive && opts.title == "" {
54		opts.title, err = input.Prompt("Board title", "title", input.Required)
55		if err != nil {
56			return err
57		}
58	}
59
60	if !opts.nonInteractive && opts.description == "" {
61		opts.description, err = input.Prompt("Board description", "description")
62		if err != nil {
63			return err
64		}
65	}
66
67	for i, column := range opts.columns {
68		opts.columns[i] = text.Cleanup(column)
69	}
70
71	b, _, err := env.Backend.Boards().New(
72		text.CleanupOneLine(opts.title),
73		text.CleanupOneLine(opts.description),
74		opts.columns,
75	)
76	if err != nil {
77		return err
78	}
79
80	env.Out.Printf("%s created\n", b.Id().Human())
81
82	return nil
83}