/
fs.go
94 lines (78 loc) · 1.91 KB
/
fs.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
package configuration
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
)
func getWorkingDirectory() string {
workingDirectory, err := os.Getwd()
if err != nil {
log.Fatal(fmt.Errorf("could not get working directory path: %s", err))
}
return workingDirectory
}
func isFile(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func findRootDirectory(filename string) string {
searchDirectoryPath := getWorkingDirectory()
// TODO: should stop search when at system root, for now just limit depth to 10
for depth := 0; depth <= 10; depth++ {
searchPath := path.Join(searchDirectoryPath, filename)
if isFile(searchPath) {
absolutePath, err := filepath.Abs(searchPath)
if err != nil {
log.Fatal(err)
}
directoryPath := strings.Replace(absolutePath, filename, "", 1)
return directoryPath
}
searchDirectoryPath += "../"
}
log.Fatal("Could not find parent directory with file")
// never gets here, stops lint error complaint
return ""
}
func findComponents(directory string, filename string, searchDepth int) []string {
var results []string
globPath := ""
for depth := 0; depth <= searchDepth; depth++ {
searchPath := path.Join(directory, globPath, filename)
matches, err := filepath.Glob(searchPath)
if err != nil {
log.Fatal(err)
}
for _, match := range matches {
absolutePath, err := filepath.Abs(match)
if err != nil {
log.Fatal(err)
}
absolutePath = strings.Replace(absolutePath, filename, "", 1)
results = append(results, absolutePath)
}
globPath += "*/"
}
return results
}
func isDirectory(directory string) bool {
info, err := os.Stat(directory)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
func areDirectories(directories []string) bool {
for _, directory := range directories {
if !isDirectory(directory) {
return false
}
}
return true
}