-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (158 loc) · 4.54 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
)
const GO_PATH_ENV_NAME = "GOPATH"
const GO_15_VENDOR_EXPERIMENT = "GO15VENDOREXPERIMENT"
func main() {
fmt.Println("Vendoring packages...")
// Check that we received the expected format
var args = os.Args[1:]
if os.Getenv(GO_15_VENDOR_EXPERIMENT) != "1" {
fmt.Println("The gv command expects the", GO_15_VENDOR_EXPERIMENT, "environment variable to be set to", 1)
os.Exit(1)
} else if len(args) == 0 {
fmt.Println("The gv command expects the format of 'go get'.")
os.Exit(1)
} else if args[0] != "get" {
fmt.Println("The only command currently supported is 'get'.")
os.Exit(1)
}
// Insert -d flag after go get. This instructs get to stop after downloading the package
args = append(args[:1], append([]string{"-d"}, args[1:]...)...)
// Set PATH to the current working directory
path, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Setenv(GO_PATH_ENV_NAME, path)
// Set up some path vars
vendorPath := filepath.Join(path, "vendor")
srcPath := filepath.Join(path, "src")
// Instantiate our 'go get' command
goGetCommand := exec.Command("go", args...)
goGetCommand.Stdin = os.Stdin
goGetCommand.Stdout = os.Stdout
goGetCommand.Stderr = os.Stderr
// Establish our exit channels
exit := make(chan bool)
success := make(chan bool)
// Run the primary routine
go func() {
// Run the 'go get' command and rename src to vendor
if err = goGetCommand.Run(); err == nil {
if err = mergeVendors(srcPath, vendorPath); err == nil {
success <- true
return
}
}
//Clean up if there was an error
fmt.Println(err)
fmt.Println("Cleaning up...")
if err = os.RemoveAll(srcPath); err != nil {
fmt.Println(err)
}
exit <- true
}()
// Listen for interrupts, and if received, cancel 'go get'
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
go func() {
<-interrupt
fmt.Println("\nCancelling...")
if err = goGetCommand.Process.Kill(); err != nil {
fmt.Println("Unable to kill running process:", err)
exit <- true
}
}()
// Listen for exits
select {
case <-success:
fmt.Println("Done.")
os.Exit(0)
case <-exit:
fmt.Println("Unable to vendor packages.")
os.Exit(1)
}
}
func fileExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return false
}
return true
}
func mergeVendors(src string, dst string) error {
// Loop through all orgs in the src directory
domains, _ := ioutil.ReadDir(src)
for _, domain := range domains {
if domain.IsDir() {
srcDomainPath := filepath.Join(src, domain.Name())
dstDomainPath := filepath.Join(dst, domain.Name())
// Ensure that the dst domain folder exists
if !fileExists(dstDomainPath) {
if err := os.MkdirAll(dstDomainPath, 0700); err != nil {
return err
}
}
// Loop through the orgs for this domain
orgs, _ := ioutil.ReadDir(srcDomainPath)
for _, org := range orgs {
srcOrgPath := filepath.Join(srcDomainPath, org.Name())
dstOrgPath := filepath.Join(dstDomainPath, org.Name())
// Ensure that the dst domain folder exists
if !fileExists(dstOrgPath) {
if err := os.MkdirAll(dstOrgPath, 0700); err != nil {
return err
}
}
// Loop through the repos for this org
repos, _ := ioutil.ReadDir(srcOrgPath)
for _, repo := range repos {
srcRepoPath := filepath.Join(srcOrgPath, repo.Name())
dstRepoPath := filepath.Join(dstOrgPath, repo.Name())
// Overwrite (Remove) any content that exists at dstRepoPath
if fileExists(dstRepoPath) {
if err := os.RemoveAll(dstRepoPath); err != nil {
return err
}
}
// Copy each repo to corresponding dst directory
if err := os.Rename(srcRepoPath, dstRepoPath); err != nil {
return err
}
// Remove version control from dstRepoPath
// Remove .git and .hg folders from dst directory
gitRepo := filepath.Join(dstRepoPath, ".git")
gitIgnore := filepath.Join(dstRepoPath, ".gitignore")
hgRepo := filepath.Join(dstRepoPath, ".hg")
if fileExists(gitRepo) {
err := os.RemoveAll(gitRepo)
if err != nil {
fmt.Println("gv:", err)
}
os.Remove(gitIgnore)
} else if fileExists(hgRepo) {
err := os.RemoveAll(hgRepo)
if err != nil {
fmt.Println("gv:", err)
}
}
vendorPath := filepath.Join(domain.Name(), org.Name(), repo.Name())
fmt.Println("Vendored: ", vendorPath)
}
}
}
}
// Remove the src directory
if err := os.RemoveAll(src); err != nil {
return err
}
return nil
}