1package commands
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/cache"
7 "github.com/MichaelMure/git-bug/input"
8 "github.com/MichaelMure/git-bug/util/interrupt"
9 "github.com/spf13/cobra"
10)
11
12var (
13 addTitle string
14 addMessage string
15 addMessageFile string
16)
17
18func runAddBug(cmd *cobra.Command, args []string) error {
19 var err error
20
21 backend, err := cache.NewRepoCache(repo)
22 if err != nil {
23 return err
24 }
25 defer backend.Close()
26 interrupt.RegisterCleaner(backend.Close)
27
28 if addMessageFile != "" && addMessage == "" {
29 addTitle, addMessage, err = input.BugCreateFileInput(addMessageFile)
30 if err != nil {
31 return err
32 }
33 }
34
35 if addMessageFile == "" && (addMessage == "" || addTitle == "") {
36 addTitle, addMessage, err = input.BugCreateEditorInput(backend, addTitle, addMessage)
37
38 if err == input.ErrEmptyTitle {
39 fmt.Println("Empty title, aborting.")
40 return nil
41 }
42 if err != nil {
43 return err
44 }
45 }
46
47 b, _, err := backend.NewBug(addTitle, addMessage)
48 if err != nil {
49 return err
50 }
51
52 fmt.Printf("%s created\n", b.Id().Human())
53
54 return nil
55}
56
57var addCmd = &cobra.Command{
58 Use: "add",
59 Short: "Create a new bug.",
60 PreRunE: loadRepo,
61 RunE: runAddBug,
62}
63
64func init() {
65 RootCmd.AddCommand(addCmd)
66
67 addCmd.Flags().SortFlags = false
68
69 addCmd.Flags().StringVarP(&addTitle, "title", "t", "",
70 "Provide a title to describe the issue",
71 )
72 addCmd.Flags().StringVarP(&addMessage, "message", "m", "",
73 "Provide a message to describe the issue",
74 )
75 addCmd.Flags().StringVarP(&addMessageFile, "file", "F", "",
76 "Take the message from the given file. Use - to read the message from the standard input",
77 )
78}