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