2020-07-09 09:59:47 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-08-19 00:34:05 +03:00
|
|
|
"github.com/MichaelMure/git-bug/commands/input"
|
2020-07-09 09:59:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type commentEditOptions struct {
|
2021-05-09 12:14:45 +03:00
|
|
|
messageFile string
|
|
|
|
message string
|
|
|
|
nonInteractive bool
|
2020-07-09 09:59:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCommentEditCommand() *cobra.Command {
|
|
|
|
env := newEnv()
|
|
|
|
options := commentEditOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2021-05-09 12:33:20 +03:00
|
|
|
Use: "edit [COMMENT_ID]",
|
|
|
|
Short: "Edit an existing comment on a bug.",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
PreRunE: loadBackendEnsureUser(env),
|
|
|
|
RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
|
2020-07-09 09:59:47 +03:00
|
|
|
return runCommentEdit(env, options, args)
|
2021-05-09 12:33:20 +03:00
|
|
|
}),
|
2020-07-09 09:59:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SortFlags = false
|
|
|
|
|
|
|
|
flags.StringVarP(&options.messageFile, "file", "F", "",
|
|
|
|
"Take the message from the given file. Use - to read the message from the standard input")
|
|
|
|
|
|
|
|
flags.StringVarP(&options.message, "message", "m", "",
|
|
|
|
"Provide the new message from the command line")
|
2021-05-09 12:14:45 +03:00
|
|
|
flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
|
2020-07-09 09:59:47 +03:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommentEdit(env *Env, opts commentEditOptions, args []string) error {
|
|
|
|
b, commentId, err := env.backend.ResolveComment(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.messageFile != "" && opts.message == "" {
|
|
|
|
opts.message, err = input.BugCommentFileInput(opts.messageFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.messageFile == "" && opts.message == "" {
|
2021-05-09 12:14:45 +03:00
|
|
|
if opts.nonInteractive {
|
|
|
|
env.err.Println("No message given. Use -m or -F option to specify a message. Aborting.")
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-09 09:59:47 +03:00
|
|
|
opts.message, err = input.BugCommentEditorInput(env.backend, "")
|
|
|
|
if err == input.ErrEmptyMessage {
|
|
|
|
env.err.Println("Empty message, aborting.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = b.EditComment(commentId, opts.message)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Commit()
|
|
|
|
}
|