add.go

 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	addTitle       string
13	addMessage     string
14	addMessageFile string
15)
16
17func runAddBug(cmd *cobra.Command, args []string) error {
18	var err error
19
20	if addMessageFile != "" && addMessage == "" {
21		addMessage, err = input.FromFile(addMessageFile)
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 addMessage == "" || addTitle == "" {
34		addTitle, addMessage, err = input.BugCreateEditorInput(backend.Repository(), addTitle, addMessage)
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(addTitle, addMessage)
46	if err != nil {
47		return err
48	}
49
50	fmt.Printf("%s created\n", b.HumanId())
51
52	return nil
53}
54
55var addCmd = &cobra.Command{
56	Use:   "add",
57	Short: "Create a new bug",
58	RunE:  runAddBug,
59}
60
61func init() {
62	RootCmd.AddCommand(addCmd)
63
64	addCmd.Flags().SortFlags = false
65
66	addCmd.Flags().StringVarP(&addTitle, "title", "t", "",
67		"Provide a title to describe the issue",
68	)
69	addCmd.Flags().StringVarP(&addMessage, "message", "m", "",
70		"Provide a message to describe the issue",
71	)
72	addCmd.Flags().StringVarP(&addMessageFile, "file", "F", "",
73		"Take the message from the given file. Use - to read the message from the standard input",
74	)
75}