new.go

 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 := bug.NewBug()
48
49	createOp := operations.NewCreateOp(author, title, newMessage)
50
51	newBug.Append(createOp)
52
53	err = newBug.Commit(repo)
54
55	fmt.Printf("%s created\n", newBug.HumanId())
56
57	return err
58
59}
60
61var newCmd = &cobra.Command{
62	Use:   "new <title> [<option>...]",
63	Short: "Create a new bug",
64	RunE:  runNewBug,
65}
66
67func init() {
68	RootCmd.AddCommand(newCmd)
69
70	newCmd.Flags().StringVarP(&newMessageFile, "file", "F", "",
71		"Take the message from the given file. Use - to read the message from the standard input",
72	)
73	newCmd.Flags().StringVarP(&newMessage, "message", "m", "",
74		"Provide a message to describe the issue",
75	)
76}