forked from evergreen-ci/evergreen
/
file.go
78 lines (70 loc) · 2.07 KB
/
file.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
package util
import (
"io"
"io/ioutil"
"os"
"path/filepath"
ignore "github.com/sabhiram/go-git-ignore"
)
// FileExists returns true if 'path' exists.
func FileExists(elem ...string) (bool, error) {
path := filepath.Join(elem...)
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// WriteToTempFile writes the given string to a temporary file and returns the
// path to the file.
func WriteToTempFile(data string) (string, error) {
dir := filepath.Join(os.TempDir(), "evergreen")
if err := os.MkdirAll(dir, 0755); err != nil {
return "", err
}
file, err := ioutil.TempFile(dir, "temp_file_")
if err != nil {
return "", err
}
defer file.Close()
if _, err = io.WriteString(file, data); err != nil {
return "", err
}
return file.Name(), nil
}
// fileListBuilder contains the information for building a list of files in the given directory.
// It adds the files to include in the fileNames array and uses the ignorer to determine if a given
// file matches and should be added.
type fileListBuilder struct {
fileNames []string
ignorer *ignore.GitIgnore
}
func (fb *fileListBuilder) walkFunc(path string, info os.FileInfo, err error) error {
if !info.IsDir() && fb.ignorer.MatchesPath(path) {
fb.fileNames = append(fb.fileNames, path)
}
return nil
}
// BuildFileList returns a list of files that match the given list of expressions
// rooted at the given startPath. The expressions correspond to gitignore ignore
// expressions: anything that would be matched - and therefore ignored by git - is included
// in the returned list of file paths. BuildFileList does not follow symlinks as
// it uses filpath.Walk, which does not follow symlinks.
func BuildFileList(startPath string, expressions ...string) ([]string, error) {
ignorer, err := ignore.CompileIgnoreLines(expressions...)
if err != nil {
return nil, err
}
fb := &fileListBuilder{
fileNames: []string{},
ignorer: ignorer,
}
err = filepath.Walk(startPath, fb.walkFunc)
if err != nil {
return nil, err
}
return fb.fileNames, nil
}