tfc is a lightweight and flexible Go package that allows you to clean up and manage files and directories. It supports file and directory removal, creation, and cleanup operations based on access time.
- ✅ Remove a list of specific files or directories.
- 🧹 Remove files based on last access time.
- 📂 Create directories and files dynamically.
- 📝 Create files with content.
- 🧪 Fully configurable using functional options.
go get github.com/xxxAlvaDevxxx/tfcpackage main
import (
"log"
"time"
"github.com/xxxAlvaDevxxx/tfc"
)
func main() {
err := tfc.Clean("example-dir",
tfc.WithRemoveListFiles(true),
tfc.WithFilesToRemove([]string{"temp.txt", "old.log"}),
tfc.WithRemoveListDirs(true),
tfc.WithDirsToRemove([]string{"cache", "tmp"}),
tfc.WithRemoveFilesWithATimeFromADirectory(true),
tfc.WithFilesWithATimeFromADirectory([]string{"session.txt"}),
tfc.WithFilesWithATimeFromADirectoryDuration(48*time.Hour),
tfc.WithCreateDir(true),
tfc.WithDirToCreate("logs"),
tfc.WithCreateFile(true),
tfc.WithFileToCreate("logs/output.log"),
tfc.WithCreateFileWithContent(true),
tfc.WithFileToCreateWithContent("logs/info.txt"),
tfc.WithFileContent("Folder cleaned successfully."),
)
if err != nil {
log.Fatal(err)
}
}Clean(dir string, opts ...Option) error: Main function to clean and manage directory contents.
| Option | Description |
|---|---|
WithRemoveListFiles(bool) |
Enable or disable file removal. |
WithFilesToRemove([]string) |
Specify list of files to remove. |
WithRemoveListDirs(bool) |
Enable or disable directory removal. |
WithDirsToRemove([]string) |
Specify list of directories to remove. |
WithRemoveFilesWithATimeFromADirectory(bool) |
Enable time-based cleanup. |
WithFilesWithATimeFromADirectory([]string) |
Files to be checked against duration. |
WithFilesWithATimeFromADirectoryDuration(time.Duration) |
Threshold duration for cleanup. |
WithCreateDir(bool) |
Enable directory creation. |
WithDirToCreate(string) |
Directory path to create. |
WithCreateFile(bool) |
Enable file creation. |
WithFileToCreate(string) |
File path to create. |
WithCreateFileWithContent(bool) |
Enable creation of file with content. |
WithFileToCreateWithContent(string) |
File path for writing content. |
WithFileContent(string) |
Content to write into file. |
ListFiles(dir string) ([]string, error): List files in a directory.ListDirs(dir string) ([]string, error): List subdirectories in a directory.