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}
15
16func newAddCommand() *cobra.Command {
17 env := newEnv()
18 options := addOptions{}
19
20 cmd := &cobra.Command{
21 Use: "add",
22 Short: "Create a new bug.",
23 PreRunE: loadBackendEnsureUser(env),
24 PostRunE: closeBackend(env),
25 RunE: 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
40 return cmd
41}
42
43func runAdd(env *Env, opts addOptions) error {
44 var err error
45 if opts.messageFile != "" && opts.message == "" {
46 opts.title, opts.message, err = input.BugCreateFileInput(opts.messageFile)
47 if err != nil {
48 return err
49 }
50 }
51
52 if opts.messageFile == "" && (opts.message == "" || opts.title == "") {
53 opts.title, opts.message, err = input.BugCreateEditorInput(env.backend, opts.title, opts.message)
54
55 if err == input.ErrEmptyTitle {
56 env.out.Println("Empty title, aborting.")
57 return nil
58 }
59 if err != nil {
60 return err
61 }
62 }
63
64 b, _, err := env.backend.NewBug(
65 text.CleanupOneLine(opts.title),
66 text.Cleanup(opts.message),
67 )
68 if err != nil {
69 return err
70 }
71
72 env.out.Printf("%s created\n", b.Id().Human())
73
74 return nil
75}