-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
74 lines (67 loc) · 1.42 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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
)
var knownLocations = []string{
"CODEOWNERS",
"docs/CODEOWNERS",
".github/CODEOWNERS",
}
const fileSizeLimit int64 = 3 * 1024 * 1024
func FindRepositoryRoot(d string) (string, error) {
command := exec.Command("git", "rev-parse", "--show-toplevel")
err := command.Run()
if err != nil {
return "", err
}
output, err := command.Output()
if err != nil {
return "", err
}
return string(output), nil
}
func FindCodeownersFile(d string) string {
for _, knownLocation := range knownLocations {
p := filepath.Join(d, knownLocation)
_, err := os.Stat(p)
if err != nil {
continue
}
return p
}
return ""
}
func ValidateCodeownersFile(f string) ([]Codeowner, error) {
fileInfo, err := os.Stat(f)
if err != nil {
return nil, err
}
if fileInfo.Size() > fileSizeLimit {
fmt.Fprint(os.Stderr, "CODEOWNERS is larger than 3 MB")
}
file, err := os.Open(f)
if err != nil {
return nil, err
}
parser := NewBufferedCodeownersParser(bufio.NewReader(file))
codeowners, parsingErrors, err := ReadAll(parser)
if err != nil {
return nil, err
}
for _, parsingError := range parsingErrors {
fmt.Fprintf(os.Stderr, "%v\n", parsingError.Error())
}
return codeowners, nil
}
func FindFile(d string, f string) (bool, bool) {
fp := filepath.Join(d, f)
fileInfo, err := os.Stat(fp)
if err != nil {
return false, false
}
return true, fileInfo.IsDir()
}