1package commands
2
3import (
4 "github.com/spf13/cobra"
5
6 "github.com/MichaelMure/git-bug/input"
7 "github.com/MichaelMure/git-bug/util/text"
8)
9
10type addOptions struct {
11 title string
12 message string
13 messageFile string
14 nonInteractive bool
15}
16
17func newAddCommand() *cobra.Command {
18 env := newEnv()
19 options := addOptions{}
20
21 cmd := &cobra.Command{
22 Use: "add",
23 Short: "Create a new bug.",
24 PreRunE: loadBackendEnsureUser(env),
25 PostRunE: closeBackend(env),
26 RunE: func(cmd *cobra.Command, args []string) error {
27 return runAdd(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 runAdd(env *Env, opts addOptions) 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}