bug_title_edit.go

 1package bugcmd
 2
 3import (
 4	"github.com/spf13/cobra"
 5
 6	"github.com/MichaelMure/git-bug/commands/bug/select"
 7	"github.com/MichaelMure/git-bug/commands/completion"
 8	"github.com/MichaelMure/git-bug/commands/execenv"
 9	"github.com/MichaelMure/git-bug/commands/input"
10	"github.com/MichaelMure/git-bug/util/text"
11)
12
13type bugTitleEditOptions struct {
14	title          string
15	nonInteractive bool
16}
17
18func newBugTitleEditCommand() *cobra.Command {
19	env := execenv.NewEnv()
20	options := bugTitleEditOptions{}
21
22	cmd := &cobra.Command{
23		Use:     "edit [BUG_ID]",
24		Short:   "Edit a title of a bug",
25		PreRunE: execenv.LoadBackendEnsureUser(env),
26		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
27			return runBugTitleEdit(env, options, args)
28		}),
29		ValidArgsFunction: completion.Bug(env),
30	}
31
32	flags := cmd.Flags()
33	flags.SortFlags = false
34
35	flags.StringVarP(&options.title, "title", "t", "",
36		"Provide a title to describe the issue",
37	)
38	flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
39
40	return cmd
41}
42
43func runBugTitleEdit(env *execenv.Env, opts bugTitleEditOptions, args []string) error {
44	b, args, err := _select.ResolveBug(env.Backend, args)
45	if err != nil {
46		return err
47	}
48
49	snap := b.Snapshot()
50
51	if opts.title == "" {
52		if opts.nonInteractive {
53			env.Err.Println("No title given. Use -m or -F option to specify a title. Aborting.")
54			return nil
55		}
56		opts.title, err = input.BugTitleEditorInput(env.Repo, snap.Title)
57		if err == input.ErrEmptyTitle {
58			env.Out.Println("Empty title, aborting.")
59			return nil
60		}
61		if err != nil {
62			return err
63		}
64	}
65
66	if opts.title == snap.Title {
67		env.Err.Println("No change, aborting.")
68	}
69
70	_, err = b.SetTitle(text.CleanupOneLine(opts.title))
71	if err != nil {
72		return err
73	}
74
75	return b.Commit()
76}