Skip to content

Commit 6cb80cb

Browse files
committed
Merge branch 'use_spawn'
2 parents c9f68e3 + 23cf927 commit 6cb80cb

File tree

6 files changed

+43
-57
lines changed

6 files changed

+43
-57
lines changed

Diff for: cmd/cmd.go

-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"os"
66
"os/exec"
77
"strings"
8-
"syscall"
98
)
109

1110
type Cmd struct {
@@ -53,20 +52,6 @@ func (cmd *Cmd) Exec() error {
5352
return c.Run()
5453
}
5554

56-
func (cmd *Cmd) SysExec() error {
57-
binary, lookErr := exec.LookPath(cmd.Name)
58-
if lookErr != nil {
59-
return fmt.Errorf("command not found: %s", cmd.Name)
60-
}
61-
62-
args := []string{cmd.Name}
63-
args = append(args, cmd.Args...)
64-
65-
env := os.Environ()
66-
67-
return syscall.Exec(binary, args, env)
68-
}
69-
7055
func New(name string) *Cmd {
7156
return &Cmd{Name: name, Args: make([]string, 0)}
7257
}

Diff for: commands/browse.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ func browse(command *Command, args *Args) {
6464
utils.Check(err)
6565
}
6666

67-
args.Replace(launcher[0], "", launcher[1:]...)
67+
args.Replace(launcher[0], "", launcher[1:]...)
6868
args.AppendParams(url)
6969
}

Diff for: commands/runner.go

+36-19
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,38 @@ import (
66
"github.com/jingweno/gh/cmd"
77
"github.com/jingweno/gh/git"
88
shellquote "github.com/kballard/go-shellquote"
9+
"os/exec"
10+
"syscall"
911
)
1012

13+
type ExecError struct {
14+
Err error
15+
ExitCode int
16+
}
17+
18+
func (execError *ExecError) Error() string {
19+
return execError.Err.Error()
20+
}
21+
22+
func newExecError(err error) ExecError {
23+
exitCode := 0
24+
if err != nil {
25+
exitCode = 1
26+
if exitError, ok := err.(*exec.ExitError); ok {
27+
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
28+
exitCode = status.ExitStatus()
29+
}
30+
}
31+
}
32+
33+
return ExecError{Err: err, ExitCode: exitCode}
34+
}
35+
1136
type Runner struct {
1237
Args []string
1338
}
1439

15-
func (r *Runner) Execute() error {
40+
func (r *Runner) Execute() ExecError {
1641
args := NewArgs(r.Args)
1742
if args.Command == "" {
1843
usage()
@@ -29,9 +54,9 @@ func (r *Runner) Execute() error {
2954
}
3055
if err := cmd.Flag.Parse(args.Params); err != nil {
3156
if err == flag.ErrHelp {
32-
return nil
57+
return newExecError(nil)
3358
} else {
34-
return err
59+
return newExecError(err)
3560
}
3661
}
3762

@@ -41,25 +66,24 @@ func (r *Runner) Execute() error {
4166
cmd.Run(cmd, args)
4267

4368
cmds := args.Commands()
69+
var err error
4470
if args.Noop {
4571
printCommands(cmds)
4672
} else {
47-
err := executeCommands(cmds)
48-
if err != nil {
49-
return err
50-
}
73+
err = executeCommands(cmds)
5174
}
5275

53-
return nil
76+
return newExecError(err)
5477
}
5578
}
5679

57-
return git.SysExec(args.Command, args.Params...)
80+
err := git.Spawn(args.Command, args.Params...)
81+
return newExecError(err)
5882
}
5983

6084
func slurpGlobalFlags(args *Args) {
6185
for i, p := range args.Params {
62-
if p == "--no-op" {
86+
if p == "--noop" {
6387
args.Noop = true
6488
args.RemoveParam(i)
6589
}
@@ -73,15 +97,8 @@ func printCommands(cmds []*cmd.Cmd) {
7397
}
7498

7599
func executeCommands(cmds []*cmd.Cmd) error {
76-
length := len(cmds)
77-
for i, c := range cmds {
78-
var err error
79-
if i == (length - 1) {
80-
err = c.SysExec()
81-
} else {
82-
err = c.Exec()
83-
}
84-
100+
for _, c := range cmds {
101+
err := c.Exec()
85102
if err != nil {
86103
return err
87104
}

Diff for: git/git.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -116,29 +116,14 @@ func Config(name string) (string, error) {
116116
return output[0], nil
117117
}
118118

119-
func SysExec(command string, args ...string) error {
120-
cmd := cmd.New("git")
121-
cmd.WithArg(command)
122-
for _, a := range args {
123-
cmd.WithArg(a)
124-
}
125-
126-
return cmd.SysExec()
127-
}
128-
129119
func Spawn(command string, args ...string) error {
130120
cmd := cmd.New("git")
131121
cmd.WithArg(command)
132122
for _, a := range args {
133123
cmd.WithArg(a)
134124
}
135125

136-
out, err := cmd.ExecOutput()
137-
if err != nil {
138-
return errors.New(out)
139-
}
140-
141-
return nil
126+
return cmd.Exec()
142127
}
143128

144129
func execGitCmd(input ...string) (outputs []string, err error) {

Diff for: github/github.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (gh *GitHub) CreateRepository(project Project, description, homepage string
5959

6060
func (gh *GitHub) CreatePullRequestForIssue(base, head, issue string) (string, error) {
6161
client := gh.client()
62-
params := octokat.PullRequestForIssueParams{Base: base, Head: head, Issue: issue}
62+
params := octokat.PullRequestForIssueParams{Base: base, Head: head, Issue: issue}
6363
pullRequest, err := client.CreatePullRequestForIssue(gh.repo(), params)
6464
if err != nil {
6565
return "", err
@@ -85,14 +85,14 @@ func (gh *GitHub) CiStatus(sha string) (*octokat.Status, error) {
8585
func (gh *GitHub) ForkRepository(name, owner string, noRemote bool) (repo *octokat.Repository, err error) {
8686
client := gh.client()
8787
config := gh.Config
88-
repo, err = client.Repository(octokat.Repo{Name: name, UserName: config.User})
88+
repo, err = client.Repository(octokat.Repo{Name: name, UserName: config.User})
8989
if repo != nil && err == nil {
9090
msg := fmt.Sprintf("Error creating fork: %s exists on %s", repo.FullName, GitHubHost)
9191
err = errors.New(msg)
9292
return
9393
}
9494

95-
repo, err = client.Fork(octokat.Repo{Name: name, UserName: owner}, nil)
95+
repo, err = client.Fork(octokat.Repo{Name: name, UserName: owner}, nil)
9696

9797
return
9898
}
@@ -109,7 +109,7 @@ func (gh *GitHub) ExpandRemoteUrl(owner, name string, isSSH bool) (url string) {
109109

110110
func (gh *GitHub) repo() octokat.Repo {
111111
project := gh.Project
112-
return octokat.Repo{Name: project.Name, UserName: project.Owner}
112+
return octokat.Repo{Name: project.Name, UserName: project.Owner}
113113
}
114114

115115
func findOrCreateToken(user, password string) (string, error) {

Diff for: main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ package main
22

33
import (
44
"github.com/jingweno/gh/commands"
5-
"github.com/jingweno/gh/utils"
65
"os"
76
)
87

98
func main() {
109
runner := commands.Runner{Args: os.Args[1:]}
1110
err := runner.Execute()
12-
utils.Check(err)
11+
os.Exit(err.ExitCode)
1312
}

0 commit comments

Comments
 (0)