refactor: replace custom ignore logic with doublestar glob matching, improve logging, and update deps to use github.com/bmatcuk/doublestar/v4 while removing unused modules

This commit is contained in:
2025-08-07 10:44:59 +02:00
parent 37b08d27f5
commit cd6017519e
3 changed files with 65 additions and 22 deletions

78
main.go
View File

@@ -11,6 +11,7 @@ import (
logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/bmatcuk/doublestar/v4"
"github.com/djherbis/times"
)
@@ -70,8 +71,8 @@ func getEnv(key, def string) string {
}
func scanRoot() {
log := logger.Default.WithPrefix("scanRoot")
log.Info("Scanning root directory...")
log := logger.Default.WithPrefix("scanRoot").WithPrefix(constants.ROOT)
log.Info("Scanning root directory")
filepath.Walk(constants.ROOT, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Error("Error scanning %s: %s", path, err)
@@ -80,21 +81,25 @@ func scanRoot() {
path = filepath.ToSlash(path)
if path == constants.ROOT {
log.Info("Skipping root directory %s...", path)
log.Info("Skipping root directory %s", path)
return nil
}
// I forgot why this code was here... It doesn't make sense to me now
// if info.IsDir() {
// log.Info("Skipping directory %s...", path)
// log.Info("Skipping directory %s", path)
// return filepath.SkipDir
// }
// We hope that IGNORED_DIRECTORIES is a small list, so we can afford to iterate over it
// In fact iteration should be faster for small lists rather than hashing
for _, ignoredDir := range constants.IGNORED_DIRECTORIES {
log.Info("Ignored directories: %s", constants.IGNORED_DIRECTORIES)
if strings.HasPrefix(path, ignoredDir) {
matched, err := doublestar.Match(ignoredDir, path)
if err != nil {
log.Error("Error matching %s: %v", path, err)
continue
}
if matched {
log.Info("Ignoring directory %s", path)
return filepath.SkipDir
}
@@ -201,28 +206,68 @@ func deleteFile(path string) {
numFilesDeleted++
}
func shouldIgnore(path string) bool {
log := logger.Default.WithPrefix("shouldIgnore").WithPrefix(path)
for _, ignoredDir := range constants.IGNORED_DIRECTORIES {
log.Debug("Checking if %s matches %s", ignoredDir, path)
matched, err := doublestar.Match(ignoredDir, path)
if err != nil {
log.Error("Error matching %s: %v", path, err)
continue
}
if matched {
log.Debug("Directory is ignored, skipping")
return true
}
}
log.Debug("Directory is not ignored")
return false
}
func cleanRoot() {
log := logger.Default.WithPrefix("cleanRoot")
var files, err = os.ReadDir(constants.ROOT)
files, err := doublestar.Glob(os.DirFS(constants.ROOT), "**")
if err != nil {
log.Error("Error reading root directory %s: %s", constants.ROOT, err)
return
}
for _, file := range files {
if !file.IsDir() {
continue
}
var empty, err = isDirEmpty(constants.ROOT + "/" + file.Name())
fullpath := filepath.Join(constants.ROOT, file)
filelog := log.WithPrefix(file)
var info os.FileInfo
filelog.Debug("Getting file info")
info, err = os.Stat(fullpath)
if err != nil {
log.Error("Error checking if directory %s is empty: %s", file.Name(), err)
filelog.Error("Error getting file info %v", err)
continue
}
log.Info("Directory %s isempty: %t", file.Name(), empty)
filelog.Trace("File info: %+v", info)
if !info.IsDir() {
filelog.Info("File is not a directory, skipping")
continue
}
filelog.Debug("Checking if directory is ignored")
if shouldIgnore(fullpath) {
filelog.Info("Directory is ignored, skipping")
continue
}
filelog.Debug("Directory is not ignored, checking if it is empty")
var empty, err = isDirEmpty(fullpath)
if err != nil {
filelog.Error("Error checking if directory - is empty: %v", err)
continue
}
filelog.Info("Directory isempty: %t", empty)
if empty {
log.Info("Deleting empty directory %s", file.Name())
var err = os.RemoveAll(constants.ROOT + "/" + file.Name())
filelog.Info("Deleting empty directory")
var err = os.RemoveAll(fullpath)
if err != nil {
log.Error("Error deleting empty directory %s: %s", file.Name(), err)
filelog.Error("Error deleting empty directory %v", err)
}
}
}
@@ -289,6 +334,7 @@ func main() {
IGNORED_DIRECTORIES = append(IGNORED_DIRECTORIES, strings.Split(ignoredEnv, ",")...)
}
IGNORED_DIRECTORIES = append(IGNORED_DIRECTORIES, ROOT_ARCHIVE)
IGNORED_DIRECTORIES = append(IGNORED_DIRECTORIES, ROOT)
for key, dir := range IGNORED_DIRECTORIES {
IGNORED_DIRECTORIES[key] = filepath.ToSlash(strings.TrimSpace(dir))
}