1package commands
2
3import (
4 "github.com/spf13/cobra"
5
6 "github.com/MichaelMure/git-bug/commands/input"
7)
8
9type commentEditOptions struct {
10 messageFile string
11 message string
12 nonInteractive bool
13}
14
15func newCommentEditCommand() *cobra.Command {
16 env := newEnv()
17 options := commentEditOptions{}
18
19 cmd := &cobra.Command{
20 Use: "edit [COMMENT_ID]",
21 Short: "Edit an existing comment on a bug.",
22 Args: cobra.ExactArgs(1),
23 PreRunE: loadBackendEnsureUser(env),
24 RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
25 return runCommentEdit(env, options, args)
26 }),
27 ValidArgsFunction: completeComment(env),
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 flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
39
40 return cmd
41}
42
43func runCommentEdit(env *Env, opts commentEditOptions, args []string) error {
44 b, commentId, err := env.backend.ResolveComment(args[0])
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 if opts.nonInteractive {
58 env.err.Println("No message given. Use -m or -F option to specify a message. Aborting.")
59 return nil
60 }
61 opts.message, err = input.BugCommentEditorInput(env.backend, "")
62 if err == input.ErrEmptyMessage {
63 env.err.Println("Empty message, aborting.")
64 return nil
65 }
66 if err != nil {
67 return err
68 }
69 }
70
71 _, err = b.EditComment(commentId, opts.message)
72 if err != nil {
73 return err
74 }
75
76 return b.Commit()
77}