-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
90 lines (77 loc) · 2.5 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
79
80
81
82
83
84
85
86
87
88
89
90
package utility
import (
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
ignore "github.com/sabhiram/go-git-ignore"
)
// FileExists provides a clearer interface for checking if a file
// exists.
func FileExists(path string) bool {
if path == "" {
return false
}
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// WriteRawFile writes a sequence of byes to a new file created at the
// specified path.
func WriteRawFile(path string, data []byte) error {
file, err := os.Create(path)
if err != nil {
return errors.Wrapf(err, "problem creating file '%s'", path)
}
if _, err := file.Write(data); err != nil {
return errors.Wrapf(err, "problem writing data to file '%s'", path)
}
if err = file.Close(); err != nil {
return errors.Wrapf(err, "problem closing file '%s' after successfully writing data", path)
}
return nil
}
// WriteFile provides a clearer interface for writing string data to a
// file.
func WriteFile(path string, data string) error {
return errors.WithStack(WriteRawFile(path, []byte(data)))
}
// 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
prefix string
}
func (fb *fileListBuilder) walkFunc(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrapf(err, "Error received by walkFunc for path %s", path)
}
path = strings.TrimPrefix(path, fb.prefix)
path = strings.TrimLeft(path, string(os.PathSeparator))
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,
prefix: startPath,
}
err = filepath.Walk(startPath, fb.walkFunc)
if err != nil {
return nil, err
}
return fb.fileNames, nil
}