bug_new.go

 1package bugcmd
 2
 3import (
 4	"github.com/spf13/cobra"
 5
 6	buginput "github.com/git-bug/git-bug/commands/bug/input"
 7	"github.com/git-bug/git-bug/commands/execenv"
 8	"github.com/git-bug/git-bug/util/text"
 9)
10
11type bugNewOptions struct {
12	title          string
13	message        string
14	messageFile    string
15	nonInteractive bool
16}
17
18func newBugNewCommand(env *execenv.Env) *cobra.Command {
19	options := bugNewOptions{}
20
21	cmd := &cobra.Command{
22		Use:     "new",
23		Short:   "Create a new bug",
24		PreRunE: execenv.LoadBackendEnsureUser(env),
25		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
26			return runBugNew(env, options)
27		}),
28	}
29
30	flags := cmd.Flags()
31	flags.SortFlags = false
32
33	flags.StringVarP(&options.title, "title", "t", "",
34		"Provide a title to describe the issue")
35	flags.StringVarP(&options.message, "message", "m", "",
36		"Provide a message to describe the issue")
37	flags.StringVarP(&options.messageFile, "file", "F", "",
38		"Take the message from the given file. Use - to read the message from the standard input")
39	flags.BoolVar(&options.nonInteractive, "non-interactive", false, "Do not ask for user input")
40
41	return cmd
42}
43
44func runBugNew(env *execenv.Env, opts bugNewOptions) error {
45	var err error
46	if opts.messageFile != "" && opts.message == "" {
47		opts.title, opts.message, err = buginput.BugCreateFileInput(opts.messageFile)
48		if err != nil {
49			return err
50		}
51	}
52
53	if !opts.nonInteractive && opts.messageFile == "" && (opts.message == "" || opts.title == "") {
54		opts.title, opts.message, err = buginput.BugCreateEditorInput(env.Backend, opts.title, opts.message)
55
56		if err == buginput.ErrEmptyTitle {
57			env.Out.Println("Empty title, aborting.")
58			return nil
59		}
60		if err != nil {
61			return err
62		}
63	}
64
65	b, _, err := env.Backend.Bugs().New(
66		text.CleanupOneLine(opts.title),
67		text.Cleanup(opts.message),
68	)
69	if err != nil {
70		return err
71	}
72
73	env.Out.Printf("%s created\n", b.Id().Human())
74
75	return nil
76}