-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
183 lines (161 loc) · 5.02 KB
/
Copy pathmain.go
File metadata and controls
183 lines (161 loc) · 5.02 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"runtime"
"runtime/debug"
"strings"
"github.com/erick303/spacestation/internal/config"
"github.com/erick303/spacestation/internal/scan"
"github.com/erick303/spacestation/internal/tui"
)
func main() {
// spacestation is macOS-only: it shells out to osascript for the Trash
// action, knows ~/Library/* layout, and its smart probes call macOS
// ecosystem CLIs (brew, xcrun simctl). Code happens to cross-compile,
// but the runtime behaviour is meaningless on other platforms — fail
// fast with a clear message rather than silently producing nothing.
if runtime.GOOS != "darwin" {
fmt.Fprintf(os.Stderr, "spacestation is macOS-only (built for darwin, running on %s)\n", runtime.GOOS)
os.Exit(1)
}
var (
jsonOut = flag.Bool("json", false, "non-interactive: print candidates as JSON and exit")
dryRun = flag.Bool("dry-run", false, "with --json, print what would be deleted (default-selected only)")
noDownloads = flag.Bool("no-downloads", false, "skip ~/Downloads")
noTrash = flag.Bool("no-trash", false, "skip ~/.Trash")
noScreens = flag.Bool("no-screenshots", false, "skip macOS screenshots (Desktop / configured location)")
showConfig = flag.Bool("config", false, "print effective config path and exit")
showVersion = flag.Bool("version", false, "print version and exit")
scanRoot rootFlag
)
flag.Var(&scanRoot, "scan-root", "root to scan for project artifact dirs (repeatable; replaces config project_roots, not additive)")
flag.Parse()
if *showVersion {
fmt.Println(versionString())
return
}
cfg, cfgPath, firstRun, err := config.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "warning: could not load config (%v); using defaults\n", err)
}
if *showConfig {
fmt.Println(cfgPath)
return
}
// First run: seed project_roots from detected dev folders. The interactive
// TUI onboarding lets the user adjust these and saves the result; the
// non-interactive (--json) path persists the seed below so the next run has
// a config to read.
if firstRun {
cfg.Scan.ProjectRoots = config.DetectProjectRoots()
}
if len(scanRoot) > 0 {
cfg.Scan.ProjectRoots = []string(scanRoot)
}
if *noDownloads {
cfg.Scan.IncludeDownloads = false
}
if *noTrash {
cfg.Scan.IncludeTrash = false
}
if *noScreens {
cfg.Scan.IncludeScreenshots = false
}
if *jsonOut {
if firstRun {
if _, err := config.Save(cfg); err != nil {
fmt.Fprintf(os.Stderr, "warning: could not write config: %v\n", err)
}
}
runJSON(cfg, *dryRun)
return
}
if err := tui.Run(cfg, firstRun); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
type rootFlag []string
func (r *rootFlag) String() string { return strings.Join(*r, ",") }
func (r *rootFlag) Set(s string) error {
*r = append(*r, s)
return nil
}
func versionString() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return "spacestation dev"
}
version := info.Main.Version
// "(devel)", empty, or a v0.0.0-<ts>-<sha> pseudo-version all mean "no
// tagged release" — collapse to "dev" and let the VCS suffix carry detail.
if version == "" || version == "(devel)" || strings.HasPrefix(version, "v0.0.0-") {
version = "dev"
}
var revision, date string
var dirty bool
for _, s := range info.Settings {
switch s.Key {
case "vcs.revision":
revision = s.Value
case "vcs.time":
date = s.Value
case "vcs.modified":
dirty = s.Value == "true"
}
}
// Build the "(short-sha[-dirty], date)" suffix when VCS info is present.
var detail string
if revision != "" {
short := revision
if len(short) > 7 {
short = short[:7]
}
if dirty {
short += "-dirty"
}
if date != "" {
detail = fmt.Sprintf(" (%s, %s)", short, date)
} else {
detail = fmt.Sprintf(" (%s)", short)
}
}
return "spacestation " + version + detail
}
func runJSON(cfg config.Config, dry bool) {
// Missing roots are walked-then-skipped silently; warn on stderr so the
// JSON on stdout stays clean and parseable.
if missing := cfg.MissingRoots(); len(missing) > 0 {
fmt.Fprintf(os.Stderr, "warning: project roots not found (skipped): %s\n", strings.Join(missing, ", "))
}
cands := scan.Run(context.Background(), scan.Options{Cfg: cfg}, nil)
_ = scan.SaveSizeCache()
if dry {
out := struct {
Candidates []scan.Candidate `json:"candidates"`
Selected int `json:"selected_count"`
Total int `json:"total_count"`
Reclaim int64 `json:"reclaim_bytes_if_applied"`
}{Candidates: cands, Total: len(cands)}
for _, c := range cands {
if c.Selected {
out.Selected++
out.Reclaim += c.SizeBytes
}
}
_ = json.NewEncoder(os.Stdout).Encode(out)
return
}
// Non-dry --json: print full report. We don't auto-delete in --json mode.
out := struct {
Candidates []scan.Candidate `json:"candidates"`
Note string `json:"note"`
}{Candidates: cands,
Note: "JSON mode never deletes. Run interactively or pass --dry-run for what-would-be-selected.",
}
_ = json.NewEncoder(os.Stdout).Encode(out)
}