forked from pashkov256/deletor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (69 loc) · 2.14 KB
/
main.go
File metadata and controls
88 lines (69 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"os"
"github.com/pashkov256/deletor/internal/cli/config"
"github.com/pashkov256/deletor/internal/cli/output"
"github.com/pashkov256/deletor/internal/filemanager"
"github.com/pashkov256/deletor/internal/rules"
"github.com/pashkov256/deletor/internal/tui"
"github.com/pashkov256/deletor/internal/utils"
)
type Task struct {
info os.FileInfo
}
func main() {
var rules = rules.NewRules()
rules.SetupRulesConfig()
var config = config.GetFlags()
var fm filemanager.FileManager = filemanager.NewFileManager()
if !config.IsCLIMode {
// Start TUI
if err := tui.Start(fm, rules); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
} else {
extMap := utils.ParseExtToMap(config.Extensions)
fileScanner := filemanager.NewFileScanner(fm, &filemanager.FileFilter{
MinSize: config.MinSize,
Extensions: extMap,
Exclude: config.Exclude,
}, config.ShowProgress)
printer := output.NewPrinter()
// If no extensions specified, print usage
if len(extMap) == 0 {
fmt.Println("Error: No file extensions specified. Use -e flag or EXTENSIONS environment variable")
fmt.Println("Example: -e \"jpg,png,mp4\" or EXTENSIONS=jpg,png,mp4")
os.Exit(1)
}
if config.ShowProgress {
fileScanner.ProgressBarScanner(config.Directory)
}
var toDeleteMap map[string]string
var totalClearSize int64
if config.IncludeSubdirs {
toDeleteMap, totalClearSize = fileScanner.ScanFilesRecursively(config.Directory)
} else {
toDeleteMap, totalClearSize = fileScanner.ScanFilesCurrentLevel(config.Directory)
}
if len(toDeleteMap) != 0 {
printer.PrintFilesTable(toDeleteMap)
actionIsDelete := true
fmt.Println() // This is required for formatting
if config.ConfirmDelete {
fmt.Println(utils.FormatSize(totalClearSize), "will be cleared.")
actionIsDelete = printer.AskForConfirmation("Delete these files?")
}
if actionIsDelete {
printer.PrintSuccess("Deleted: %s", utils.FormatSize(totalClearSize))
for path := range toDeleteMap {
os.Remove(path)
}
utils.LogDeletionToFile(toDeleteMap)
}
} else {
printer.PrintWarning("File not found")
}
}
}