-
Notifications
You must be signed in to change notification settings - Fork 27
/
cmd.go
76 lines (60 loc) · 2.04 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
package main
import (
"fmt"
"go/build"
)
var cmdCmd = &Command{
UsageLine: "cmd [project import path] [cmd import path]",
Short: "add a command to your project's GLOCKFILE",
Long: `cmd is used to record a Go command-line tool that the project depends on.
The cmd import path must reference a main package. Its dependencies will be
included in the GLOCKFILE along with the project's dependencies, and the command
will be built by "glock sync" and updated by "glock apply".
This functionality allows you to install and update development tools like "vet"
and "errcheck" across a team. It can even be used to automatically update glock
itself.
Commands are recorded at the top of your GLOCKFILE, in the following format:
cmd code.google.com/p/go.tools/cmd/godoc
cmd code.google.com/p/go.tools/cmd/goimports
cmd code.google.com/p/go.tools/cmd/vet
Dependencies of the commands will be included in the overall calculation and
included alongside your project's library dependencies within the GLOCKFILE.
Options:
-n print to stdout instead of writing to file.
`,
}
var cmdN = cmdCmd.Flag.Bool("n", false, "Don't save the file, just print to stdout")
func init() {
cmdCmd.Run = runCmd // break init loop
}
func runCmd(_ *Command, args []string) {
if len(args) != 2 {
cmdCmd.Usage()
return
}
var (
importPath = args[0]
cmd = args[1]
)
// Import the cmd, verify it's a main package, and build it.
pkg, err := build.Import(cmd, "", 0)
if err != nil {
perror(fmt.Errorf("Failed to import %v: %v", cmd, err))
}
if pkg.Name != "main" {
perror(fmt.Errorf("Found package %v, expected main", pkg.Name))
}
installOutput, err := run("go", "install", "-v", cmd)
if err != nil {
perror(fmt.Errorf("Failed to build %v:\n%v", cmd, string(installOutput)))
}
// Add new cmd to the list, recalculate dependencies, and write result
var (
cmds = append(readCmds(importPath), cmd)
depRoots = calcDepRoots(importPath, cmds)
output = glockfileWriter(importPath, *cmdN)
)
outputCmds(output, cmds)
outputDeps(output, depRoots)
output.Close()
}