-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmd.go
249 lines (209 loc) · 4.84 KB
/
cmd.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"fmt"
"log"
"os"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var (
version = "0.3.4"
d *data
)
func main() {
app := cli.NewApp()
app.Name = "xp"
app.HelpName = "xp"
app.Version = version
app.Usage = "extreme programming made simple"
xpConfig, err := homedir.Expand("~/.xp")
if err != nil {
log.Fatal(err)
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Value: xpConfig,
Usage: "set the default configuration file",
},
}
app.Before = func(c *cli.Context) error {
cfg := c.String("config")
f, err := os.Open(cfg)
if err != nil {
if os.IsNotExist(err) {
d = new(data)
return nil
}
return errors.Wrapf(err, "could not open %s", cfg)
}
defer f.Close()
d, err = load(f)
if err != nil {
return errors.Wrap(err, "load failed")
}
return nil
}
app.After = func(c *cli.Context) error {
cfg := c.String("config")
f, err := os.OpenFile(cfg, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
return errors.Wrapf(err, "could not open %s for writing", cfg)
}
defer f.Close()
if err := d.store(f); err != nil {
return errors.Wrap(err, "store failed")
}
return nil
}
app.Commands = []cli.Command{
showConfigCommand,
addInfoCommand,
addDevCommand,
initCommand,
setDevsCommand,
// Below commands are deprecated.
devCommand,
repoCommand,
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
var showConfigCommand = cli.Command{
Name: "show-config",
Aliases: []string{"sc"},
Usage: "Print the current config",
Action: func(c *cli.Context) error {
fmt.Print(d)
return nil
},
}
var addInfoCommand = cli.Command{
Name: "add-info",
Usage: "Add xp info to the COMMIT msg file",
Description: "This is supposed to be invoked from inside a prepare-commit-msg hook",
ArgsUsage: "commit-msg-file",
Hidden: true,
Action: func(c *cli.Context) error {
wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "could not get wd")
}
msgFile := c.Args().Get(0)
if err := d.appendInfo(wd, msgFile); err != nil {
return errors.Wrap(err, "info add failed")
}
return nil
},
}
var addDevCommand = cli.Command{
Name: "add-dev",
Usage: "Add a new developer",
ArgsUsage: `id "name" email`,
Action: devAddAction,
}
var devCommand = cli.Command{
Name: "dev",
Aliases: []string{"d"},
Usage: "Dev management",
Hidden: true,
Subcommands: []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a new developer",
ArgsUsage: `id "name" email`,
Action: devAddAction,
},
},
}
var devAddAction = func(c *cli.Context) error {
args := c.Args()
id, name, email := args.Get(0), args.Get(1), args.Get(2)
if id == "" || name == "" || email == "" {
return errors.New("invalid id/name/email")
}
d.addDev(id, name, email)
return nil
}
var repoCommand = cli.Command{
Name: "repo",
Aliases: []string{"r"},
Usage: "Repo management",
Hidden: true,
Subcommands: []cli.Command{
initCommand,
repoDevsCommand,
},
}
var initCommand = cli.Command{
Name: "init",
Aliases: []string{"i"},
Usage: "Initialize a repo. Setup prepare-commit-msg hook",
ArgsUsage: ".",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "overwrite",
Usage: "overwrite the .git/hooks/prepare-commit-msg file",
},
cli.StringSliceFlag{
Name: "devs",
Usage: "initial set of devs (optional)",
},
cli.StringFlag{
Name: "story-id",
Usage: "story id (optional)",
},
},
Action: func(c *cli.Context) error {
args := c.Args()
dir := args.Get(0)
if dir == "." || dir == "" {
var err error
dir, err = os.Getwd()
if err != nil {
return errors.Wrap(err, "could not get wd")
}
}
xpBinPath, err := os.Executable()
if err != nil {
return errors.Wrap(err, "xp binary path resolve failed")
}
overwrite := c.Bool("overwrite")
if err := initRepo(dir, overwrite, xpBinPath); err != nil {
return errors.Wrap(err, "repo .git hook init failed")
}
devs := c.StringSlice("devs")
storyID := c.String("story-id")
if err := d.addRepo(dir, devs, storyID); err != nil {
return errors.Wrap(err, "could add init repo")
}
return nil
},
}
var setDevsCommand = cli.Command{
Name: "set-devs",
Usage: "Set list of devs working on the repo",
ArgsUsage: "dev1 dev2 dev3",
Action: repoDevsAction,
}
var repoDevsCommand = cli.Command{
Name: "devs",
Aliases: []string{"d", "dev"},
Usage: "Set list of devs working on repo",
ArgsUsage: "dev1 dev2 dev3",
Action: repoDevsAction,
}
var repoDevsAction = func(c *cli.Context) error {
wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "could not get wd")
}
devs := c.Args()
if err := d.updateRepoDevs(wd, devs); err != nil {
return errors.Wrap(err, "could not set devs")
}
return nil
}