forked from Masterminds/glide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
303 lines (251 loc) · 6.33 KB
/
path.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
// Package path contains path and environment utilities for Glide.
//
// This includes tools to find and manipulate Go path variables, as well as
// tools for copying from one path to another.
package path
import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"strings"
)
// DefaultGlideFile is the default name for the glide.yaml file.
const DefaultGlideFile = "glide.yaml"
// VendorDir is the name of the directory that holds vendored dependencies.
//
// As of Go 1.5, this is always vendor.
var VendorDir = "vendor"
// Tmp is the temporary directory Glide should use. Defaults to "" which
// signals using the system default.
var Tmp = ""
// Cache the location of the homedirectory.
var homeDir = ""
// GlideFile is the name of the Glide file.
//
// Setting this is not concurrency safe. For consistency, it should really
// only be set once, at startup, or not at all.
var GlideFile = DefaultGlideFile
// LockFile is the default name for the lock file.
const LockFile = "glide.lock"
// Home returns the Glide home directory ($GLIDE_HOME or ~/.glide, typically).
//
// This normalizes to an absolute path, and passes through os.ExpandEnv.
func Home() string {
if homeDir != "" {
return homeDir
}
// Initialize the default user.
u, err := user.Current()
if err == nil && u.HomeDir != "" {
homeDir = filepath.Join(u.HomeDir, ".glide")
} else {
cwd, err := os.Getwd()
if err == nil {
homeDir = filepath.Join(cwd, ".glide")
} else {
homeDir = ".glide"
}
}
return homeDir
}
// SetHome sets the home directory for Glide.
func SetHome(h string) {
homeDir = h
}
// Vendor calculates the path to the vendor directory.
//
// Based on working directory, VendorDir and GlideFile, this attempts to
// guess the location of the vendor directory.
func Vendor() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
// Find the directory that contains glide.yaml
yamldir, err := GlideWD(cwd)
if err != nil {
return cwd, err
}
gopath := filepath.Join(yamldir, VendorDir)
// Resolve symlinks
info, err := os.Lstat(gopath)
if err != nil {
return gopath, nil
}
for i := 0; IsLink(info) && i < 255; i++ {
p, err := os.Readlink(gopath)
if err != nil {
return gopath, nil
}
if filepath.IsAbs(p) {
gopath = p
} else {
gopath = filepath.Join(filepath.Dir(gopath), p)
}
info, err = os.Lstat(gopath)
if err != nil {
return gopath, nil
}
}
return gopath, nil
}
// Glide gets the path to the closest glide file.
func Glide() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
// Find the directory that contains glide.yaml
yamldir, err := GlideWD(cwd)
if err != nil {
return cwd, err
}
gf := filepath.Join(yamldir, GlideFile)
return gf, nil
}
// GlideWD finds the working directory of the glide.yaml file, starting at dir.
//
// If the glide file is not found in the current directory, it recurses up
// a directory.
func GlideWD(dir string) (string, error) {
fullpath := filepath.Join(dir, GlideFile)
if _, err := os.Stat(fullpath); err == nil {
return dir, nil
}
base := filepath.Dir(dir)
if base == dir {
return "", fmt.Errorf("Cannot resolve parent of %s", base)
}
return GlideWD(base)
}
// Gopath gets GOPATH from environment and return the most relevant path.
//
// A GOPATH can contain a colon-separated list of paths. This retrieves the
// GOPATH and returns only the FIRST ("most relevant") path.
//
// This should be used carefully. If, for example, you are looking for a package,
// you may be better off using Gopaths.
func Gopath() string {
gopaths := Gopaths()
if len(gopaths) == 0 {
return ""
}
return gopaths[0]
}
// Gopaths retrieves the Gopath as a list when there is more than one path
// listed in the Gopath.
func Gopaths() []string {
p := os.Getenv("GOPATH")
p = strings.Trim(p, string(filepath.ListSeparator))
return filepath.SplitList(p)
}
// Basepath returns the current working directory.
//
// If there is an error getting the working directory, this returns ".", which
// should function in cases where the directory is unlinked... Then again,
// maybe not.
func Basepath() string {
base, err := os.Getwd()
if err != nil {
return "."
}
return base
}
// StripBasepath removes the base directory from a passed in path.
func StripBasepath(p string) string {
bp := Basepath()
return strings.TrimPrefix(p, bp+string(os.PathSeparator))
}
// IsLink returns true if the given FileInfo references a link.
func IsLink(fi os.FileInfo) bool {
return fi.Mode()&os.ModeSymlink == os.ModeSymlink
}
// HasLock returns true if this can stat a lockfile at the givin location.
func HasLock(basepath string) bool {
_, err := os.Stat(filepath.Join(basepath, LockFile))
return err == nil
}
// IsDirectoryEmpty checks if a directory is empty.
func IsDirectoryEmpty(dir string) (bool, error) {
f, err := os.Open(dir)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err
}
// CopyDir copies an entire source directory to the dest directory.
//
// This is akin to `cp -a src/* dest/`
//
// We copy the directory here rather than jumping out to a shell so we can
// support multiple operating systems.
func CopyDir(source string, dest string) error {
// get properties of source dir
si, err := os.Stat(source)
if err != nil {
return err
}
err = os.MkdirAll(dest, si.Mode())
if err != nil {
return err
}
d, err := os.Open(source)
if err != nil {
return err
}
defer d.Close()
objects, err := d.Readdir(-1)
for _, obj := range objects {
sp := filepath.Join(source, "/", obj.Name())
dp := filepath.Join(dest, "/", obj.Name())
if obj.IsDir() {
err = CopyDir(sp, dp)
if err != nil {
return err
}
} else {
// perform copy
err = CopyFile(sp, dp)
if err != nil {
return err
}
}
}
return nil
}
// CopyFile copies a source file to a destination.
//
// It follows symbolic links and retains modes.
func CopyFile(source string, dest string) error {
ln, err := os.Readlink(source)
if err == nil {
return os.Symlink(ln, dest)
}
s, err := os.Open(source)
if err != nil {
return err
}
defer s.Close()
d, err := os.Create(dest)
if err != nil {
return err
}
defer d.Close()
_, err = io.Copy(d, s)
if err != nil {
return err
}
si, err := os.Stat(source)
if err != nil {
return err
}
err = os.Chmod(dest, si.Mode())
return err
}