-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcmd.go
205 lines (176 loc) · 4.25 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
package main
import (
"fmt"
"log"
"os"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var version = "0.2.2"
func main() {
var d *data
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{
{
Name: "show-config",
Aliases: []string{"sc"},
Usage: "Print the current config",
Action: func(c *cli.Context) error {
fmt.Print(d)
return nil
},
},
{
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",
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
},
},
{
Name: "dev",
Aliases: []string{"d"},
Usage: "Dev management",
Subcommands: []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a new developer",
ArgsUsage: `id "name" email`,
Action: 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
},
},
},
},
{
Name: "repo",
Aliases: []string{"r"},
Usage: "Repo management",
Subcommands: []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 == "." {
var err error
dir, err = os.Getwd()
if err != nil {
return errors.Wrap(err, "could not get wd")
}
}
overwrite := c.Bool("overwrite")
if err := initRepo(dir, overwrite); 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
},
},
{
Name: "devs",
Aliases: []string{"d"},
Usage: "Set list of devs working on repo",
ArgsUsage: "dev1 dev2 dev3",
Action: 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
},
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}