зеркало из https://github.com/mislav/hub.git
Refactor to use ExecCmd
This commit is contained in:
Родитель
b2c16c9b30
Коммит
5a67989a8b
12
exec_cmd.go
12
exec_cmd.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
|
@ -15,7 +16,7 @@ func (cmd *ExecCmd) WithArg(arg string) *ExecCmd {
|
|||
return cmd
|
||||
}
|
||||
|
||||
func (cmd *ExecCmd) Exec() (out string, err error) {
|
||||
func (cmd *ExecCmd) ExecOutput() (string, error) {
|
||||
output, err := exec.Command(cmd.Name, cmd.Args...).Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -24,6 +25,15 @@ func (cmd *ExecCmd) Exec() (out string, err error) {
|
|||
return string(output), nil
|
||||
}
|
||||
|
||||
func (cmd *ExecCmd) Exec() error {
|
||||
c := exec.Command(cmd.Name, cmd.Args...)
|
||||
c.Stdin = os.Stdin
|
||||
c.Stdout = os.Stdout
|
||||
c.Stderr = os.Stderr
|
||||
|
||||
return c.Run()
|
||||
}
|
||||
|
||||
func NewExecCmd(name string) *ExecCmd {
|
||||
return &ExecCmd{name, make([]string, 0)}
|
||||
}
|
||||
|
|
2
git.go
2
git.go
|
@ -50,7 +50,7 @@ func (g *Git) CommitLogs(sha1, sha2 string) string {
|
|||
shaRange := fmt.Sprintf("%s...%s", sha1, sha2)
|
||||
execCmd.WithArg(shaRange)
|
||||
|
||||
outputs, err := execCmd.Exec()
|
||||
outputs, err := execCmd.ExecOutput()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
@ -50,7 +49,7 @@ func pullRequest(cmd *Command, args []string) {
|
|||
}
|
||||
|
||||
editCmd := buildEditCommand(messageFile)
|
||||
err = execCmd(editCmd)
|
||||
err = editCmd.Exec()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -96,33 +95,23 @@ func writePullRequestChanges(messageFile, base, head string) error {
|
|||
|
||||
func getLocalBranch(branchName string) string {
|
||||
result := strings.Split(branchName, ":")
|
||||
|
||||
return result[len(result)-1]
|
||||
}
|
||||
|
||||
func buildEditCommand(messageFile string) []string {
|
||||
editCmd := make([]string, 0)
|
||||
func buildEditCommand(messageFile string) *ExecCmd {
|
||||
gitEditor := git.Editor()
|
||||
editCmd = append(editCmd, gitEditor)
|
||||
editCmd := NewExecCmd(gitEditor)
|
||||
r := regexp.MustCompile("^[mg]?vim$")
|
||||
if r.MatchString(gitEditor) {
|
||||
editCmd = append(editCmd, "-c")
|
||||
editCmd = append(editCmd, "set ft=gitcommit")
|
||||
editCmd.WithArg("-c")
|
||||
editCmd.WithArg("set ft=gitcommit")
|
||||
}
|
||||
editCmd = append(editCmd, messageFile)
|
||||
editCmd.WithArg(messageFile)
|
||||
|
||||
return editCmd
|
||||
}
|
||||
|
||||
func execCmd(command []string) error {
|
||||
cmd := exec.Command(command[0], command[1:]...)
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
err := cmd.Run()
|
||||
return err
|
||||
}
|
||||
|
||||
func readTitleAndBodyFromFile(messageFile string) (title, body string, err error) {
|
||||
f, err := os.Open(messageFile)
|
||||
defer f.Close()
|
||||
|
@ -131,6 +120,7 @@ func readTitleAndBodyFromFile(messageFile string) (title, body string, err error
|
|||
}
|
||||
|
||||
reader := bufio.NewReader(f)
|
||||
|
||||
return readTitleAndBody(reader)
|
||||
}
|
||||
|
||||
|
@ -170,5 +160,6 @@ func readln(r *bufio.Reader) (string, error) {
|
|||
line, isPrefix, err = r.ReadLine()
|
||||
ln = append(ln, line...)
|
||||
}
|
||||
|
||||
return string(ln), err
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче