1package bugcmd
2
3import (
4 "github.com/spf13/cobra"
5
6 buginput "github.com/MichaelMure/git-bug/commands/bug/input"
7 "github.com/MichaelMure/git-bug/commands/execenv"
8 "github.com/MichaelMure/git-bug/util/text"
9)
10
11type bugTitleEditOptions struct {
12 title string
13 nonInteractive bool
14}
15
16func newBugTitleEditCommand(env *execenv.Env) *cobra.Command {
17 options := bugTitleEditOptions{}
18
19 cmd := &cobra.Command{
20 Use: "edit [BUG_ID]",
21 Short: "Edit a title of a bug",
22 PreRunE: execenv.LoadBackendEnsureUser(env),
23 RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
24 return runBugTitleEdit(env, options, args)
25 }),
26 ValidArgsFunction: BugCompletion(env),
27 }
28
29 flags := cmd.Flags()
30 flags.SortFlags = false
31
32 flags.StringVarP(&options.title, "title", "t", "",
33 "Provide a title to describe the issue",
34 )
35 flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
36
37 return cmd
38}
39
40func runBugTitleEdit(env *execenv.Env, opts bugTitleEditOptions, args []string) error {
41 b, _, err := ResolveSelected(env.Backend, args)
42 if err != nil {
43 return err
44 }
45
46 snap := b.Compile()
47
48 if opts.title == "" {
49 if opts.nonInteractive {
50 env.Err.Println("No title given. Use -m or -F option to specify a title. Aborting.")
51 return nil
52 }
53 opts.title, err = buginput.BugTitleEditorInput(env.Repo, snap.Title)
54 if err == buginput.ErrEmptyTitle {
55 env.Out.Println("Empty title, aborting.")
56 return nil
57 }
58 if err != nil {
59 return err
60 }
61 }
62
63 if opts.title == snap.Title {
64 env.Err.Println("No change, aborting.")
65 }
66
67 _, err = b.SetTitle(text.CleanupOneLine(opts.title))
68 if err != nil {
69 return err
70 }
71
72 return b.Commit()
73}