Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
420 lines (394 loc) · 13.4 KB
/
Copy pathmain.go
File metadata and controls
420 lines (394 loc) · 13.4 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package main
import (
"crypto/sha256"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
func main() {
log.SetFlags(0)
maybeAutoUpdate()
if len(os.Args) == 1 {
runNoArg(os.Stdout)
return
}
if len(os.Args) > 1 {
if isManageCmd(os.Args[1]) {
if err := runManage(os.Args[1], os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "manage: %v\n", err)
os.Exit(1)
}
return
}
switch os.Args[1] {
case "_serve":
if err := runRunner(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "_serve: %v\n", err)
os.Exit(1)
}
return
case "_preview":
if err := runPreviewServer(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "_preview: %v\n", err)
os.Exit(1)
}
return
case "--version", "version":
printVersion(os.Stdout)
return
case "signup":
if err := runSignup(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "signup: %v\n", err)
os.Exit(1)
}
return
case "share":
if err := runShare(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "share: %v\n", err)
os.Exit(1)
}
return
case "watch":
if err := runWatchCmd(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "watch: %v\n", err)
os.Exit(1)
}
return
case "remove":
if err := runRemove(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "remove: %v\n", err)
os.Exit(1)
}
return
case "archive":
if err := runArchive(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "archive: %v\n", err)
os.Exit(1)
}
return
case "list":
if err := runList(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "list: %v\n", err)
os.Exit(1)
}
return
case "invite":
if err := runInvite(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "invite: %v\n", err)
os.Exit(1)
}
return
case "status":
if err := runStatus(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "status: %v\n", err)
os.Exit(1)
}
return
case "stop":
if err := runStop(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "stop: %v\n", err)
os.Exit(1)
}
return
case "logs":
if err := runLogs(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "logs: %v\n", err)
os.Exit(1)
}
return
case "runner":
if err := runRunnerCmd(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "runner: %v\n", err)
os.Exit(1)
}
return
case "completion":
if err := runCompletion(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "completion: %v\n", err)
os.Exit(1)
}
return
case "auth":
if err := runAuth(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "auth: %v\n", err)
os.Exit(1)
}
return
case "comments":
if err := runComments(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "comments: %v\n", err)
os.Exit(1)
}
return
case "mcp":
if err := runMCP(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "mcp: %v\n", err)
os.Exit(1)
}
return
case "skill":
if err := runSkill(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "skill: %v\n", err)
os.Exit(1)
}
return
case "uninstall":
if err := runUninstall(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "uninstall: %v\n", err)
os.Exit(1)
}
return
case "--help", "-h", "help":
printUsage(os.Stdout)
return
}
}
if len(os.Args) > 1 && (os.Args[1] == "--upgrade" || os.Args[1] == "upgrade") {
if len(os.Args) > 2 {
fmt.Fprintln(os.Stderr, "error: --upgrade takes no arguments")
os.Exit(1)
}
if err := runUpgrade(); err != nil {
log.Fatalf("upgrade: %v", err)
}
return
}
outFile := flag.String("outfile", "", "Optional: write HTML output to file instead of opening in browser")
watch := flag.Bool("watch", false, "Watch the file for changes and live-reload the browser preview")
foreground := flag.Bool("foreground", false, "With --watch, run the blocking watcher in-process (no runner handoff)")
silent := flag.Bool("silent", false, "Do not open a browser; still print the preview URL")
existing := flag.Bool("existing", false, "With --watch on a directory, also onboard unmatched .md files already in the folder")
noRecursive := flag.Bool("no-recursive", false, "With --watch on a directory, do not watch subdirectories")
glob := flag.String("glob", "", "With --watch on a directory, filename glob (default **/*.md)")
yes := flag.Bool("yes", false, "With --watch --existing on a directory, confirm more than 50 files")
upgrade := flag.Bool("upgrade", false, "Install the latest release, or pinned_version when set, then exit")
flag.Parse()
if *upgrade {
if err := runUpgrade(); err != nil {
log.Fatalf("upgrade: %v", err)
}
return
}
args := flag.Args()
if len(args) == 0 {
printUsage(os.Stderr)
os.Exit(1)
}
inFile := args[0]
absPath, err := filepath.Abs(inFile)
if err != nil {
log.Fatalf("Failed to resolve file path: %v", err)
}
if *outFile != "" && *watch {
fmt.Fprintln(os.Stderr, "error: -outfile and --watch cannot be used together")
os.Exit(1)
}
cfg, err := LoadConfig()
if err != nil {
log.Printf("config: %v (using defaults)", err)
}
useWatch := *watch
if !flagWasSet("watch") {
useWatch = cfg.Watch
}
dirFlags := *existing || *noRecursive || *glob != "" || *yes
if fi, statErr := os.Stat(absPath); statErr == nil && fi.IsDir() {
if *outFile != "" {
fmt.Fprintln(os.Stderr, "error: -outfile cannot be used with a directory")
os.Exit(1)
}
if !useWatch {
fmt.Fprintf(os.Stderr, "error: %s is a directory; pass --watch to adopt new markdown files\n", absPath)
os.Exit(1)
}
if *foreground {
fmt.Fprintln(os.Stderr, "error: directory watches require the runner; drop --foreground")
os.Exit(1)
}
if *glob != "" {
if _, err := filepath.Match(*glob, "x"); err != nil {
log.Fatalf("invalid --glob: %v", err)
}
}
canonical, err := canonicalPath(absPath)
if err != nil {
log.Fatalf("watch: %v", err)
}
if err := handOffWatchDir(canonical, string(modeDirLocal), dirWatchOpts{
Recursive: !*noRecursive,
Glob: *glob,
Existing: *existing,
Yes: *yes,
}); err != nil {
log.Fatalf("watch: %v", err)
}
return
}
if dirFlags {
fmt.Fprintln(os.Stderr, "error: --existing, --no-recursive, --glob, and --yes only apply to directories")
os.Exit(1)
}
if useWatch {
if *foreground {
if err := runWatch(absPath, cfg, *silent); err != nil {
log.Fatalf("watch: %v", err)
}
return
}
if err := handOffWatch(absPath, *silent); err != nil {
log.Fatalf("watch: %v", err)
}
return
}
if *outFile != "" {
content, err := os.ReadFile(absPath)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
if err := writeHTMLTo(*outFile, content); err != nil {
log.Fatalf("Failed to write HTML: %v", err)
}
fmt.Printf("HTML written to: %s\n", *outFile)
return
}
if err := runOneShotPreview(absPath, *silent); err != nil {
log.Fatalf("%v", err)
}
}
func runOneShotPreview(absPath string, silent bool) error {
content, err := os.ReadFile(absPath)
if err != nil {
return fmt.Errorf("Failed to read file: %v", err)
}
if !signedUp() {
url, err := startSignupPreview(absPath, content)
if err != nil {
return fmt.Errorf("Failed to serve preview: %v", err)
}
fmt.Printf("Preview at: %s\n", url)
if silent {
return nil
}
if err := openBrowser(url); err != nil {
log.Printf("Warning: could not open browser: %v", err)
}
return nil
}
tmpPath, err := writeHTMLToTemp(content)
if err != nil {
return fmt.Errorf("Failed to write temp HTML: %v", err)
}
url := "file://" + tmpPath
fmt.Printf("Preview at: %s\n", url)
if silent {
return nil
}
if err := openBrowser(url); err != nil {
log.Printf("Warning: could not open browser: %v", err)
}
return nil
}
func printVersion(w io.Writer) {
fmt.Fprintf(w, "gander %s\n", Version)
}
func runNoArg(w io.Writer) {
fmt.Fprintf(w, "gander %s\n", Version)
switch {
case Version == "dev":
fmt.Fprintln(w, "Running a dev build — gander --upgrade may be a no-op")
default:
rel, pin, err := resolveUpdateTarget()
switch {
case err != nil:
fmt.Fprintf(w, "(could not check for updates: %v)\n", err)
case pin != "" && rel == nil:
fmt.Fprintf(w, "Pinned to %s.\n", pin)
case pin != "":
fmt.Fprintf(w, "Pinned to %s (installed: %s).\n", pin, Version)
case rel == nil:
fmt.Fprintln(w, "You're on the latest release.")
default:
fmt.Fprintf(w, "Update available: %s → run gander --upgrade\n", rel.TagName)
}
}
fmt.Fprintln(w)
printUsage(w)
}
func printUsage(w io.Writer) {
cfg, _ := LoadConfig()
authed := cfg.APIToken != ""
fmt.Fprintln(w, "gander — render Markdown, optionally share it on gander.md")
fmt.Fprintln(w)
fmt.Fprintln(w, "Usage:")
fmt.Fprintln(w, " gander <file.md|dir> [options] Render and open locally (--watch a dir adopts new .md)")
fmt.Fprintln(w, " gander signup --email <addr> Open signup form in your browser, save the API token")
if authed {
fmt.Fprintln(w, " gander share [--watch] [--silent] [--existing] [--no-recursive] [--glob=pattern] [--yes]")
fmt.Fprintln(w, " [--visibility=anyone|private|hidden] [--private]")
fmt.Fprintln(w, " [--comments=anyone|private|disabled] [--no-comments] [--label name] [--no-labels] <file|dir>")
fmt.Fprintln(w, " Upload to gander.md (keeps a dashboard share of the same file)")
fmt.Fprintln(w, " gander watch [--silent] [--existing] [--no-recursive] [--glob=pattern] [--yes]")
fmt.Fprintln(w, " [--visibility=anyone|private|hidden] [--private]")
fmt.Fprintln(w, " [--comments=anyone|private|disabled] [--no-comments] [--label name] [--no-labels] <file|dir>")
fmt.Fprintln(w, " Live-share to gander.md and push every save (alias for `share --watch`)")
fmt.Fprintln(w, " gander remove [--all|--pick <short_id>|--yes|--non-interactive] <file|short_id|url>")
fmt.Fprintln(w, " Delete a share from gander.md")
fmt.Fprintln(w, " gander archive [--all|--pick <short_id>|--yes|--non-interactive] <file|short_id|url>")
fmt.Fprintln(w, " Hide a share; sharing the file again restores it")
fmt.Fprintln(w, " gander list List shares currently on gander.md")
fmt.Fprintln(w, " gander invite [--email <addr>] [--share <short_id>] Mint a team invite link")
fmt.Fprintln(w, " gander comments [file] List unresolved review comments")
fmt.Fprintln(w, " gander mcp [install] MCP stdio server, or install into agent runtimes")
fmt.Fprintln(w, " gander manage | dashboard | dash | --d Open the dashboard in your browser")
fmt.Fprintln(w, " gander auth <api_token> Install a new API token (e.g. after rotating in the dashboard)")
}
fmt.Fprintln(w, " gander status Show runner version + active watches + URLs")
fmt.Fprintln(w, " gander stop [<file>|<id>] [--all] Remove a watch from the runner")
fmt.Fprintln(w, " gander logs [<id>] [--follow|--no-follow] Tail the runner log; filter by watch id")
fmt.Fprintln(w, " gander runner install|uninstall Auto-start the runner at login (LaunchAgent/systemd)")
fmt.Fprintln(w, " gander skill [install] Install the agent skill (OpenCode, Claude, Cursor, Grok)")
fmt.Fprintln(w, " gander uninstall [--yes] [--keep-config] Remove CLI, MCP, skill, runner (and optionally ~/.gander)")
fmt.Fprintln(w, " gander --upgrade Install latest, or pinned_version when set")
fmt.Fprintln(w, " gander --version Print the version and exit")
fmt.Fprintln(w, " gander completion {bash|zsh} Print a shell completion script to stdout")
fmt.Fprintln(w)
fmt.Fprintln(w, "Render options:")
fmt.Fprintln(w, " -outfile string Write HTML to a file instead of opening in browser")
fmt.Fprintln(w, " -watch Live-reload the local browser preview on save (via the runner)")
fmt.Fprintln(w, " -foreground With -watch, run the blocking watcher in-process (CI / debug)")
fmt.Fprintln(w, " -silent Do not open a browser; still print the preview or share URL")
fmt.Fprintln(w, " -existing With -watch on a directory, also onboard unmatched .md already in the folder")
fmt.Fprintln(w, " -no-recursive With -watch on a directory, do not watch subdirectories")
fmt.Fprintln(w, " -glob string With -watch on a directory, filename glob (default **/*.md)")
fmt.Fprintln(w, " -yes With -watch -existing, confirm more than 50 files")
if !authed {
fmt.Fprintln(w)
fmt.Fprintln(w, "Run `gander signup --email you@example.com` to enable share / watch / remove / archive / list / invite / manage / auth.")
}
}
func flagWasSet(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func writeHTMLTo(outPath string, content []byte) error {
html, headings := renderMarkdownWithIDs(string(content))
page := buildHTML(html, headings, false, "")
return os.WriteFile(outPath, []byte(page), 0644)
}
func writeHTMLToTemp(content []byte) (string, error) {
sum := sha256.Sum256(content)
name := fmt.Sprintf("gander-%x.html", sum[:8])
path := filepath.Join(os.TempDir(), name)
if err := writeHTMLTo(path, content); err != nil {
return "", err
}
return path, nil
}