Skip to content

Commit

Permalink
Fixed loop var
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-kuendig committed Aug 24, 2023
1 parent a376fc6 commit cc0289c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
7 changes: 3 additions & 4 deletions cmd/scrubber/example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

2 changes: 1 addition & 1 deletion cmd/scrubber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
8 changes: 0 additions & 8 deletions directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package scrubber

import (
"os"
"slices"
)

// directory holds the cleanup information for a single path in the filesystem.
Expand Down Expand Up @@ -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:]
}
Expand Down
21 changes: 15 additions & 6 deletions scrubber.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
)

// Scrubber holds the configuration and a filesystem handle.
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand Down

0 comments on commit cc0289c

Please sign in to comment.