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