-
Notifications
You must be signed in to change notification settings - Fork 2
/
finder.go
153 lines (130 loc) · 3.37 KB
/
finder.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
package diff
import (
"github.com/VerifyTests/Verify.Go/utils"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
type diffFinder struct {
logger Logger
}
var finder = newDiffFinder()
// newDiffFinder creates a new diffFinder instance
func newDiffFinder() *diffFinder {
return &diffFinder{
logger: newLogger("finder"),
}
}
// TryFindExe tries to find the diff tool at all the specified paths
func (f *diffFinder) TryFindExe(paths []string) (exePath string, found bool) {
ps := f.unique(paths)
for _, p := range ps {
if r, found := f.TryFind(p); found {
return r, found
}
}
return "", false
}
// TryFind tries to find the diff tool at the specified paths
func (f *diffFinder) TryFind(path string) (result string, found bool) {
expanded := os.ExpandEnv(path)
if !strings.ContainsRune(expanded, '*') {
if utils.File.Exists(expanded) {
result = expanded
found = true
return
}
f.logger.Info("could not find file: %s", path)
return "", false
}
var filePart = filepath.Base(expanded)
var directoryPart = utils.File.GetDirectoryName(expanded)
var directories = f.getDirectories(directoryPart)
for _, dir := range directories {
if strings.ContainsRune(dir, '*') {
panic("wildcard in file part currently not supported.")
}
var filePath = filepath.Join(dir, filePart)
if utils.File.Exists(filePath) {
return filePath, true
}
}
f.logger.Info("could not find file: %s", path)
return "", false
}
func (f *diffFinder) unique(slice []string) []string {
keys := make(map[string]bool)
var list []string
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func (f *diffFinder) getDirectories(directory string) []string {
var expanded = os.ExpandEnv(directory)
if !strings.ContainsRune(expanded, '*') {
dirExists, _ := utils.File.FileOrDirectoryExists(directory)
if dirExists {
return []string{directory}
}
}
var segments = strings.Split(expanded, string(filepath.Separator))
var currentRoots = []string{segments[0] + string(filepath.Separator)}
for i, segment := range segments {
if i == 0 {
continue //skip the first
}
var newRoots = make([]string, 0)
for _, root := range currentRoots {
if strings.ContainsRune(segment, '*') {
matches, _ := f.getDirectoriesFromRoot(root, segment)
newRoots = append(newRoots, matches...)
} else {
var newRoot = filepath.Join(root, segment)
var dirExists, _ = utils.File.FileOrDirectoryExists(newRoot)
if dirExists {
newRoots = append(newRoots, newRoot)
}
}
}
if len(newRoots) == 0 {
return nil
}
currentRoots = newRoots
}
return currentRoots
}
type dirInfo struct {
name string
date time.Time
}
func (f *diffFinder) getDirectoriesFromRoot(root, segment string) ([]string, error) {
matches := make([]dirInfo, 0)
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if matched, _ := filepath.Match(segment, info.Name()); matched {
matches = append(matches, dirInfo{name: path, date: info.ModTime()})
}
}
return nil
})
if err != nil {
return nil, err
}
sort.Slice(matches, func(i, j int) bool {
return matches[i].date.Before(matches[j].date)
})
sorted := make([]string, 0)
for _, match := range matches {
sorted = append(sorted, match.name)
}
return sorted, nil
}