aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-07-31 15:18:09 +0200
committerMichael Muré <batolettre@gmail.com>2018-07-31 16:44:23 +0200
commiteb39c5c29bc0e9b5e15a940a1b71bdac688b6535 (patch)
tree5ef9b827f4a3097e4eb1367097d0059f895befbe /commands
parent8a4e373e7b1c093abeb967d9a6a43c5ed533edb8 (diff)
downloadgit-bug-eb39c5c29bc0e9b5e15a940a1b71bdac688b6535.tar.gz
cli: rework new and comment command to better use the editor
a nice templace is now provided with explanations new: title and message can now be provided from the editor. Title will be the first non-empty line
Diffstat (limited to 'commands')
-rw-r--r--commands/comment.go12
-rw-r--r--commands/input/input.go104
-rw-r--r--commands/new.go36
3 files changed, 27 insertions, 125 deletions
diff --git a/commands/comment.go b/commands/comment.go
index ebbeaa77..cebf729c 100644
--- a/commands/comment.go
+++ b/commands/comment.go
@@ -2,9 +2,10 @@ package commands
import (
"errors"
+ "fmt"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/bug/operations"
- "github.com/MichaelMure/git-bug/commands/input"
+ "github.com/MichaelMure/git-bug/input"
"github.com/spf13/cobra"
)
@@ -32,8 +33,13 @@ func runComment(cmd *cobra.Command, args []string) error {
return err
}
}
- if commentMessageFile == "" && commentMessage == "" {
- commentMessage, err = input.LaunchEditor(repo, messageFilename)
+
+ if commentMessage == "" {
+ commentMessage, err = input.BugCommentEditorInput(repo, messageFilename)
+ if err == input.ErrEmptyMessage {
+ fmt.Println("Empty message, aborting.")
+ return nil
+ }
if err != nil {
return err
}
diff --git a/commands/input/input.go b/commands/input/input.go
deleted file mode 100644
index 531a4386..00000000
--- a/commands/input/input.go
+++ /dev/null
@@ -1,104 +0,0 @@
-// Taken from the git-appraise project
-
-package input
-
-import (
- "bufio"
- "bytes"
- "fmt"
- "github.com/MichaelMure/git-bug/repository"
- "io/ioutil"
- "os"
- "os/exec"
-)
-
-// LaunchEditor launches the default editor configured for the given repo. This
-// method blocks until the editor command has returned.
-//
-// The specified filename should be a temporary file and provided as a relative path
-// from the repo (e.g. "FILENAME" will be converted to ".git/FILENAME"). This file
-// will be deleted after the editor is closed and its contents have been read.
-//
-// This method returns the text that was read from the temporary file, or
-// an error if any step in the process failed.
-func LaunchEditor(repo repository.Repo, fileName string) (string, error) {
- editor, err := repo.GetCoreEditor()
- if err != nil {
- return "", fmt.Errorf("Unable to detect default git editor: %v\n", err)
- }
-
- path := fmt.Sprintf("%s/.git/%s", repo.GetPath(), fileName)
-
- cmd, err := startInlineCommand(editor, path)
- if err != nil {
- // Running the editor directly did not work. This might mean that
- // the editor string is not a path to an executable, but rather
- // a shell command (e.g. "emacsclient --tty"). As such, we'll try
- // to run the command through bash, and if that fails, try with sh
- args := []string{"-c", fmt.Sprintf("%s %q", editor, path)}
- cmd, err = startInlineCommand("bash", args...)
- if err != nil {
- cmd, err = startInlineCommand("sh", args...)
- }
- }
- if err != nil {
- return "", fmt.Errorf("Unable to start editor: %v\n", err)
- }
-
- if err := cmd.Wait(); err != nil {
- return "", fmt.Errorf("Editing finished with error: %v\n", err)
- }
-
- output, err := ioutil.ReadFile(path)
- if err != nil {
- os.Remove(path)
- return "", fmt.Errorf("Error reading edited file: %v\n", err)
- }
- os.Remove(path)
- return string(output), err
-}
-
-// 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) {
- if fileName == "-" {
- stat, err := os.Stdin.Stat()
- if err != nil {
- return "", fmt.Errorf("Error reading from stdin: %v\n", err)
- }
- if (stat.Mode() & os.ModeCharDevice) == 0 {
- // There is no tty. This will allow us to read piped data instead.
- output, err := ioutil.ReadAll(os.Stdin)
- if err != nil {
- return "", fmt.Errorf("Error reading from stdin: %v\n", err)
- }
- return string(output), err
- }
-
- fmt.Printf("(reading comment from standard input)\n")
- var output bytes.Buffer
- s := bufio.NewScanner(os.Stdin)
- for s.Scan() {
- output.Write(s.Bytes())
- output.WriteRune('\n')
- }
- return output.String(), nil
- }
-
- output, err := ioutil.ReadFile(fileName)
- if err != nil {
- return "", fmt.Errorf("Error reading file: %v\n", err)
- }
- return string(output), err
-}
-
-func startInlineCommand(command string, args ...string) (*exec.Cmd, error) {
- cmd := exec.Command(command, args...)
- cmd.Stdin = os.Stdin
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
- err := cmd.Start()
- return cmd, err
-}
diff --git a/commands/new.go b/commands/new.go
index 4168453e..88e7cd81 100644
--- a/commands/new.go
+++ b/commands/new.go
@@ -1,39 +1,36 @@
package commands
import (
- "errors"
"fmt"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/bug/operations"
- "github.com/MichaelMure/git-bug/commands/input"
+ "github.com/MichaelMure/git-bug/input"
"github.com/spf13/cobra"
)
var (
- newMessageFile string
+ newTitle string
newMessage string
+ newMessageFile string
)
func runNewBug(cmd *cobra.Command, args []string) error {
var err error
- if len(args) == 0 {
- return errors.New("No title provided")
- }
- if len(args) > 1 {
- return errors.New("Only accepting one title is supported")
- }
-
- title := args[0]
-
if newMessageFile != "" && newMessage == "" {
newMessage, err = input.FromFile(newMessageFile)
if err != nil {
return err
}
}
- if newMessageFile == "" && newMessage == "" {
- newMessage, err = input.LaunchEditor(repo, messageFilename)
+
+ if newMessage == "" || newTitle == "" {
+ newTitle, newMessage, err = input.BugCreateEditorInput(repo, messageFilename, newTitle, newMessage)
+
+ if err == input.ErrEmptyTitle {
+ fmt.Println("Empty title, aborting.")
+ return nil
+ }
if err != nil {
return err
}
@@ -44,7 +41,7 @@ func runNewBug(cmd *cobra.Command, args []string) error {
return err
}
- newBug, err := operations.Create(author, title, newMessage)
+ newBug, err := operations.Create(author, newTitle, newMessage)
if err != nil {
return err
}
@@ -61,7 +58,7 @@ func runNewBug(cmd *cobra.Command, args []string) error {
}
var newCmd = &cobra.Command{
- Use: "new <title> [<option>...]",
+ Use: "new [<option>...]",
Short: "Create a new bug",
RunE: runNewBug,
}
@@ -69,10 +66,13 @@ var newCmd = &cobra.Command{
func init() {
RootCmd.AddCommand(newCmd)
- newCmd.Flags().StringVarP(&newMessageFile, "file", "F", "",
- "Take the message from the given file. Use - to read the message from the standard input",
+ newCmd.Flags().StringVarP(&newTitle, "title", "t", "",
+ "Provide a title to describe the issue",
)
newCmd.Flags().StringVarP(&newMessage, "message", "m", "",
"Provide a message to describe the issue",
)
+ newCmd.Flags().StringVarP(&newMessageFile, "file", "F", "",
+ "Take the message from the given file. Use - to read the message from the standard input",
+ )
}