-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (112 loc) · 3.49 KB
/
Copy pathmain.go
File metadata and controls
132 lines (112 loc) · 3.49 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"errors"
"flag"
"fmt"
"os"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/2ykwang/mac-cleanup-go/internal/cli"
"github.com/2ykwang/mac-cleanup-go/internal/config"
"github.com/2ykwang/mac-cleanup-go/internal/logger"
"github.com/2ykwang/mac-cleanup-go/internal/styles"
"github.com/2ykwang/mac-cleanup-go/internal/tui"
"github.com/2ykwang/mac-cleanup-go/internal/userconfig"
pkgversion "github.com/2ykwang/mac-cleanup-go/internal/version"
)
// version is set via ldflags: go build -ldflags "-X main.version=1.0.0"
var version = "dev"
func main() {
// Flags
showVersion := flag.Bool("version", false, "Show version")
shortVersion := flag.Bool("v", false, "Show version (short)")
doUpdate := flag.Bool("update", false, "Update to latest version via Homebrew")
debugMode := flag.Bool("debug", false, "Enable debug logging to ~/.config/mac-cleanup-go/debug.log")
selectTargets := flag.Bool("select", false, "Select cleanup targets")
doClean := flag.Bool("clean", false, "Clean selected targets")
dryRun := flag.Bool("dry-run", false, "Show report without deleting (requires --clean)")
flag.Parse()
// Initialize logger: --debug flag or DEBUG env var
debug := *debugMode || os.Getenv("DEBUG") == "true"
if err := logger.Init(debug); err != nil {
fmt.Fprintf(os.Stderr, "warning: logging disabled: %v\n", err)
}
defer logger.Close()
if *showVersion || *shortVersion {
fmt.Printf("mac-cleanup %s\n", version)
return
}
if *doUpdate {
fmt.Println("Updating mac-cleanup-go via Homebrew...")
if err := pkgversion.RunUpdate(); err != nil {
fmt.Fprintf(os.Stderr, "Update failed: %v\n", err)
os.Exit(1)
}
fmt.Println("Update complete!")
return
}
cfg, err := config.LoadEmbedded()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
os.Exit(1)
}
if *selectTargets && *doClean {
fmt.Fprintln(os.Stderr, "error: --select cannot be combined with --clean")
os.Exit(1)
}
if *dryRun && !*doClean {
fmt.Fprintln(os.Stderr, "error: --dry-run requires --clean")
os.Exit(1)
}
if *selectTargets {
p := tea.NewProgram(
tui.NewConfigModel(cfg),
)
if _, err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
return
}
if *doClean {
// CLI path has no event loop, so detect background once up front.
theme := styles.New(lipgloss.HasDarkBackground(os.Stdin, os.Stdout))
userCfg, err := userconfig.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load user config: %v\n", err)
os.Exit(1)
}
runner, err := cli.NewRunner(cfg, userCfg)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to initialize cli runner: %v\n", err)
os.Exit(1)
}
report, warnings, err := runner.Run(*dryRun)
if err != nil {
switch {
case errors.Is(err, cli.ErrNoSelection):
fmt.Fprintln(os.Stderr, "no targets selected. run `mac-cleanup --select` to configure.")
case errors.Is(err, cli.ErrNoEligibleTargets):
fmt.Fprintln(os.Stderr, "no eligible targets selected. run `mac-cleanup --select` to configure.")
default:
fmt.Fprintf(os.Stderr, "clean failed: %v\n", err)
}
os.Exit(1)
}
if len(warnings) > 0 {
fmt.Fprintln(os.Stderr, "Warnings:")
for _, warning := range warnings {
fmt.Fprintln(os.Stderr, " - "+warning)
}
}
fmt.Print(cli.FormatReport(report, *dryRun, theme))
return
}
p := tea.NewProgram(
tui.NewModel(cfg, version),
)
if _, err := p.Run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}