-
Notifications
You must be signed in to change notification settings - Fork 27
/
apply.go
101 lines (90 loc) · 2.37 KB
/
apply.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
package main
import (
"bufio"
"fmt"
"go/build"
"os"
"path"
"path/filepath"
"strings"
)
var cmdApply = &Command{
UsageLine: "apply [import path]",
Short: "apply the changes described by a GLOCKFILE diff (on STDIN) to the current GOPATH.",
Long: `apply the changes described by a GLOCKFILE diff (on STDIN) to the current GOPATH.
It is meant to be called from a VCS hook on any change to the GLOCKFILE.
`,
}
func init() {
cmdApply.Run = runApply // break init loop
}
var actionstr = map[action]string{
add: "add ",
update: "update ",
remove: "remove ",
}
func runApply(cmd *Command, args []string) {
if len(args) == 0 {
fmt.Fprintf(os.Stderr, "error: no import path provided\n")
cmdApply.Usage()
return
}
var importPath = args[0]
var gopath = filepath.SplitList(build.Default.GOPATH)[0]
var book = buildPlaybook(readDiffLines(os.Stdin))
var updated = false
for _, cmd := range book.library {
fmt.Printf("%s %-50.49s %s\n", actionstr[cmd.action], cmd.importPath, cmd.revision)
var importDir = path.Join(gopath, "src", cmd.importPath)
switch cmd.action {
case remove:
// do nothing
case update:
updated = true
fallthrough
case add:
// add or update the dependency
run("go", "get", "-u", "-d", path.Join(cmd.importPath, "..."))
// update that dependency
var repo, err = glockRepoRootForImportPath(cmd.importPath)
if err != nil {
fmt.Println("error determining repo root for", cmd.importPath, err)
continue
}
err = repo.vcs.run(importDir, repo.vcs.tagSyncCmd, "tag", cmd.revision)
if err != nil {
fmt.Println("error syncing", cmd.importPath, "to", cmd.revision, "-", err)
continue
}
}
}
// Collect the import paths for all added commands.
var cmds []string
for _, cmd := range book.cmd {
if cmd.add {
cmds = append(cmds, cmd.importPath)
}
}
// If a package was updated, reinstall all commands.
if updated {
cmds = nil
var glockfile = glockfileReader(importPath, false)
var scanner = bufio.NewScanner(glockfile)
for scanner.Scan() {
var txt = scanner.Text()
if strings.HasPrefix(txt, "cmd ") {
cmds = append(cmds, txt[4:])
}
}
if err := scanner.Err(); err != nil {
perror(err)
}
}
for _, cmd := range cmds {
fmt.Println("install", cmd)
installOutput, err := run("go", "install", cmd)
if err != nil {
fmt.Println("failed:\n", string(installOutput), err)
}
}
}