-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathignore.go
47 lines (43 loc) · 1.05 KB
/
ignore.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
package main
import (
"bufio"
"log"
"os"
"os/user"
"path/filepath"
"github.com/viktomas/godu/files"
)
func readIgnoreFile() []string {
usr, err := user.Current()
if err != nil {
log.Println("Wasn't able to retrieve current user at runtime")
return []string{}
}
ignoreFileName := filepath.Join(usr.HomeDir, ".goduignore")
if _, err := os.Stat(ignoreFileName); os.IsNotExist(err) {
return []string{}
}
ignoreFile, err := os.Open(ignoreFileName)
if err != nil {
log.Printf("Failed to read ingorefile because %s\n", err.Error())
return []string{}
}
defer ignoreFile.Close()
scanner := bufio.NewScanner(ignoreFile)
lines := []string{}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines
}
func ignoreBasedOnIgnoreFile(ignoreFile []string) files.ShouldIgnoreFolder {
ignoredFolders := map[string]struct{}{}
for _, line := range ignoreFile {
ignoredFolders[line] = struct{}{}
}
return func(absolutePath string) bool {
_, name := filepath.Split(absolutePath)
_, ignored := ignoredFolders[name]
return ignored
}
}