diff options
Diffstat (limited to 'commands/add.go')
-rw-r--r-- | commands/add.go | 80 |
1 files changed, 40 insertions, 40 deletions
diff --git a/commands/add.go b/commands/add.go index e656a262..8b5facaf 100644 --- a/commands/add.go +++ b/commands/add.go @@ -1,42 +1,65 @@ package commands import ( - "fmt" + "github.com/spf13/cobra" "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/input" "github.com/MichaelMure/git-bug/util/interrupt" - "github.com/spf13/cobra" ) -var ( - addTitle string - addMessage string - addMessageFile string -) +type addOptions struct { + title string + message string + messageFile string +} + +func newAddCommand() *cobra.Command { + env := newEnv() + options := addOptions{} + + cmd := &cobra.Command{ + Use: "add", + Short: "Create a new bug.", + PreRunE: loadRepoEnsureUser(env), + RunE: func(cmd *cobra.Command, args []string) error { + return runAdd(env, options) + }, + } + + flags := cmd.Flags() + flags.SortFlags = false -func runAddBug(cmd *cobra.Command, args []string) error { - var err error + flags.StringVarP(&options.title, "title", "t", "", + "Provide a title to describe the issue") + flags.StringVarP(&options.message, "message", "m", "", + "Provide a message to describe the issue") + flags.StringVarP(&options.messageFile, "file", "F", "", + "Take the message from the given file. Use - to read the message from the standard input") - backend, err := cache.NewRepoCache(repo) + return cmd +} + +func runAdd(env *Env, opts addOptions) error { + backend, err := cache.NewRepoCache(env.repo) if err != nil { return err } defer backend.Close() interrupt.RegisterCleaner(backend.Close) - if addMessageFile != "" && addMessage == "" { - addTitle, addMessage, err = input.BugCreateFileInput(addMessageFile) + if opts.messageFile != "" && opts.message == "" { + opts.title, opts.message, err = input.BugCreateFileInput(opts.messageFile) if err != nil { return err } } - if addMessageFile == "" && (addMessage == "" || addTitle == "") { - addTitle, addMessage, err = input.BugCreateEditorInput(backend, addTitle, addMessage) + if opts.messageFile == "" && (opts.message == "" || opts.title == "") { + opts.title, opts.message, err = input.BugCreateEditorInput(backend, opts.title, opts.message) if err == input.ErrEmptyTitle { - fmt.Println("Empty title, aborting.") + env.out.Println("Empty title, aborting.") return nil } if err != nil { @@ -44,35 +67,12 @@ func runAddBug(cmd *cobra.Command, args []string) error { } } - b, _, err := backend.NewBug(addTitle, addMessage) + b, _, err := backend.NewBug(opts.title, opts.message) if err != nil { return err } - fmt.Printf("%s created\n", b.Id().Human()) + env.out.Printf("%s created\n", b.Id().Human()) return nil } - -var addCmd = &cobra.Command{ - Use: "add", - Short: "Create a new bug.", - PreRunE: loadRepoEnsureUser, - RunE: runAddBug, -} - -func init() { - RootCmd.AddCommand(addCmd) - - addCmd.Flags().SortFlags = false - - addCmd.Flags().StringVarP(&addTitle, "title", "t", "", - "Provide a title to describe the issue", - ) - addCmd.Flags().StringVarP(&addMessage, "message", "m", "", - "Provide a message to describe the issue", - ) - addCmd.Flags().StringVarP(&addMessageFile, "file", "F", "", - "Take the message from the given file. Use - to read the message from the standard input", - ) -} |