refactor: rename WORKDIR to ROOT, export env vars in debug.sh, enhance logging, and adjust path handling for clarity and robustness
This commit is contained in:
5
debug.sh
5
debug.sh
@@ -1,4 +1,9 @@
|
|||||||
|
ROOT=~/Documents
|
||||||
SCAN_INTERVAL=20s
|
SCAN_INTERVAL=20s
|
||||||
FORBIDDEN="3D Objects, Pictures, Recorded Calls, vmlogs, Music, Searches, Favorites, .wallaby, .android, .dotnet, .rest-client, .liner, .duckdb, .tree-sitter, .console-ninja, .quokka, .ipython, .templateengine, .dbus-keyrings, .azure, .aws, .dolt, .kube, .keras, .codegpt"
|
FORBIDDEN="3D Objects, Pictures, Recorded Calls, vmlogs, Music, Searches, Favorites, .wallaby, .android, .dotnet, .rest-client, .liner, .duckdb, .tree-sitter, .console-ninja, .quokka, .ipython, .templateengine, .dbus-keyrings, .azure, .aws, .dolt, .kube, .keras, .codegpt"
|
||||||
|
|
||||||
|
export SCAN_INTERVAL
|
||||||
|
export FORBIDDEN
|
||||||
|
export ROOT
|
||||||
|
|
||||||
go run . "$@"
|
go run . "$@"
|
45
main.go
45
main.go
@@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
WorkDir string
|
Root string
|
||||||
Patterns []string
|
Patterns []string
|
||||||
ScanInterval time.Duration
|
ScanInterval time.Duration
|
||||||
}
|
}
|
||||||
@@ -69,9 +69,9 @@ func loadConfig() Config {
|
|||||||
// but it is safe to call to honor standard logger flags if provided.
|
// but it is safe to call to honor standard logger flags if provided.
|
||||||
logger.InitFlag()
|
logger.InitFlag()
|
||||||
|
|
||||||
workdir := filepath.ToSlash(strings.TrimSpace(getenv("WORKDIR", "/tmp")))
|
root := filepath.ToSlash(strings.TrimSpace(getenv("ROOT", "/tmp")))
|
||||||
if pfx := strings.TrimSpace(getenv("PATH_PREFIX", "")); pfx != "" {
|
if pfx := strings.TrimSpace(getenv("PATH_PREFIX", "")); pfx != "" {
|
||||||
workdir = filepath.ToSlash(filepath.Join(workdir, pfx))
|
root = filepath.ToSlash(filepath.Join(root, pfx))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patterns from FORBIDDEN (comma-separated). Relative to WorkDir.
|
// Patterns from FORBIDDEN (comma-separated). Relative to WorkDir.
|
||||||
@@ -88,13 +88,13 @@ func loadConfig() Config {
|
|||||||
interval := time.Duration(parseDurationMS(getenv("SCAN_INTERVAL", "60s"))) * time.Millisecond
|
interval := time.Duration(parseDurationMS(getenv("SCAN_INTERVAL", "60s"))) * time.Millisecond
|
||||||
|
|
||||||
logger.Info("Config:")
|
logger.Info("Config:")
|
||||||
logger.Info(" WORKDIR: %s", workdir)
|
logger.Info(" ROOT: %s", root)
|
||||||
logger.Info(" PATH_PREFIX: %s", getenv("PATH_PREFIX", ""))
|
logger.Info(" PATH_PREFIX: %s", getenv("PATH_PREFIX", ""))
|
||||||
logger.Info(" FORBIDDEN: %v", patterns)
|
logger.Info(" FORBIDDEN: %v", patterns)
|
||||||
logger.Info(" SCAN_INTERVAL(ms): %d", interval.Milliseconds())
|
logger.Info(" SCAN_INTERVAL(ms): %d", interval.Milliseconds())
|
||||||
|
|
||||||
return Config{
|
return Config{
|
||||||
WorkDir: workdir,
|
Root: root,
|
||||||
Patterns: patterns,
|
Patterns: patterns,
|
||||||
ScanInterval: interval,
|
ScanInterval: interval,
|
||||||
}
|
}
|
||||||
@@ -102,50 +102,59 @@ func loadConfig() Config {
|
|||||||
|
|
||||||
func deleteMatches(cfg Config) {
|
func deleteMatches(cfg Config) {
|
||||||
log := logger.Default.WithPrefix("deleteMatches")
|
log := logger.Default.WithPrefix("deleteMatches")
|
||||||
|
log.Debug("Starting deleteMatches operation.")
|
||||||
|
log.Trace("Config: %+v", cfg)
|
||||||
|
|
||||||
if cfg.WorkDir == "" {
|
if cfg.Root == "" {
|
||||||
log.Error("WorkDir is empty")
|
log.Error("Root is empty")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(cfg.WorkDir); err != nil {
|
if _, err := os.Stat(cfg.Root); err != nil {
|
||||||
log.Error("WorkDir not accessible %s: %v", cfg.WorkDir, err)
|
log.Error("Root not accessible %s: %v", cfg.Root, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pat := range cfg.Patterns {
|
for _, pat := range cfg.Patterns {
|
||||||
|
patlog := log.WithPrefix(pat)
|
||||||
|
patlog.Debug("Processing pattern")
|
||||||
if pat == "" {
|
if pat == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
matches, err := doublestar.Glob(os.DirFS(cfg.WorkDir), pat)
|
matches, err := doublestar.Glob(os.DirFS(cfg.Root), pat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("glob %q: %v", pat, err)
|
patlog.Error("glob %q: %v", pat, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(matches) == 0 {
|
if len(matches) == 0 {
|
||||||
log.Debug("No matches for pattern %q", pat)
|
patlog.Debug("No matches for pattern")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
patlog.Trace("Found matches for pattern %q: %v", pat, matches)
|
||||||
|
|
||||||
for _, rel := range matches {
|
for _, rel := range matches {
|
||||||
full := filepath.Clean(filepath.Join(cfg.WorkDir, rel))
|
itemlog := patlog.WithPrefix(rel)
|
||||||
|
itemlog.Debug("Processing matched item")
|
||||||
|
full := filepath.Clean(filepath.Join(cfg.Root, rel))
|
||||||
info, err := os.Stat(full)
|
info, err := os.Stat(full)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warning("stat %s: %v", full, err)
|
itemlog.Warning("stat %s: %v", full, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if info.IsDir() {
|
if info.IsDir() {
|
||||||
log.Info("Removing directory %s", full)
|
itemlog.Info("Removing directory %s", full)
|
||||||
} else {
|
} else {
|
||||||
log.Info("Removing file %s", full)
|
itemlog.Info("Removing file %s", full)
|
||||||
}
|
}
|
||||||
|
itemlog.Trace("Attempting to remove: %s", full)
|
||||||
if err := os.RemoveAll(full); err != nil {
|
if err := os.RemoveAll(full); err != nil {
|
||||||
log.Error("remove %s: %v", full, err)
|
itemlog.Error("remove %s: %v", full, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
itemlog.Debug("Successfully removed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log.Debug("Finished deleteMatches operation.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func doRun(cfg Config) {
|
func doRun(cfg Config) {
|
||||||
|
Reference in New Issue
Block a user