-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
182 lines (148 loc) · 3.99 KB
/
command.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
package main
import (
"fmt"
"io"
)
type Command func(c CommandOptions) error
type CommandOptions struct {
target string // Often the repo name, but sometimes the group or prefix
args []string
Stdout io.Writer
Stderr io.Writer
}
var CommandNames = []string{
"clone",
"commit",
"pull",
"push",
"checkout",
"add",
"status",
"switch",
"sh",
"stash",
"register",
"unregister",
"make",
"group",
"prefix",
"list",
"reset",
}
var NameCommandMap = map[string]Command{
"clone": Clone,
"commit": Commit,
"pull": Pull,
"push": Push,
"checkout": Checkout,
"add": Add,
"status": Status,
"switch": Switch,
"sh": Sh,
"stash": Stash,
"reset": Reset,
}
var ErrUnknownCommand = fmt.Errorf("unknown command")
// CommandHasClIBasedTarget checks if the command expects a target from the CLI
// as opposed to targets from the active group
func CommandHasCLIBasedTarget(cmd string) bool {
return cmd == "clone" || cmd == "register" || cmd == "unregister" || cmd == "group" || cmd == "prefix"
}
// ResolveCommand resolves the command name to a command function
// or returns ErrUnknownCommand if the command is not found
func ResolveCommand(cmd string, config *Config) (Command, error) {
if c, ok := NameCommandMap[cmd]; ok {
return c, nil
}
switch cmd {
case "register":
return config.Register, nil
case "unregister":
return config.Unregister, nil
case "list":
return config.List, nil
case "make":
return config.Make, nil
case "group":
return config.SetGroup, nil
case "prefix":
return config.SetPrefix, nil
}
return nil, ErrUnknownCommand
}
// Clone runs the git clone command for the given repo
func Clone(c CommandOptions) error {
args := append([]string{"clone", c.target}, c.args...)
cmd := CreateCommand("git", args...)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}
func Commit(c CommandOptions) error {
args := append([]string{"commit"}, c.args...)
cmd := CreateCommand("git", args...)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
cmd.Dir = ResolveRepoPath(c.target)
return cmd.Run()
}
// Pull runs the git pull command for the given repo
func Pull(c CommandOptions) error {
args := append([]string{"pull"}, c.args...)
cmd := CreateCommand("git", args...)
cmd.Dir = ResolveRepoPath(c.target)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}
func Push(c CommandOptions) error {
args := append([]string{"push"}, c.args...)
cmd := CreateCommand("git", args...)
cmd.Dir = ResolveRepoPath(c.target)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}
func Checkout(c CommandOptions) error {
args := append([]string{"checkout"}, c.args...)
cmd := CreateCommand("git", args...)
cmd.Dir = ResolveRepoPath(c.target)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}
func Add(c CommandOptions) error {
args := append([]string{"add"}, c.args...)
cmd := CreateCommand("git", args...)
cmd.Dir = ResolveRepoPath(c.target)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}
func Status(c CommandOptions) error {
args := append([]string{"status"}, c.args...)
cmd := CreateCommand("git", args...)
cmd.Dir = ResolveRepoPath(c.target)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}
func Switch(c CommandOptions) error {
args := append([]string{"switch"}, c.args...)
cmd := CreateCommand("git", args...)
cmd.Dir = ResolveRepoPath(c.target)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}
func Stash(c CommandOptions) error {
args := append([]string{"stash"}, c.args...)
cmd := CreateCommand("git", args...)
cmd.Dir = ResolveRepoPath(c.target)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}
func Reset(c CommandOptions) error {
args := append([]string{"reset"}, c.args...)
cmd := CreateCommand("git", args...)
cmd.Dir = ResolveRepoPath(c.target)
return cmd.Run()
}
func Sh(c CommandOptions) error {
cmd := CreateCommand(c.args[0], c.args[1:]...)
cmd.Dir = ResolveRepoPath(c.target)
AddOutsToCommand(cmd, c.Stdout, c.Stderr)
return cmd.Run()
}