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