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 RunE: closeBackend(env, func(cmd *cobra.Command, args []string) error {
26 return runAdd(env, options)
27 }),
28 }
29
30 flags := cmd.Flags()
31 flags.SortFlags = false
32
33 flags.StringVarP(&options.title, "title", "t", "",
34 "Provide a title to describe the issue")
35 flags.StringVarP(&options.message, "message", "m", "",
36 "Provide a message to describe the issue")
37 flags.StringVarP(&options.messageFile, "file", "F", "",
38 "Take the message from the given file. Use - to read the message from the standard input")
39 flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
40
41 return cmd
42}
43
44func runAdd(env *Env, opts addOptions) error {
45 var err error
46 if opts.messageFile != "" && opts.message == "" {
47 opts.title, opts.message, err = input.BugCreateFileInput(opts.messageFile)
48 if err != nil {
49 return err
50 }
51 }
52
53 if !opts.nonInteractive && opts.messageFile == "" && (opts.message == "" || opts.title == "") {
54 opts.title, opts.message, err = input.BugCreateEditorInput(env.backend, opts.title, opts.message)
55
56 if err == input.ErrEmptyTitle {
57 env.out.Println("Empty title, aborting.")
58 return nil
59 }
60 if err != nil {
61 return err
62 }
63 }
64
65 b, _, err := env.backend.NewBug(
66 text.CleanupOneLine(opts.title),
67 text.Cleanup(opts.message),
68 )
69 if err != nil {
70 return err
71 }
72
73 env.out.Printf("%s created\n", b.Id().Human())
74
75 return nil
76}