1package bugcmd
2
3import (
4 "github.com/spf13/cobra"
5
6 "github.com/MichaelMure/git-bug/commands/execenv"
7 "github.com/MichaelMure/git-bug/commands/input"
8 "github.com/MichaelMure/git-bug/util/text"
9)
10
11type bugNewOptions struct {
12 title string
13 message string
14 messageFile string
15 nonInteractive bool
16}
17
18func newBugNewCommand() *cobra.Command {
19 env := execenv.NewEnv()
20 options := bugNewOptions{}
21
22 cmd := &cobra.Command{
23 Use: "new",
24 Short: "Create a new bug",
25 PreRunE: execenv.LoadBackendEnsureUser(env),
26 RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
27 return runBugNew(env, options)
28 }),
29 }
30
31 flags := cmd.Flags()
32 flags.SortFlags = false
33
34 flags.StringVarP(&options.title, "title", "t", "",
35 "Provide a title to describe the issue")
36 flags.StringVarP(&options.message, "message", "m", "",
37 "Provide a message to describe the issue")
38 flags.StringVarP(&options.messageFile, "file", "F", "",
39 "Take the message from the given file. Use - to read the message from the standard input")
40 flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
41
42 return cmd
43}
44
45func runBugNew(env *execenv.Env, opts bugNewOptions) error {
46 var err error
47 if opts.messageFile != "" && opts.message == "" {
48 opts.title, opts.message, err = input.BugCreateFileInput(opts.messageFile)
49 if err != nil {
50 return err
51 }
52 }
53
54 if !opts.nonInteractive && opts.messageFile == "" && (opts.message == "" || opts.title == "") {
55 opts.title, opts.message, err = input.BugCreateEditorInput(env.Backend, opts.title, opts.message)
56
57 if err == input.ErrEmptyTitle {
58 env.Out.Println("Empty title, aborting.")
59 return nil
60 }
61 if err != nil {
62 return err
63 }
64 }
65
66 b, _, err := env.Backend.NewBug(
67 text.CleanupOneLine(opts.title),
68 text.Cleanup(opts.message),
69 )
70 if err != nil {
71 return err
72 }
73
74 env.Out.Printf("%s created\n", b.Id().Human())
75
76 return nil
77}