1package commands
2
3import (
4 "errors"
5 "fmt"
6 "github.com/MichaelMure/git-bug/bug"
7 "github.com/MichaelMure/git-bug/bug/operations"
8 "github.com/MichaelMure/git-bug/commands/input"
9 "github.com/spf13/cobra"
10)
11
12var (
13 newMessageFile string
14 newMessage string
15)
16
17func runNewBug(cmd *cobra.Command, args []string) error {
18 var err error
19
20 if len(args) == 0 {
21 return errors.New("No title provided")
22 }
23 if len(args) > 1 {
24 return errors.New("Only accepting one title is supported")
25 }
26
27 title := args[0]
28
29 if newMessageFile != "" && newMessage == "" {
30 newMessage, err = input.FromFile(newMessageFile)
31 if err != nil {
32 return err
33 }
34 }
35 if newMessageFile == "" && newMessage == "" {
36 newMessage, err = input.LaunchEditor(repo, messageFilename)
37 if err != nil {
38 return err
39 }
40 }
41
42 author, err := bug.GetUser(repo)
43 if err != nil {
44 return err
45 }
46
47 newBug, err := operations.Create(author, title, newMessage)
48 if err != nil {
49 return err
50 }
51
52 err = newBug.Commit(repo)
53
54 if err != nil {
55 return err
56 }
57
58 fmt.Printf("%s created\n", newBug.HumanId())
59
60 return nil
61}
62
63var newCmd = &cobra.Command{
64 Use: "new <title> [<option>...]",
65 Short: "Create a new bug",
66 RunE: runNewBug,
67}
68
69func init() {
70 RootCmd.AddCommand(newCmd)
71
72 newCmd.Flags().StringVarP(&newMessageFile, "file", "F", "",
73 "Take the message from the given file. Use - to read the message from the standard input",
74 )
75 newCmd.Flags().StringVarP(&newMessage, "message", "m", "",
76 "Provide a message to describe the issue",
77 )
78}