Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0dbc14
commit 26bd1dd
Showing
42 changed files
with
1,339 additions
and
1,122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,78 @@ | ||
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 { | ||
return err | ||
} | ||
} | ||
|
||
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", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.