-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
runner.go
162 lines (133 loc) · 2.86 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
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
ExitCode int
}
func (execError *ExecError) Error() string {
return execError.Err.Error()
}
func newExecError(err error) ExecError {
exitCode := 0
if err != nil {
exitCode = 1
if exitError, ok := err.(*exec.ExitError); ok {
if status, ok := exitError.Sys().(syscall.WaitStatus); ok {
exitCode = status.ExitStatus()
}
}
}
return ExecError{Err: err, ExitCode: exitCode}
}
type Runner struct {
commands map[string]*Command
execute func(cmds []*cmd.Cmd) 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:])
if args.Command == "" {
printUsage()
return newExecError(nil)
}
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() {
return r.Call(cmd, args)
}
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)
}
return newExecError(err)
}
func printCommands(cmds []*cmd.Cmd) {
for _, c := range cmds {
ui.Println(c)
}
}
func executeCommands(cmds []*cmd.Cmd) error {
for i, c := range cmds {
var err error
// Run with `Exec` for the last command in chain
if 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
}