2018-07-12 13:44:46 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-06-28 19:26:29 +03:00
|
|
|
"github.com/spf13/cobra"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2018-07-31 16:18:09 +03:00
|
|
|
"github.com/MichaelMure/git-bug/input"
|
2021-04-17 18:40:11 +03:00
|
|
|
"github.com/MichaelMure/git-bug/util/text"
|
2018-07-12 13:44:46 +03:00
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
type addOptions struct {
|
|
|
|
title string
|
|
|
|
message string
|
|
|
|
messageFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAddCommand() *cobra.Command {
|
|
|
|
env := newEnv()
|
|
|
|
options := addOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2020-06-28 20:09:32 +03:00
|
|
|
Use: "add",
|
|
|
|
Short: "Create a new bug.",
|
|
|
|
PreRunE: loadBackendEnsureUser(env),
|
|
|
|
PostRunE: closeBackend(env),
|
2020-06-28 19:26:29 +03:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runAdd(env, options)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.SortFlags = false
|
2018-07-12 13:44:46 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
flags.StringVarP(&options.title, "title", "t", "",
|
|
|
|
"Provide a title to describe the issue")
|
|
|
|
flags.StringVarP(&options.message, "message", "m", "",
|
|
|
|
"Provide a message to describe the issue")
|
|
|
|
flags.StringVarP(&options.messageFile, "file", "F", "",
|
|
|
|
"Take the message from the given file. Use - to read the message from the standard input")
|
2018-07-12 13:44:46 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAdd(env *Env, opts addOptions) error {
|
2020-06-28 20:09:32 +03:00
|
|
|
var err error
|
2020-06-28 19:26:29 +03:00
|
|
|
if opts.messageFile != "" && opts.message == "" {
|
|
|
|
opts.title, opts.message, err = input.BugCreateFileInput(opts.messageFile)
|
2018-10-17 22:42:13 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
if opts.messageFile == "" && (opts.message == "" || opts.title == "") {
|
2020-06-28 20:09:32 +03:00
|
|
|
opts.title, opts.message, err = input.BugCreateEditorInput(env.backend, opts.title, opts.message)
|
2018-07-31 16:18:09 +03:00
|
|
|
|
|
|
|
if err == input.ErrEmptyTitle {
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Println("Empty title, aborting.")
|
2018-07-31 16:18:09 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-12 13:44:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 18:40:11 +03:00
|
|
|
b, _, err := env.backend.NewBug(
|
|
|
|
text.CleanupOneLine(opts.title),
|
|
|
|
text.Cleanup(opts.message),
|
|
|
|
)
|
2018-07-25 19:01:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-17 02:52:56 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Printf("%s created\n", b.Id().Human())
|
2018-07-12 13:44:46 +03:00
|
|
|
|
2018-07-25 19:01:32 +03:00
|
|
|
return nil
|
2018-07-12 13:44:46 +03:00
|
|
|
}
|