comment_edit.go

 1package commands
 2
 3import (
 4	"github.com/spf13/cobra"
 5
 6	_select "github.com/MichaelMure/git-bug/commands/select"
 7	"github.com/MichaelMure/git-bug/input"
 8)
 9
10type commentEditOptions struct {
11	messageFile string
12	message     string
13}
14
15func newCommentEditCommand() *cobra.Command {
16	env := newEnv()
17	options := commentEditOptions{}
18
19	cmd := &cobra.Command{
20		Use:      "edit <id>",
21		Short:    "Edit an existing comment on a bug.",
22		Args:     cobra.ExactArgs(1),
23		PreRunE:  loadBackendEnsureUser(env),
24		PostRunE: closeBackend(env),
25		RunE: func(cmd *cobra.Command, args []string) error {
26			return runCommentEdit(env, options, args)
27		},
28	}
29
30	flags := cmd.Flags()
31	flags.SortFlags = false
32
33	flags.StringVarP(&options.messageFile, "file", "F", "",
34		"Take the message from the given file. Use - to read the message from the standard input")
35
36	flags.StringVarP(&options.message, "message", "m", "",
37		"Provide the new message from the command line")
38
39	return cmd
40}
41
42func runCommentEdit(env *Env, opts commentEditOptions, args []string) error {
43	b, c, err := _select.ResolveComment(env.backend, args[0])
44
45	if err != nil {
46		return err
47	}
48
49	if opts.messageFile != "" && opts.message == "" {
50		opts.message, err = input.BugCommentFileInput(opts.messageFile)
51		if err != nil {
52			return err
53		}
54	}
55
56	if opts.messageFile == "" && opts.message == "" {
57		opts.message, err = input.BugCommentEditorInput(env.backend, "")
58		if err == input.ErrEmptyMessage {
59			env.err.Println("Empty message, aborting.")
60			return nil
61		}
62		if err != nil {
63			return err
64		}
65	}
66
67	_, err = b.EditComment(c, opts.message)
68	if err != nil {
69		return err
70	}
71
72	return b.Commit()
73}