Skip to content

Commit 330816f

Browse files
authored
#5: remove hidden files from discovery (#16)
#5: remove hidden files from discovery
1 parent bddfc78 commit 330816f

File tree

5 files changed

+66
-9
lines changed

5 files changed

+66
-9
lines changed

pkg/action/discover.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ var actionsSubdir = strings.Join([]string{"", actionsDirname, ""}, string(filepa
4040

4141
func (ad *yamlDiscovery) isValid(path string, d fs.DirEntry) bool {
4242
i := strings.LastIndex(path, actionsSubdir)
43-
return !d.IsDir() &&
44-
i != -1 &&
45-
strings.Count(path[i+len(actionsSubdir):], string(filepath.Separator)) == 1 && // Nested actions are not allowed.
43+
44+
if d.IsDir() || i == -1 || isHidden(path) {
45+
return false
46+
}
47+
48+
return strings.Count(path[i+len(actionsSubdir):], string(filepath.Separator)) == 1 && // Nested actions are not allowed.
4649
ad.targetRgx.MatchString(d.Name())
4750
}
4851

@@ -52,11 +55,19 @@ func (ad *yamlDiscovery) findFiles() chan string {
5255
ch := make(chan string, 10)
5356
go func() {
5457
err := fs.WalkDir(ad.fs, ".", func(path string, d fs.DirEntry, err error) error {
55-
if err == nil && ad.isValid(path, d) {
58+
if err != nil {
59+
return err
60+
}
61+
62+
if d.IsDir() && isHidden(path) {
63+
return fs.SkipDir
64+
}
65+
66+
if ad.isValid(path, d) {
5667
ch <- path
5768
}
5869

59-
return err
70+
return nil
6071
})
6172

6273
if err != nil {

pkg/action/discover_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func Test_Discover_isValid(t *testing.T) {
8787
{"incorrect filename prefix", "1/2/actions/3/myaction.yaml", false}, // Incorrect prefix.
8888
{"incorrect filename suffix", "1/2/actions/3/action.yaml.bkp", false}, // Incorrect suffix.
8989
{"incorrect path", "1/2/3/action.yaml", false}, // File not inside an "actions" directory.
90+
{"incorrect hidden path", ".1/2/actions/3/action.yml", false}, // Invalid hidden directory
9091
{"nested action", "1/2/actions/3/4/5/action.yaml", false}, // There is a deeper nesting in actions directory.
9192
{"root action", "actions/verb/action.yaml", false}, // Actions are located in root.
9293
{"dir", "1/2/actions/3", false}, // A directory is given.

pkg/action/discover_unix.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package action
5+
6+
import (
7+
"path/filepath"
8+
)
9+
10+
func isHidden(path string) bool {
11+
pathList := filepath.SplitList(path)
12+
for _, v := range pathList {
13+
if len(v) > 1 && v[0:1] == "." {
14+
return true
15+
}
16+
}
17+
18+
return false
19+
}

pkg/action/discover_windows.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Package action provides implementations of discovering and running actions.
2+
package action
3+
4+
import (
5+
"path/filepath"
6+
"syscall"
7+
)
8+
9+
func isHidden(path string) bool {
10+
absPath, err := filepath.Abs(path)
11+
if err != nil {
12+
return false
13+
}
14+
15+
pointer, err := syscall.UTF16PtrFromString(`\\?\` + absPath)
16+
if err != nil {
17+
return false
18+
}
19+
20+
attributes, err := syscall.GetFileAttributes(pointer)
21+
if err != nil {
22+
return false
23+
}
24+
25+
return attributes&syscall.FILE_ATTRIBUTE_HIDDEN != 0
26+
}

pkg/driver/mocks/driver.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)