git-bug/commands/add.go

72 lines
1.6 KiB
Go
Raw Normal View History

package commands
import (
2020-06-28 19:26:29 +03:00
"github.com/spf13/cobra"
2018-08-13 19:32:11 +03:00
"github.com/MichaelMure/git-bug/input"
)
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{
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
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")
2020-06-28 19:26:29 +03:00
return cmd
}
func runAdd(env *Env, opts addOptions) error {
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)
if err != nil {
return err
}
}
2020-06-28 19:26:29 +03:00
if opts.messageFile == "" && (opts.message == "" || opts.title == "") {
opts.title, opts.message, err = input.BugCreateEditorInput(env.backend, opts.title, opts.message)
if err == input.ErrEmptyTitle {
2020-06-28 19:26:29 +03:00
env.out.Println("Empty title, aborting.")
return nil
}
if err != nil {
return err
}
}
b, _, err := env.backend.NewBug(opts.title, opts.message)
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())
return nil
}