68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
logger "git.site.quack-lab.dev/dave/cylogger"
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
logger.InitFlag()
|
|
if flag.NArg() == 0 {
|
|
fmt.Println("Usage: cleaner <files>")
|
|
os.Exit(1)
|
|
}
|
|
// regex to match "2025-07-08"
|
|
re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})`)
|
|
|
|
for _, file := range flag.Args() {
|
|
filelog := logger.Default.WithPrefix(file)
|
|
filelog.Info("Processing file")
|
|
|
|
info, err := os.Stat(file)
|
|
if err != nil {
|
|
filelog.Error("ERROR: %v\n", err)
|
|
continue
|
|
}
|
|
if info.IsDir() {
|
|
filelog.Info("SKIP (directory): %s\n", file)
|
|
continue
|
|
}
|
|
|
|
name := filepath.Base(file)
|
|
match := re.FindStringSubmatch(name)
|
|
filelog.Debug("Match: %v", match)
|
|
if match == nil {
|
|
filelog.Info("SKIP (no date pattern): %s\n", name)
|
|
continue
|
|
}
|
|
|
|
newName := match[1] + filepath.Ext(name)
|
|
filelog.Debug("New name: %s", newName)
|
|
if name == newName {
|
|
filelog.Info("SKIP (already named): %s\n", name)
|
|
continue
|
|
}
|
|
|
|
filelog.Debug("Checking if target exists: %s", newName)
|
|
if _, err := os.Stat(newName); err == nil {
|
|
filelog.Info("SKIP (target exists): %s -> %s\n", name, newName)
|
|
continue
|
|
}
|
|
|
|
filelog.Info("Renaming to: %s", newName)
|
|
err = os.Rename(name, newName)
|
|
if err != nil {
|
|
filelog.Error("ERROR renaming %s: %v\n", name, err)
|
|
} else {
|
|
filelog.Info("RENAMED: %s -> %s\n", name, newName)
|
|
}
|
|
filelog.Info("All done")
|
|
continue
|
|
}
|
|
}
|