diff options
author | Michael Muré <batolettre@gmail.com> | 2018-10-17 21:42:13 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-10-17 21:42:13 +0200 |
commit | d37ffa6bd49f6f39a4de03dc2fa101832a1befe8 (patch) | |
tree | ec2a2b1f261eed43d3ace51e5ba4d806189539b9 | |
parent | f67c57c08407b935c82b8f47d36823725351efe0 (diff) | |
download | git-bug-d37ffa6bd49f6f39a4de03dc2fa101832a1befe8.tar.gz |
commands: unify the processing from editor/file/stdin for "add" and "comment add"
fix #68
-rw-r--r-- | commands/add.go | 16 | ||||
-rw-r--r-- | commands/comment_add.go | 4 | ||||
-rw-r--r-- | input/input.go | 34 |
3 files changed, 42 insertions, 12 deletions
diff --git a/commands/add.go b/commands/add.go index 081c99f1..ecc2381e 100644 --- a/commands/add.go +++ b/commands/add.go @@ -17,20 +17,20 @@ var ( func runAddBug(cmd *cobra.Command, args []string) error { var err error - if addMessageFile != "" && addMessage == "" { - addMessage, err = input.FromFile(addMessageFile) - if err != nil { - return err - } - } - backend, err := cache.NewRepoCache(repo) if err != nil { return err } defer backend.Close() - if addMessage == "" || addTitle == "" { + if addMessageFile != "" && addMessage == "" { + addTitle, addMessage, err = input.BugCreateFileInput(addMessageFile) + if err != nil { + return err + } + } + + if addMessageFile == "" && (addMessage == "" || addTitle == "") { addTitle, addMessage, err = input.BugCreateEditorInput(backend, addTitle, addMessage) if err == input.ErrEmptyTitle { diff --git a/commands/comment_add.go b/commands/comment_add.go index 5a7cd6a8..d0912844 100644 --- a/commands/comment_add.go +++ b/commands/comment_add.go @@ -27,13 +27,13 @@ func runCommentAdd(cmd *cobra.Command, args []string) error { } if commentAddMessageFile != "" && commentAddMessage == "" { - commentAddMessage, err = input.FromFile(commentAddMessageFile) + commentAddMessage, err = input.BugCommentFileInput(commentAddMessageFile) if err != nil { return err } } - if commentAddMessage == "" { + if commentAddMessageFile == "" && commentAddMessage == "" { commentAddMessage, err = input.BugCommentEditorInput(backend, "") if err == input.ErrEmptyMessage { fmt.Println("Empty message, aborting.") diff --git a/input/input.go b/input/input.go index 07148acc..c36b9046 100644 --- a/input/input.go +++ b/input/input.go @@ -48,6 +48,21 @@ func BugCreateEditorInput(repo repository.RepoCommon, preTitle string, preMessag return "", "", err } + return processCreate(raw) +} + +// BugCreateFileInput read from either from a file or from the standard input +// and extract a title and a message +func BugCreateFileInput(fileName string) (string, string, error) { + raw, err := fromFile(fileName) + if err != nil { + return "", "", err + } + + return processCreate(raw) +} + +func processCreate(raw string) (string, string, error) { lines := strings.Split(raw, "\n") var title string @@ -94,6 +109,21 @@ func BugCommentEditorInput(repo repository.RepoCommon, preMessage string) (strin return "", err } + return processComment(raw) +} + +// BugCommentFileInput read from either from a file or from the standard input +// and extract a message +func BugCommentFileInput(fileName string) (string, error) { + raw, err := fromFile(fileName) + if err != nil { + return "", err + } + + return processComment(raw) +} + +func processComment(raw string) (string, error) { lines := strings.Split(raw, "\n") var buffer bytes.Buffer @@ -266,11 +296,11 @@ func launchEditor(repo repository.RepoCommon, fileName string) (string, error) { return string(output), err } -// FromFile loads and returns the contents of a given file. If - is passed +// fromFile loads and returns the contents of a given file. If - is passed // through, much like git, it will read from stdin. This can be piped data, // unless there is a tty in which case the user will be prompted to enter a // message. -func FromFile(fileName string) (string, error) { +func fromFile(fileName string) (string, error) { if fileName == "-" { stat, err := os.Stdin.Stat() if err != nil { |