pkg/io¶
Small filesystem helpers for bulk operations (delete, backup, restore) based on file extensions. The package walks a directory tree, so you can prepare rollbacks or cleanups with a single call.
Quick Start¶
API Overview¶
| Function | Description |
|---|---|
SafeClose(c) |
safely close closer c, no output |
SafeClosePrint(c) |
safely close closer c, and output the error if any |
DeleteFileWithExt(dir, []string{".log"}) |
remove every matching file (no prompt) |
BackupFilesWithExt(dir, []string{".conf"}) |
copy foo.conf → foo.conf.bak |
RestoreAllBakFiles(dir) |
copy *.bak back to originals and remove the .bak files |
Extensions must include the leading dot (
.log). Matches are based on the final path extension (file.txt.bakis treated as.bak).
Usage Patterns¶
Cleaning Generated Files¶
Safe Inline Backups¶
Testing Hooks¶
Internally the package swaps os.Remove, filepath.Walk, and copy operations with overridable function variables to
simplify unit tests. In production you never touch these, but the pattern means you can inject fakes when writing tests
for your code that depends on pkg/io.
Tips¶
- Run these helpers on the narrowest directory possible to avoid traversing large trees.
- All operations are best-effort: once a delete or copy fails, the function returns the first error with the file path.
RestoreAllBakFilesoverwrites existing originals. UseBackupFilesWithExtimmediately before mutations if you need a guaranteed rollback path.