/
dir.go
57 lines (47 loc) · 995 Bytes
/
dir.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
package dir
import (
"os"
"path/filepath"
"github.com/mitchellh/go-homedir"
)
const (
hookDir = "./.git/hooks/"
)
func FindFiles(file string) []string {
paths := make([]string, 0)
if home, err := homedir.Dir(); err == nil {
paths = append(paths, filepath.Join(home, file))
}
f, err := os.Stat(hookDir)
if (err == nil || os.IsExist(err)) && f.IsDir() {
// working dir is on project root
paths = append(paths, filepath.Join(hookDir, file))
} else {
// work around for test
paths = append(paths, file)
}
return paths
}
func FindFirstExist(file string) string {
if isFile(file) {
return file
}
hookFile := filepath.Join(hookDir, file)
if isFile(hookFile) {
return hookFile
}
if home, err := homedir.Dir(); err == nil {
homeFile := filepath.Join(home, file)
if isFile(homeFile) {
return homeFile
}
}
return ""
}
func isFile(path string) bool {
fi, err := os.Stat(path)
if err != nil && !os.IsExist(err) {
return false
}
return !fi.IsDir()
}