diff --git a/cmd/scrubber/example.config.toml b/cmd/scrubber/example.config.toml index 2bbed2e..1dcc99a 100644 --- a/cmd/scrubber/example.config.toml +++ b/cmd/scrubber/example.config.toml @@ -2,12 +2,11 @@ title = "Log scrubber example config" [[directory]] name = "Apache Logs" -path = "/tmp/files/*/*/*" -exclude = ["zip"] +path = "/var/log/apache" keep_latest = 10 [[directory.strategy]] - type = "size" + type = "age" action = "delete" - limit = "1b" + limit = "30d" diff --git a/cmd/scrubber/main.go b/cmd/scrubber/main.go index 66f4f81..cc24d7a 100644 --- a/cmd/scrubber/main.go +++ b/cmd/scrubber/main.go @@ -29,7 +29,7 @@ func main() { fs := scrubber.OSFilesystem{} s := scrubber.New(&conf, fs, logger, *pretend) - _, err := s.Scrub() + err := s.Scrub() if err != nil { logger.Fatalf("error while scrubbing files: %s", err) } diff --git a/delete.go b/delete.go index 69d2a7b..71fa401 100644 --- a/delete.go +++ b/delete.go @@ -16,7 +16,8 @@ func newDeleteAction(dir *directory, fs Filesystem, log logger, pretend bool) *d // perform deletes files that are past a certain age or certain size. func (a deleteAction) perform(files []os.FileInfo, check checkFn) ([]os.FileInfo, error) { - for i, file := range files { + var newFiles []os.FileInfo + for _, file := range files { file := file filename := a.fs.FullPath(file, a.dir.Path) if check(file) { @@ -33,10 +34,10 @@ func (a deleteAction) perform(files []os.FileInfo, check checkFn) ([]os.FileInfo a.log.Printf("[Delete] ERROR: Failed to delete file %s: %s", filename, err) continue } - files = a.action.removeFile(files, i) } else { a.log.Printf("[Delete] No action is needed for file %s", filename) + newFiles = append(newFiles, file) } } - return files, nil + return newFiles, nil } diff --git a/directory.go b/directory.go index 0c03970..5e385d6 100644 --- a/directory.go +++ b/directory.go @@ -2,7 +2,6 @@ package scrubber import ( "os" - "slices" ) // directory holds the cleanup information for a single path in the filesystem. @@ -86,13 +85,6 @@ func ApplyKeepLatest(files []os.FileInfo, latest int) []os.FileInfo { return files } - slices.SortFunc(files, func(i, j os.FileInfo) int { - if i.ModTime().After(j.ModTime()) { - return 0 - } - return 1 - }) - if len(files) > latest { return files[latest:] } diff --git a/scrubber.go b/scrubber.go index b0c2220..127b967 100644 --- a/scrubber.go +++ b/scrubber.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "slices" ) // Scrubber holds the configuration and a filesystem handle. @@ -78,8 +79,7 @@ func New(c *TomlConfig, fs Filesystem, log logger, pretend bool) *Scrubber { } // Scrub performs the actual cleanup. -func (s Scrubber) Scrub() ([]os.FileInfo, error) { - var files []os.FileInfo +func (s Scrubber) Scrub() error { for _, configDir := range s.config.Directories { expandedDirs, err := s.expandDirs(configDir.Path) @@ -105,6 +105,14 @@ func (s Scrubber) Scrub() ([]os.FileInfo, error) { continue } + // Sort files by modification time. + slices.SortFunc(files, func(i, j os.FileInfo) int { + if i.ModTime().After(j.ModTime()) { + return 0 + } + return 1 + }) + files = scanner.filterFiles(files) files = ApplyKeepLatest(files, dir.KeepLatest) @@ -116,19 +124,20 @@ func (s Scrubber) Scrub() ([]os.FileInfo, error) { s.log.Printf("Found %d files to process", len(files)) for _, strategy := range dir.Strategies { + strategy := strategy s, err := strategyFromConfig(&strategy, &dir, s.fs, s.log, s.pretend) if err != nil { - return nil, err + return err } _, err = s.process(files) if err != nil { - return nil, fmt.Errorf("error while processing files: %s", err) + return fmt.Errorf("error while processing files: %s", err) } } } - } - return files, nil + + return nil } // expandDirs expands a Glob pattern and returns all directories. diff --git a/zip.go b/zip.go index acc8db7..36e02b2 100644 --- a/zip.go +++ b/zip.go @@ -19,7 +19,8 @@ func newZipAction(dir *directory, fs Filesystem, log logger, pretend bool) *zipA // perform zips files that are past a certain age or certain size. func (a zipAction) perform(files []os.FileInfo, check checkFn) ([]os.FileInfo, error) { - for i, file := range files { + var newFiles []os.FileInfo + for _, file := range files { file := file filename := a.fs.FullPath(file, a.dir.Path) @@ -40,12 +41,12 @@ func (a zipAction) perform(files []os.FileInfo, check checkFn) ([]os.FileInfo, e a.log.Printf("[ZIP] ERROR: Failed to delete original file %s: %s", filename, err) continue } - files = a.action.removeFile(files, i) } else { a.log.Printf("[ZIP] No action is needed for file %s", filename) + newFiles = append(newFiles, file) } } - return files, nil + return newFiles, nil } // zip creates a zip file containing a single file.