diff --git a/internal/config/state/cleaners.go b/internal/config/state/cleaners.go index 6705ec86..25162e17 100644 --- a/internal/config/state/cleaners.go +++ b/internal/config/state/cleaners.go @@ -3,7 +3,6 @@ package state import ( "log" "os" - "path/filepath" "github.com/vinegarhq/vinegar/internal/dirs" "github.com/vinegarhq/vinegar/util" @@ -19,7 +18,7 @@ func CleanPackages() error { return util.WalkDirExcluded(dirs.Downloads, pkgs, func(path string) error { log.Printf("Removing unused package %s", path) - return os.Remove(filepath.Join(dirs.Downloads, path)) + return os.Remove(path) }) } @@ -33,6 +32,6 @@ func CleanVersions() error { return util.WalkDirExcluded(dirs.Versions, vers, func(path string) error { log.Printf("Removing unused version directory %s", path) - return os.RemoveAll(filepath.Join(dirs.Versions, path)) + return os.RemoveAll(path) }) } diff --git a/util/paths.go b/util/paths.go index 74c7970a..5d0acf0a 100644 --- a/util/paths.go +++ b/util/paths.go @@ -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 -}