2022-09-10 12:09:19 +03:00
|
|
|
package bugcmd
|
2020-07-09 09:59:47 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2022-12-27 19:23:56 +03:00
|
|
|
buginput "github.com/MichaelMure/git-bug/commands/bug/input"
|
2022-09-10 12:09:19 +03:00
|
|
|
"github.com/MichaelMure/git-bug/commands/execenv"
|
2020-07-09 09:59:47 +03:00
|
|
|
)
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
type bugCommentEditOptions struct {
|
2021-05-09 12:14:45 +03:00
|
|
|
messageFile string
|
|
|
|
message string
|
|
|
|
nonInteractive bool
|
2020-07-09 09:59:47 +03:00
|
|
|
}
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
func newBugCommentEditCommand() *cobra.Command {
|
|
|
|
env := execenv.NewEnv()
|
|
|
|
options := bugCommentEditOptions{}
|
2020-07-09 09:59:47 +03:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2021-05-09 12:33:20 +03:00
|
|
|
Use: "edit [COMMENT_ID]",
|
2022-09-10 12:09:19 +03:00
|
|
|
Short: "Edit an existing comment on a bug",
|
2021-05-09 12:33:20 +03:00
|
|
|
Args: cobra.ExactArgs(1),
|
2022-09-10 12:09:19 +03:00
|
|
|
PreRunE: execenv.LoadBackendEnsureUser(env),
|
|
|
|
RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runBugCommentEdit(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
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:09:19 +03:00
|
|
|
func runBugCommentEdit(env *execenv.Env, opts bugCommentEditOptions, args []string) error {
|
2022-12-21 23:54:36 +03:00
|
|
|
b, commentId, err := env.Backend.Bugs().ResolveComment(args[0])
|
2020-07-09 09:59:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.messageFile != "" && opts.message == "" {
|
2022-12-27 19:23:56 +03:00
|
|
|
opts.message, err = buginput.BugCommentFileInput(opts.messageFile)
|
2020-07-09 09:59:47 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.messageFile == "" && opts.message == "" {
|
2021-05-09 12:14:45 +03:00
|
|
|
if opts.nonInteractive {
|
2022-09-10 12:09:19 +03:00
|
|
|
env.Err.Println("No message given. Use -m or -F option to specify a message. Aborting.")
|
2021-05-09 12:14:45 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-27 19:23:56 +03:00
|
|
|
opts.message, err = buginput.BugCommentEditorInput(env.Backend, "")
|
|
|
|
if err == buginput.ErrEmptyMessage {
|
2022-09-10 12:09:19 +03:00
|
|
|
env.Err.Println("Empty message, aborting.")
|
2020-07-09 09:59:47 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = b.EditComment(commentId, opts.message)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Commit()
|
|
|
|
}
|