1package commands
 2
 3import (
 4	"fmt"
 5
 6	"github.com/MichaelMure/git-bug/cache"
 7	"github.com/MichaelMure/git-bug/input"
 8	"github.com/spf13/cobra"
 9)
10
11var (
12	newTitle       string
13	newMessage     string
14	newMessageFile string
15)
16
17func runNewBug(cmd *cobra.Command, args []string) error {
18	var err error
19
20	if newMessageFile != "" && newMessage == "" {
21		newMessage, err = input.FromFile(newMessageFile)
22		if err != nil {
23			return err
24		}
25	}
26
27	backend, err := cache.NewRepoCache(repo)
28	if err != nil {
29		return err
30	}
31	defer backend.Close()
32
33	if newMessage == "" || newTitle == "" {
34		newTitle, newMessage, err = input.BugCreateEditorInput(backend.Repository(), newTitle, newMessage)
35
36		if err == input.ErrEmptyTitle {
37			fmt.Println("Empty title, aborting.")
38			return nil
39		}
40		if err != nil {
41			return err
42		}
43	}
44
45	b, err := backend.NewBug(newTitle, newMessage)
46	if err != nil {
47		return err
48	}
49
50	fmt.Printf("%s created\n", b.HumanId())
51
52	return nil
53}
54
55var newCmd = &cobra.Command{
56	Use:   "new",
57	Short: "Create a new bug",
58	RunE:  runNewBug,
59}
60
61func init() {
62	RootCmd.AddCommand(newCmd)
63
64	newCmd.Flags().StringVarP(&newTitle, "title", "t", "",
65		"Provide a title to describe the issue",
66	)
67	newCmd.Flags().StringVarP(&newMessage, "message", "m", "",
68		"Provide a message to describe the issue",
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}