title_edit.go

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