title_edit.go

 1package commands
 2
 3import (
 4	"errors"
 5	"fmt"
 6
 7	"github.com/MichaelMure/git-bug/cache"
 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	var err error
18
19	if len(args) > 1 {
20		return errors.New("Only one bug id is supported")
21	}
22
23	if len(args) == 0 {
24		return errors.New("You must provide a bug id")
25	}
26
27	backend, err := cache.NewRepoCache(repo)
28	if err != nil {
29		return err
30	}
31	defer backend.Close()
32
33	prefix := args[0]
34
35	b, err := backend.ResolveBugPrefix(prefix)
36	if err != nil {
37		return err
38	}
39
40	snap := b.Snapshot()
41
42	if titleEditTitle == "" {
43		titleEditTitle, err = input.BugTitleEditorInput(repo, snap.Title)
44		if err == input.ErrEmptyTitle {
45			fmt.Println("Empty title, aborting.")
46			return nil
47		}
48		if err != nil {
49			return err
50		}
51	}
52
53	err = b.SetTitle(titleEditTitle)
54	if err != nil {
55		return err
56	}
57
58	return b.Commit()
59}
60
61var titleEditCmd = &cobra.Command{
62	Use:   "edit <id>",
63	Short: "Edit a bug title",
64	RunE:  runTitleEdit,
65}
66
67func init() {
68	titleCmd.AddCommand(titleEditCmd)
69
70	titleEditCmd.Flags().SortFlags = false
71
72	titleEditCmd.Flags().StringVarP(&titleEditTitle, "title", "t", "",
73		"Provide a title to describe the issue",
74	)
75}