forked from mislav/hub
/
runner.go
186 lines (155 loc) · 3.23 KB
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package commands
import (
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"github.com/github/hub/cmd"
"github.com/github/hub/git"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
"github.com/kballard/go-shellquote"
flag "github.com/ogier/pflag"
)
type ExecError struct {
Err error
Ran bool
ExitCode int
}
func (execError *ExecError) Error() string {
return execError.Err.Error()
}
func newExecError(err error) ExecError {
exitCode := 0
ran := true
if err != nil {
exitCode = 1
switch e := err.(type) {
case *exec.ExitError:
if status, ok := e.Sys().(syscall.WaitStatus); ok {
exitCode = status.ExitStatus()
}
case *exec.Error:
ran = false
}
}
return ExecError{
Err: err,
Ran: ran,
ExitCode: exitCode,
}
}
type Runner struct {
commands map[string]*Command
execute func([]*cmd.Cmd, bool) error
}
func NewRunner() *Runner {
return &Runner{
commands: make(map[string]*Command),
execute: executeCommands,
}
}
func (r *Runner) All() map[string]*Command {
return r.commands
}
func (r *Runner) Use(command *Command, aliases ...string) {
r.commands[command.Name()] = command
if len(aliases) > 0 {
r.commands[aliases[0]] = command
}
}
func (r *Runner) Lookup(name string) *Command {
return r.commands[name]
}
func (r *Runner) Execute() ExecError {
args := NewArgs(os.Args[1:])
forceFail := false
if args.Command == "" {
args.Command = "help"
forceFail = true
}
updater := NewUpdater()
err := updater.PromptForUpdate()
utils.Check(err)
git.GlobalFlags = args.GlobalFlags // preserve git global flags
expandAlias(args)
cmd := r.Lookup(args.Command)
if cmd != nil && cmd.Runnable() {
execErr := r.Call(cmd, args)
if execErr.ExitCode == 0 && forceFail {
execErr = newExecError(fmt.Errorf(""))
}
return execErr
}
err = git.Run(args.Command, args.Params...)
return newExecError(err)
}
func (r *Runner) Call(cmd *Command, args *Args) ExecError {
err := cmd.Call(args)
if err != nil {
if err == flag.ErrHelp {
err = nil
}
return newExecError(err)
}
cmds := args.Commands()
if args.Noop {
printCommands(cmds)
} else {
err = r.execute(cmds, len(args.Callbacks) == 0)
}
if err == nil {
for _, fn := range args.Callbacks {
err = fn()
if err != nil {
break
}
}
}
return newExecError(err)
}
func printCommands(cmds []*cmd.Cmd) {
for _, c := range cmds {
ui.Println(c)
}
}
func executeCommands(cmds []*cmd.Cmd, execFinal bool) error {
for i, c := range cmds {
var err error
// Run with `Exec` for the last command in chain
if execFinal && i == len(cmds)-1 {
err = c.Run()
} else {
err = c.Spawn()
}
if err != nil {
return err
}
}
return nil
}
func expandAlias(args *Args) {
cmd := args.Command
expandedCmd, err := git.Alias(cmd)
if err == nil && expandedCmd != "" {
words, e := splitAliasCmd(expandedCmd)
if e == nil {
args.Command = words[0]
args.PrependParams(words[1:]...)
}
}
}
func splitAliasCmd(cmd string) ([]string, error) {
if cmd == "" {
return nil, fmt.Errorf("alias can't be empty")
}
if strings.HasPrefix(cmd, "!") {
return nil, fmt.Errorf("alias starting with ! can't be split")
}
words, err := shellquote.Split(cmd)
if err != nil {
return nil, err
}
return words, nil
}