-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
359 lines (326 loc) · 9.13 KB
/
commands.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package main
import (
"bytes"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/codegangsta/cli"
)
const (
GOPKG = repoType(iota)
HTTP
HTTPS
GIT
SSH
)
type repoType int
var (
getCommandDesc = `Clones a package into your GOPATH using the go tool chain,
creates a link between it and your workspace, and if possible updates
the repos remote origin to use SSH.`
getCommand = cli.Command{
Name: "get",
Usage: "'go get' a repo, and link it to your workspace",
Description: getCommandDesc,
Action: func(c *cli.Context) {
err := getCommandAction(c)
if err != nil {
exitStatus = FAIL
return
}
},
Flags: []cli.Flag{
cli.BoolFlag{"update, u", "Update existing code"},
cli.BoolFlag{"download-only, d", "Only download the code, don't install with the go toolchain (go build, go install)"},
cli.BoolFlag{"force, f", "Force updating and linking (Irreverseible)"},
cli.BoolFlag{"no-ssh", "Do not update the remote origin to use SSH"},
cli.StringFlag{"ssh-user", "<user>", "Set the user for the SSH url (Default: git)"},
},
}
cloneCommand = cli.Command{
Name: "clone",
Usage: "Clone the repo into your GOPATH",
Action: func(c *cli.Context) {
err := cloneCommandAction(c)
if err != nil {
exitStatus = FAIL
return
}
},
Flags: []cli.Flag{
cli.BoolFlag{"update, u", "Update existing code"},
cli.BoolFlag{"download-only, d", "Only download the code, don't install with the go toolchain (go build, go install)"},
},
}
linkCommand = cli.Command{
Name: "link",
Usage: "Create a link from the GOPATH/project to WORKSPACE/project",
Action: func(c *cli.Context) {
err := linkCommandAction(c)
if err != nil {
exitStatus = FAIL
return
}
},
Flags: []cli.Flag{
cli.BoolFlag{"update, u", "Update existing link"},
cli.BoolFlag{"force, f", "Force updating and linking (Irreverseible)"},
},
Subcommands: []cli.Command{
{
Name: "rm",
Usage: "Remove workspace link",
Action: func(c *cli.Context) {
err := rmLinkSubCommandAction(c)
if err != nil {
exitStatus = FAIL
return
}
},
},
},
}
updateRemoteCommand = cli.Command{
Name: "update-remote",
Usage: "Updates the git remote origin url to use SSH",
Action: func(c *cli.Context) {
err := updateRemoteCommandAction(c)
if err != nil {
exitStatus = FAIL
return
}
},
Flags: []cli.Flag{
cli.StringFlag{"user", "git", "Set the user for the SSH url (Default: git)"},
},
}
rmCommand = cli.Command{
Name: "rm",
Usage: "Removes both the link and the original project folder",
Action: func(c *cli.Context) {
err := rmCommandAction(c)
if err != nil {
exitStatus = FAIL
return
}
},
}
newCommand = cli.Command{
Name: "new",
Usage: "Creates a new project with, a GOPATH folder, symlink, and",
Action: func(c *cli.Context) {
err := newCommandAction(c)
if err != nil {
exitStatus = FAIL
return
}
},
}
makeCommand = cli.Command{
Name: "make",
Usage: "Make all the go binaries of a project",
Flags: []cli.Flag{
cli.StringFlag{"folder", "cmd", "Set which folder containes the collection of binary go files (Default: cmd)"},
cli.StringFlag{"file", "make.go", "Give the name of the make file to run or generate"},
cli.BoolFlag{"update, u", "Should update the make file"},
},
Action: func(c *cli.Context) {
err := makeCommandAction(c)
if err != nik {
exitStatus = FAIL
return
}
},
}
)
func getCommandAction(c *cli.Context) error {
err := cloneCommandAction(c)
if err != nil {
return err
}
err = linkCommandAction(c)
if err != nil {
return err
}
if !c.Bool("no-ssh") {
err = updateRemoteCommandAction(c)
if err != nil {
return err
}
}
return nil
}
func cloneCommandAction(c *cli.Context) error {
pkgpath := projectFromURL(c.Args().First())
log.Info("Getting package: %v", pkgpath)
args := []string{"get"}
if c.Bool("update") {
args = append(args, "-u")
}
if c.Bool("download-only") {
args = append(args, "-d")
}
args = append(args, pkgpath)
log.Debug(" ----> running go %v", concatWithSpace(args...))
err := pipeFromExec(os.Stdout, "go", args...)
if err != nil {
log.Error("Couldn't get package: %v", err)
return err
}
log.Debug(" ----> Successfully got package!")
return nil
}
func linkCommandAction(c *cli.Context) error {
pkgpath := projectFromURL(c.Args().First())
pkg := pkgFromPath(pkgpath)
workspacepath := concat(WORKSPACE, "/", pkg)
log.Info("Linking package %v to %v", pkgpath, workspacepath)
fullpkgpath := getAbsPath(concat(GOPATH, "/src/", pkgpath))
fullworkspacepath := getAbsPath(workspacepath)
// check if anything exists here
if _, err := os.Stat(fullworkspacepath); !os.IsNotExist(err) {
info, _ := os.Lstat(fullworkspacepath)
if info.Mode()&os.ModeSymlink != 0 {
if c.Bool("update") || c.Bool("force") {
os.Remove(fullworkspacepath)
} else {
log.Warning("[WARNING]: Link already exists!")
symlink, _ := filepath.EvalSymlinks(fullworkspacepath)
log.Warning(" ----> %v -> %v", workspacepath, symlink)
return errors.New("Link already exists")
}
} else if c.Bool("force") {
log.Warning(" ----> removing %v", workspacepath)
os.Remove(fullworkspacepath)
} else {
log.Error(" ----> [ERROR]: File/Folder already exists at %v, if you wish to proceed use -f", workspacepath)
return errors.New("File/Folder already exists")
}
}
cmd := exec.Command("ln", "-s", fullpkgpath, fullworkspacepath)
log.Debug(" ----> running: %v", concat("ln", " -s", concat(" $GOPATH/src/", pkgpath), concat(" ", WORKSPACE, "/", pkg)))
err := cmd.Run()
if err != nil {
log.Error("Failed to create link: %v", err)
return err
}
log.Debug(" ----> Successfully linked!")
return nil
}
func updateRemoteCommandAction(c *cli.Context) error {
path := c.Args().First()
pkgpath := projectFromURL(path)
fullpkgpath := getAbsPath(concat(GOPATH, "/src/", pkgpath))
log.Info("Update remote origin URL for repo: %v", pkgpath)
os.Chdir(fullpkgpath)
cmd := exec.Command("git", "remote", "-v")
log.Debug(" ----> running: git remote -v")
var buf bytes.Buffer
cmd.Stdout = &buf
err := cmd.Run()
if err != nil {
log.Error("Couldn't update remote url: %v", err)
return err
}
endpoints := strings.Split(buf.String(), "\n")
var repo string
for _, line := range endpoints {
url, err := parseGitOriginURL(line)
if err == nil {
repo = url
}
}
if repo == "" {
log.Error("Couldn't parse git remote origin url")
return errors.New("Couldn't parse git remote origin url")
}
user := c.String("ssh-user")
newRepoURL := getSSHPath(repo, user)
os.Chdir(fullpkgpath)
cmd = exec.Command("git", "remote", "set-url", "origin", newRepoURL)
log.Debug(" ----> running: git remote set-url origin %v", newRepoURL)
err = cmd.Run()
if err != nil {
log.Error("Failed to update remote origin: %v", err)
return err
}
log.Debug(" ----> Successfully updated remote origin")
return nil
}
func rmLinkSubCommandAction(c *cli.Context) error {
log.Info("Removing workspace link")
path := c.Args().First()
pkg := pkgFromPath(path)
workspacepath := getAbsPath(concat(WORKSPACE, "/", pkg))
cmd := exec.Command("rm", workspacepath)
log.Debug(" ----> running: rm %s", workspacepath)
err := cmd.Run()
if err != nil {
log.Error("Failed to remove workspace link")
return err
}
log.Debug(" ----> successfully removed workspace link")
return nil
}
func rmCommandAction(c *cli.Context) error {
if err := rmLinkSubCommandAction(c); err != nil {
log.Error("Failed to remove project")
return err
}
log.Info("Removing project")
path := c.Args().First()
fullpkgpath := getAbsPath(concat(GOPATH, "/src/", projectFromURL(path)))
cmd := exec.Command("rm", "-rf", fullpkgpath)
log.Debug(" ----> running rm -rf %s", fullpkgpath)
err := cmd.Run()
if err != nil {
log.Error("Failed to removed project folder")
return err
}
log.Debug(" ----> successfully removed project folder")
return nil
}
func newCommandAction(c *cli.Context) error {
log.Info("Creating new project")
path := c.Args().First()
pkgpath := projectFromURL(path)
fullpkgpath := getAbsPath(concat(GOPATH, "/src/", pkgpath))
cmd := exec.Command("mkdir", fullpkgpath)
log.Debug(" ----> running mkdir %s", fullpkgpath)
err := cmd.Run()
if err != nil {
log.Error("Failed to create project folder: %v", err)
return err
}
log.Debug(" ----> successfully created project folder")
// initialite git
log.Info("Initializing git repo")
os.Chdir(fullpkgpath)
cmd = exec.Command("git", "init")
log.Debug(" ----> running git init")
err = cmd.Run()
if err != nil {
log.Error("Failed to initialize git repo")
return err
}
log.Debug(" ----> successfully initialized git repo")
// add git remote origin
log.Info("Adding remote origin")
gitorigin := getSSHPath(path, "git")
cmd = exec.Command("git", "remote", "add", "origin", gitorigin)
log.Debug(" ----> running git remote add origin %v", gitorigin)
err = cmd.Run()
if err != nil {
log.Error("Failed to add remote origin")
return err
}
log.Debug(" ----> successfully added git remote origin")
err = linkCommandAction(c)
if err != nil {
return err
}
log.Debug(" ----> successfully created new project %v", path)
return nil
}