Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace godirwalk with filepath.WalkDir #1058

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.6.0
github.com/google/go-jsonnet v0.20.0
github.com/karrick/godirwalk v1.17.0
github.com/pkg/errors v0.9.1
github.com/posener/complete v1.2.3
github.com/rs/zerolog v1.33.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+l
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/karrick/godirwalk v1.17.0 h1:b4kY7nqDdioR/6qnbHQyDvmA17u5G1cZ6J+CZXwSWoI=
github.com/karrick/godirwalk v1.17.0/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
40 changes: 18 additions & 22 deletions pkg/jsonnet/files.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package jsonnet

import (
"io/fs"
"os"
"path/filepath"

"github.com/gobwas/glob"
"github.com/karrick/godirwalk"
)

// FindFiles takes a file / directory and finds all Jsonnet files
Expand All @@ -21,31 +21,27 @@ func FindFiles(target string, excludes []glob.Glob) ([]string, error) {

var files []string

// godirwalk is faster than filepath.Walk, 'cause no os.Stat required
err = godirwalk.Walk(target, &godirwalk.Options{
Callback: func(rawPath string, de *godirwalk.Dirent) error {
// Normalize slashes for Windows
path := filepath.ToSlash(rawPath)
err = filepath.WalkDir(target, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
path = filepath.ToSlash(path)
if d.IsDir() {
return nil
}

if de.IsDir() {
// excluded?
for _, g := range excludes {
if g.Match(path) {
return nil
}
}

// excluded?
for _, g := range excludes {
if g.Match(path) {
return nil
}
}

// only .jsonnet or .libsonnet
if ext := filepath.Ext(path); ext == ".jsonnet" || ext == ".libsonnet" {
files = append(files, path)
}
return nil
},
// faster, no sort required
Unsorted: true,
// only .jsonnet or .libsonnet
if ext := filepath.Ext(path); ext == ".jsonnet" || ext == ".libsonnet" {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
Expand Down
Loading