-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
99 lines (85 loc) · 1.67 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
package fs
import (
"go/build"
"io"
"log"
"os"
"path/filepath"
"runtime"
)
func Exist(p string) bool {
_, err := os.Stat(p)
return err == nil || !os.IsNotExist(err)
}
func NotExist(p string) bool {
_, err := os.Stat(p)
return err != nil && os.IsNotExist(err)
}
func IsFile(p string) bool {
fi, _ := os.Stat(p)
return fi != nil && fi.Mode().IsRegular()
}
func IsDir(p string) bool {
fi, _ := os.Stat(p)
return fi != nil && fi.IsDir()
}
func IsEmptyDir(p string) (bool, error) {
f, err := os.Open(p)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
return true, nil
} else {
return false, err
}
}
func DetectDir(dir string, features ...string) string {
for {
if hasAllFeatures(dir, features) {
return dir
}
if parentDir := filepath.Dir(dir); parentDir != dir {
dir = parentDir
} else {
return ``
}
}
return ``
}
func hasAllFeatures(dir string, features []string) bool {
for _, feature := range features {
if matches, err := filepath.Glob(filepath.Join(dir, feature)); err != nil {
log.Panic(err)
} else if len(matches) == 0 {
return false
}
}
return true
}
func GoPath() string {
gopath := os.Getenv(`GOPATH`)
if gopath == `` {
gopath = build.Default.GOPATH
}
return gopath
}
func GoModPath() string {
return filepath.Join(GoPath(), `pkg`, `mod`)
}
func GoSrcPath() string {
return filepath.Join(GoPath(), `src`)
}
func SourceDir() string {
_, filename, _, _ := runtime.Caller(1)
return filepath.Dir(filename)
}
func SourceFile() string {
_, filename, _, _ := runtime.Caller(1)
return filepath.FromSlash(filename)
}