1package boardcmd
2
3import (
4 "github.com/spf13/cobra"
5
6 "github.com/git-bug/git-bug/commands/execenv"
7 "github.com/git-bug/git-bug/commands/input"
8 "github.com/git-bug/git-bug/util/text"
9)
10
11type boardDescriptionEditOptions struct {
12 description string
13 nonInteractive bool
14}
15
16func newBoardDescriptionEditCommand() *cobra.Command {
17 env := execenv.NewEnv()
18 options := boardDescriptionEditOptions{}
19
20 cmd := &cobra.Command{
21 Use: "edit [BUG_ID]",
22 Short: "Edit a description of a board",
23 PreRunE: execenv.LoadBackendEnsureUser(env),
24 RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
25 return runBugDescriptionEdit(env, options, args)
26 }),
27 ValidArgsFunction: BoardCompletion(env),
28 }
29
30 flags := cmd.Flags()
31 flags.SortFlags = false
32
33 flags.StringVarP(&options.description, "description", "t", "",
34 "Provide a description for the board",
35 )
36 flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
37
38 return cmd
39}
40
41func runBugDescriptionEdit(env *execenv.Env, opts boardDescriptionEditOptions, args []string) error {
42 b, args, err := ResolveSelected(env.Backend, args)
43 if err != nil {
44 return err
45 }
46
47 snap := b.Snapshot()
48
49 if opts.description == "" {
50 if opts.nonInteractive {
51 env.Err.Println("No description given. Aborting.")
52 return nil
53 }
54 opts.description, err = input.PromptDefault("Board description", "description", snap.Description, input.Required)
55 if err != nil {
56 return err
57 }
58 }
59
60 if opts.description == snap.Description {
61 env.Err.Println("No change, aborting.")
62 }
63
64 _, err = b.SetDescription(text.CleanupOneLine(opts.description))
65 if err != nil {
66 return err
67 }
68
69 return b.Commit()
70}