new.go

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