1package commands
2
3import (
4 "github.com/spf13/cobra"
5
6 "github.com/MichaelMure/git-bug/cache"
7 "github.com/MichaelMure/git-bug/commands/select"
8 "github.com/MichaelMure/git-bug/input"
9 "github.com/MichaelMure/git-bug/util/interrupt"
10)
11
12type titleEditOptions struct {
13 title string
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: loadRepoEnsureUser(env),
24 RunE: func(cmd *cobra.Command, args []string) error {
25 return runTitleEdit(env, options, args)
26 },
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
36 return cmd
37}
38
39func runTitleEdit(env *Env, opts titleEditOptions, args []string) error {
40 backend, err := cache.NewRepoCache(env.repo)
41 if err != nil {
42 return err
43 }
44 defer backend.Close()
45 interrupt.RegisterCleaner(backend.Close)
46
47 b, args, err := _select.ResolveBug(backend, args)
48 if err != nil {
49 return err
50 }
51
52 snap := b.Snapshot()
53
54 if opts.title == "" {
55 opts.title, err = input.BugTitleEditorInput(env.repo, snap.Title)
56 if err == input.ErrEmptyTitle {
57 env.out.Println("Empty title, aborting.")
58 return nil
59 }
60 if err != nil {
61 return err
62 }
63 }
64
65 if opts.title == snap.Title {
66 env.err.Println("No change, aborting.")
67 }
68
69 _, err = b.SetTitle(opts.title)
70 if err != nil {
71 return err
72 }
73
74 return b.Commit()
75}