1package commands
 2
 3import (
 4	"fmt"
 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	"github.com/spf13/cobra"
11)
12
13var (
14	titleEditTitle string
15)
16
17func runTitleEdit(cmd *cobra.Command, args []string) error {
18	backend, err := cache.NewRepoCache(repo)
19	if err != nil {
20		return err
21	}
22	defer backend.Close()
23	interrupt.RegisterCleaner(backend.Close)
24
25	b, args, err := _select.ResolveBug(backend, args)
26	if err != nil {
27		return err
28	}
29
30	snap := b.Snapshot()
31
32	if titleEditTitle == "" {
33		titleEditTitle, err = input.BugTitleEditorInput(repo, snap.Title)
34		if err == input.ErrEmptyTitle {
35			fmt.Println("Empty title, aborting.")
36			return nil
37		}
38		if err != nil {
39			return err
40		}
41	}
42
43	if titleEditTitle == snap.Title {
44		fmt.Println("No change, aborting.")
45	}
46
47	_, err = b.SetTitle(titleEditTitle)
48	if err != nil {
49		return err
50	}
51
52	return b.Commit()
53}
54
55var titleEditCmd = &cobra.Command{
56	Use:     "edit [<id>]",
57	Short:   "Edit a title of a bug.",
58	PreRunE: loadRepoEnsureUser,
59	RunE:    runTitleEdit,
60}
61
62func init() {
63	titleCmd.AddCommand(titleEditCmd)
64
65	titleEditCmd.Flags().SortFlags = false
66
67	titleEditCmd.Flags().StringVarP(&titleEditTitle, "title", "t", "",
68		"Provide a title to describe the issue",
69	)
70}