-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: use slices for walkdirexcluded
- Loading branch information
1 parent
1463d85
commit 0b43ad5
Showing
2 changed files
with
8 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,28 @@ | ||
package util | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
"slices" | ||
) | ||
|
||
// WalkDirExcluded will walk the file tree located at dir, calling | ||
// onExcluded for every file or directory that does not have a name in included. | ||
func WalkDirExcluded(dir string, included []string, onExcluded func(string) error) error { | ||
files, err := os.ReadDir(dir) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
find: | ||
for _, file := range files { | ||
for _, inc := range included { | ||
if file.Name() == inc { | ||
continue find | ||
} | ||
if slices.Contains(included, file.Name()) { | ||
continue | ||
} | ||
|
||
if err := onExcluded(file.Name()); err != nil { | ||
if err := onExcluded(filepath.Join(dir, file.Name())); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func FindTimeFile(dir string, comparison *time.Time) (string, error) { | ||
var name string | ||
|
||
err := filepath.Walk(dir, func(p string, i fs.FileInfo, err error) error { | ||
if i.ModTime().After(*comparison) { | ||
name = p | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if name == "" { | ||
return "", os.ErrNotExist | ||
} | ||
|
||
return name, nil | ||
} |