-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathsetup.go
More file actions
636 lines (558 loc) · 21.3 KB
/
setup.go
File metadata and controls
636 lines (558 loc) · 21.3 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package main
import (
"bufio"
"crypto/rand"
"encoding/hex"
"fmt"
"log/slog"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/onllm-dev/onwatch/v2/internal/api"
)
// ANSI colors
const (
colorRed = "\033[0;31m"
colorGreen = "\033[0;32m"
colorYellow = "\033[1;33m"
colorBlue = "\033[0;34m"
colorCyan = "\033[0;36m"
colorBold = "\033[1m"
colorDim = "\033[2m"
colorReset = "\033[0m"
)
type setupConfig struct {
syntheticKey string
zaiKey string
zaiBaseURL string
anthropicToken string
codexToken string
antigravityEnabled bool
geminiEnabled bool
adminUser string
adminPass string
port int
pollInterval int
}
func runSetup() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("cannot determine home directory: %w", err)
}
installDir := filepath.Join(homeDir, ".onwatch")
envFile := filepath.Join(installDir, ".env")
reader := bufio.NewReader(os.Stdin)
fmt.Println()
fmt.Printf(" %sonWatch Setup%s\n", colorBold, colorReset)
fmt.Printf(" %shttps://github.com/onllm-dev/onwatch%s\n", colorDim, colorReset)
fmt.Println()
// Create directories
if err := os.MkdirAll(filepath.Join(installDir, "data"), 0755); err != nil {
return fmt.Errorf("failed to create directories: %w", err)
}
// Check for existing .env
if _, err := os.Stat(envFile); err == nil {
existing := loadExistingEnv(envFile)
if allProvidersConfigured(existing) {
fmt.Printf(" %sinfo%s Existing .env found -- all providers configured\n", colorBlue, colorReset)
fmt.Printf(" %sTo reconfigure, delete %s and run setup again.%s\n", colorDim, envFile, colorReset)
return nil
}
if anyProviderConfigured(existing) {
return addMissingProviders(reader, envFile, existing)
}
// .env exists but no providers - remove and start fresh
fmt.Printf(" %swarn%s Existing .env found but no API keys configured\n", colorYellow, colorReset)
os.Remove(envFile)
}
// Fresh setup
cfg, err := freshSetup(reader)
if err != nil {
return err
}
if err := writeEnvFile(envFile, cfg); err != nil {
return err
}
printSummary(cfg)
printNextSteps()
return nil
}
func freshSetup(reader *bufio.Reader) (*setupConfig, error) {
fmt.Printf("\n %s--- Provider Selection ---%s\n\n", colorBold, colorReset)
providerOptions := []string{
"Synthetic only",
"Z.ai only",
"Anthropic (Claude Code) only",
"Codex only",
"Antigravity (Windsurf) only",
"Gemini CLI only",
"Multiple (choose one at a time)",
"All available",
}
choice := promptChoice(reader, "Which providers do you want to track?", providerOptions)
cfg := &setupConfig{}
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
switch choice {
case 1: // Synthetic only
cfg.syntheticKey = collectSyntheticKey(reader)
case 2: // Z.ai only
cfg.zaiKey, cfg.zaiBaseURL = collectZaiConfig(reader)
case 3: // Anthropic only
cfg.anthropicToken = collectAnthropicToken(reader, logger)
case 4: // Codex only
cfg.codexToken = collectCodexToken(reader, logger)
case 5: // Antigravity only
cfg.antigravityEnabled = true
fmt.Printf(" %s ok %s Antigravity enabled (auto-detects running Windsurf process)\n", colorGreen, colorReset)
case 6: // Gemini only
cfg.geminiEnabled = true
fmt.Printf(" %s ok %s Gemini enabled (auto-detects from ~/.gemini/oauth_creds.json)\n", colorGreen, colorReset)
case 7: // Multiple
cfg.syntheticKey, cfg.zaiKey, cfg.zaiBaseURL, cfg.anthropicToken, cfg.codexToken, cfg.antigravityEnabled, cfg.geminiEnabled = collectMultipleProviders(reader, logger)
case 8: // All
cfg.syntheticKey = collectSyntheticKey(reader)
cfg.zaiKey, cfg.zaiBaseURL = collectZaiConfig(reader)
cfg.anthropicToken = collectAnthropicToken(reader, logger)
cfg.codexToken = collectCodexToken(reader, logger)
cfg.antigravityEnabled = true
fmt.Printf("\n %s ok %s Antigravity enabled (auto-detects running Windsurf process)\n", colorGreen, colorReset)
cfg.geminiEnabled = true
fmt.Printf(" %s ok %s Gemini enabled (auto-detects from ~/.gemini/oauth_creds.json)\n", colorGreen, colorReset)
}
// Validate at least one provider
if cfg.syntheticKey == "" && cfg.zaiKey == "" && cfg.anthropicToken == "" && cfg.codexToken == "" && !cfg.antigravityEnabled && !cfg.geminiEnabled {
return nil, fmt.Errorf("at least one provider is required")
}
// Dashboard credentials
fmt.Printf("\n %s--- Dashboard Credentials ---%s\n\n", colorBold, colorReset)
cfg.adminUser = promptWithDefault(reader, "Dashboard username", "admin")
fmt.Printf(" Dashboard password %s[Enter = auto-generate]%s: ", colorDim, colorReset)
passInput := readLine(reader)
if passInput == "" {
cfg.adminPass = generatePassword()
fmt.Printf(" %s ok %s Generated password: %s%s%s\n", colorGreen, colorReset, colorBold, cfg.adminPass, colorReset)
fmt.Printf(" %sSave this password -- it won't be shown again%s\n", colorYellow, colorReset)
} else {
cfg.adminPass = passInput
fmt.Printf(" %s ok %s Password set\n", colorGreen, colorReset)
}
// Optional settings
fmt.Printf("\n %s--- Optional Settings ---%s\n\n", colorBold, colorReset)
for {
portStr := promptWithDefault(reader, "Dashboard port", "9211")
port, err := strconv.Atoi(portStr)
if err == nil && port >= 1 && port <= 65535 {
cfg.port = port
break
}
fmt.Printf(" %sMust be a number between 1 and 65535%s\n", colorRed, colorReset)
}
for {
intervalStr := promptWithDefault(reader, "Polling interval in seconds", "120")
interval, err := strconv.Atoi(intervalStr)
if err == nil && interval >= 10 && interval <= 3600 {
cfg.pollInterval = interval
break
}
fmt.Printf(" %sMust be a number between 10 and 3600%s\n", colorRed, colorReset)
}
return cfg, nil
}
func collectMultipleProviders(reader *bufio.Reader, logger *slog.Logger) (synKey, zaiKey, zaiURL, anthToken, codexToken string, antiEnabled, geminiEnabled bool) {
if promptYesNo(reader, "Add Synthetic provider?", false) {
synKey = collectSyntheticKey(reader)
}
if promptYesNo(reader, "Add Z.ai provider?", false) {
zaiKey, zaiURL = collectZaiConfig(reader)
}
if promptYesNo(reader, "Add Anthropic (Claude Code) provider?", false) {
anthToken = collectAnthropicToken(reader, logger)
}
if promptYesNo(reader, "Add Codex provider?", false) {
codexToken = collectCodexToken(reader, logger)
}
if promptYesNo(reader, "Add Antigravity (Windsurf) provider?", false) {
antiEnabled = true
fmt.Printf(" %sAntigravity auto-detects the running Windsurf process%s\n", colorDim, colorReset)
}
if promptYesNo(reader, "Add Gemini CLI provider?", false) {
geminiEnabled = true
fmt.Printf(" %sGemini auto-detects from ~/.gemini/oauth_creds.json%s\n", colorDim, colorReset)
}
return
}
func collectSyntheticKey(reader *bufio.Reader) string {
fmt.Printf("\n %sGet your key: https://synthetic.new/settings/api%s\n", colorDim, colorReset)
for {
fmt.Print(" Synthetic API key (syn_...): ")
key := readLine(reader)
if strings.HasPrefix(key, "syn_") {
fmt.Printf(" %s ok %s %s%s...%s%s\n", colorGreen, colorReset, colorDim, key[:6], key[len(key)-4:], colorReset)
return key
}
if key == "" {
fmt.Printf(" %sCannot be empty%s\n", colorRed, colorReset)
} else {
fmt.Printf(" %sKey must start with 'syn_'%s\n", colorRed, colorReset)
}
}
}
func collectZaiConfig(reader *bufio.Reader) (string, string) {
fmt.Printf("\n %sGet your key: https://www.z.ai/api-keys%s\n", colorDim, colorReset)
var key string
for {
fmt.Print(" Z.ai API key: ")
key = readLine(reader)
if key != "" {
masked := maskValue(key)
fmt.Printf(" %s ok %s %s%s%s\n", colorGreen, colorReset, colorDim, masked, colorReset)
break
}
fmt.Printf(" %sCannot be empty%s\n", colorRed, colorReset)
}
fmt.Println()
defaultURL := "https://api.z.ai/api"
if promptYesNo(reader, fmt.Sprintf("Use default Z.ai base URL (%s)?", defaultURL), true) {
return key, defaultURL
}
for {
baseURL := promptWithDefault(reader, "Z.ai base URL", "https://open.bigmodel.cn/api")
if strings.HasPrefix(baseURL, "https://") {
return key, baseURL
}
fmt.Printf(" %sURL must start with 'https://'%s\n", colorRed, colorReset)
}
}
func collectAnthropicToken(reader *bufio.Reader, logger *slog.Logger) string {
fmt.Printf("\n %sAnthropic (Claude Code) Token Setup%s\n", colorBold, colorReset)
fmt.Printf(" %sonWatch can auto-detect your Claude Code credentials.%s\n\n", colorDim, colorReset)
// Try auto-detection
if token := api.DetectAnthropicToken(logger); token != "" {
masked := maskValue(token)
fmt.Printf(" %s ok %s Auto-detected Claude Code token\n", colorGreen, colorReset)
fmt.Printf(" %sToken: %s%s\n", colorDim, masked, colorReset)
if promptYesNo(reader, "Use auto-detected token?", true) {
return token
}
} else {
fmt.Printf(" %s!%s Could not auto-detect Claude Code credentials\n", colorYellow, colorReset)
}
return promptSecret(reader, "Anthropic token")
}
func collectCodexToken(reader *bufio.Reader, logger *slog.Logger) string {
fmt.Printf("\n %sCodex Token Setup%s\n", colorBold, colorReset)
fmt.Printf(" %sonWatch can auto-detect your Codex OAuth token from auth.json.%s\n\n", colorDim, colorReset)
// Try auto-detection
if token := api.DetectCodexToken(logger); token != "" {
masked := maskValue(token)
fmt.Printf(" %s ok %s Auto-detected Codex token\n", colorGreen, colorReset)
fmt.Printf(" %sToken: %s%s\n", colorDim, masked, colorReset)
if promptYesNo(reader, "Use auto-detected token?", true) {
return token
}
} else {
fmt.Printf(" %s!%s Could not auto-detect Codex token from auth.json\n", colorYellow, colorReset)
}
return promptSecret(reader, "Codex token")
}
func writeEnvFile(path string, cfg *setupConfig) error {
var b strings.Builder
b.WriteString("# ===============================================================\n")
b.WriteString("# onWatch Configuration\n")
b.WriteString(fmt.Sprintf("# Generated by 'onwatch setup' on %s\n", time.Now().UTC().Format("2006-01-02 15:04:05 UTC")))
b.WriteString("# ===============================================================\n\n")
if cfg.syntheticKey != "" {
b.WriteString("# Synthetic API key (https://synthetic.new/settings/api)\n")
b.WriteString(fmt.Sprintf("SYNTHETIC_API_KEY=%s\n\n", cfg.syntheticKey))
}
if cfg.zaiKey != "" {
b.WriteString("# Z.ai API key (https://www.z.ai/api-keys)\n")
b.WriteString(fmt.Sprintf("ZAI_API_KEY=%s\n\n", cfg.zaiKey))
b.WriteString("# Z.ai base URL\n")
b.WriteString(fmt.Sprintf("ZAI_BASE_URL=%s\n\n", cfg.zaiBaseURL))
}
if cfg.anthropicToken != "" {
b.WriteString("# Anthropic token (Claude Code)\n")
b.WriteString(fmt.Sprintf("ANTHROPIC_TOKEN=%s\n\n", cfg.anthropicToken))
}
if cfg.codexToken != "" {
b.WriteString("# Codex OAuth token\n")
b.WriteString(fmt.Sprintf("CODEX_TOKEN=%s\n\n", cfg.codexToken))
}
if cfg.antigravityEnabled {
b.WriteString("# Antigravity (Windsurf) - auto-detected from local process\n")
b.WriteString("ANTIGRAVITY_ENABLED=true\n\n")
}
if cfg.geminiEnabled {
b.WriteString("# Gemini CLI - auto-detected from ~/.gemini/oauth_creds.json\n")
b.WriteString("GEMINI_ENABLED=true\n\n")
}
b.WriteString("# Dashboard credentials\n")
b.WriteString(fmt.Sprintf("ONWATCH_ADMIN_USER=%s\n", cfg.adminUser))
b.WriteString(fmt.Sprintf("ONWATCH_ADMIN_PASS=%s\n\n", cfg.adminPass))
b.WriteString("# Polling interval in seconds (10-3600)\n")
b.WriteString(fmt.Sprintf("ONWATCH_POLL_INTERVAL=%d\n\n", cfg.pollInterval))
b.WriteString("# Dashboard port\n")
b.WriteString(fmt.Sprintf("ONWATCH_PORT=%d\n", cfg.port))
if err := os.WriteFile(path, []byte(b.String()), 0600); err != nil {
return fmt.Errorf("failed to write .env: %w", err)
}
fmt.Printf(" %s ok %s Created %s\n", colorGreen, colorReset, path)
return nil
}
func printSummary(cfg *setupConfig) {
providers := []string{}
if cfg.syntheticKey != "" {
providers = append(providers, "Synthetic")
}
if cfg.zaiKey != "" {
providers = append(providers, "Z.ai")
}
if cfg.anthropicToken != "" {
providers = append(providers, "Anthropic")
}
if cfg.codexToken != "" {
providers = append(providers, "Codex")
}
if cfg.antigravityEnabled {
providers = append(providers, "Antigravity")
}
if cfg.geminiEnabled {
providers = append(providers, "Gemini")
}
providerLabel := strings.Join(providers, ", ")
maskedPass := strings.Repeat("*", len(cfg.adminPass))
fmt.Println()
fmt.Printf(" %s+- Configuration Summary ------------------+%s\n", colorBold, colorReset)
fmt.Printf(" %s|%s Provider: %-29s%s|%s\n", colorBold, colorReset, providerLabel, colorBold, colorReset)
fmt.Printf(" %s|%s Dashboard: %-29s%s|%s\n", colorBold, colorReset, fmt.Sprintf("http://localhost:%d", cfg.port), colorBold, colorReset)
fmt.Printf(" %s|%s Username: %-29s%s|%s\n", colorBold, colorReset, cfg.adminUser, colorBold, colorReset)
fmt.Printf(" %s|%s Password: %-29s%s|%s\n", colorBold, colorReset, maskedPass, colorBold, colorReset)
fmt.Printf(" %s|%s Interval: %-29s%s|%s\n", colorBold, colorReset, fmt.Sprintf("%ds", cfg.pollInterval), colorBold, colorReset)
fmt.Printf(" %s+-------------------------------------------+%s\n", colorBold, colorReset)
}
func printNextSteps() {
fmt.Println()
fmt.Printf(" %sNext steps:%s\n", colorBold, colorReset)
fmt.Printf(" %sonwatch%s # Start (runs in background)\n", colorCyan, colorReset)
fmt.Printf(" %sonwatch stop%s # Stop\n", colorCyan, colorReset)
fmt.Printf(" %sonwatch status%s # Status\n", colorCyan, colorReset)
fmt.Printf(" %sonwatch --debug%s # Run in foreground\n", colorCyan, colorReset)
fmt.Println()
}
// --- Existing .env handling ---
type existingEnv struct {
syntheticKey string
zaiKey string
anthropicToken string
codexToken string
antigravityEnabled bool
geminiEnabled bool
}
func loadExistingEnv(path string) *existingEnv {
data, err := os.ReadFile(path)
if err != nil {
return &existingEnv{}
}
env := &existingEnv{}
for _, line := range strings.Split(string(data), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
key, val := parts[0], parts[1]
switch key {
case "SYNTHETIC_API_KEY":
env.syntheticKey = val
case "ZAI_API_KEY":
env.zaiKey = val
case "ANTHROPIC_TOKEN":
env.anthropicToken = val
case "CODEX_TOKEN":
env.codexToken = val
case "ANTIGRAVITY_ENABLED":
env.antigravityEnabled = val == "true"
case "GEMINI_ENABLED":
env.geminiEnabled = val == "true"
}
}
if !env.geminiEnabled {
if home, err := os.UserHomeDir(); err == nil {
if _, err := os.Stat(filepath.Join(home, ".gemini", "oauth_creds.json")); err == nil {
env.geminiEnabled = true
}
}
}
return env
}
func allProvidersConfigured(env *existingEnv) bool {
return env.syntheticKey != "" && env.zaiKey != "" && env.anthropicToken != "" && env.codexToken != "" && env.antigravityEnabled && env.geminiEnabled
}
func anyProviderConfigured(env *existingEnv) bool {
return env.syntheticKey != "" || env.zaiKey != "" || env.anthropicToken != "" || env.codexToken != "" || env.antigravityEnabled || env.geminiEnabled
}
func addMissingProviders(reader *bufio.Reader, envFile string, existing *existingEnv) error {
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
configured := []string{}
if existing.syntheticKey != "" {
configured = append(configured, "Synthetic")
}
if existing.zaiKey != "" {
configured = append(configured, "Z.ai")
}
if existing.anthropicToken != "" {
configured = append(configured, "Anthropic")
}
if existing.codexToken != "" {
configured = append(configured, "Codex")
}
if existing.antigravityEnabled {
configured = append(configured, "Antigravity")
}
if existing.geminiEnabled {
configured = append(configured, "Gemini")
}
fmt.Printf(" %sinfo%s Existing .env found -- configured: %s\n\n", colorBlue, colorReset, strings.Join(configured, ", "))
f, err := os.OpenFile(envFile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return fmt.Errorf("failed to open .env: %w", err)
}
defer f.Close()
if existing.syntheticKey == "" {
if promptYesNo(reader, "Add Synthetic provider?", false) {
key := collectSyntheticKey(reader)
fmt.Fprintf(f, "\n# Synthetic API key (https://synthetic.new/settings/api)\nSYNTHETIC_API_KEY=%s\n", key)
fmt.Printf(" %s ok %s Added Synthetic provider to .env\n", colorGreen, colorReset)
}
}
if existing.zaiKey == "" {
if promptYesNo(reader, "Add Z.ai provider?", false) {
key, baseURL := collectZaiConfig(reader)
fmt.Fprintf(f, "\n# Z.ai API key (https://www.z.ai/api-keys)\nZAI_API_KEY=%s\n\n# Z.ai base URL\nZAI_BASE_URL=%s\n", key, baseURL)
fmt.Printf(" %s ok %s Added Z.ai provider to .env\n", colorGreen, colorReset)
}
}
if existing.anthropicToken == "" {
// Try auto-detection
if token := api.DetectAnthropicToken(logger); token != "" {
fmt.Printf(" %s ok %s Claude Code credentials detected on this system\n", colorGreen, colorReset)
if promptYesNo(reader, "Enable Anthropic tracking?", true) {
fmt.Fprintf(f, "\n# Anthropic token (Claude Code -- auto-detected)\nANTHROPIC_TOKEN=%s\n", token)
fmt.Printf(" %s ok %s Added Anthropic provider to .env (auto-detected)\n", colorGreen, colorReset)
}
} else if promptYesNo(reader, "Add Anthropic (Claude Code) provider?", false) {
token := collectAnthropicToken(reader, logger)
fmt.Fprintf(f, "\n# Anthropic token (Claude Code)\nANTHROPIC_TOKEN=%s\n", token)
fmt.Printf(" %s ok %s Added Anthropic provider to .env\n", colorGreen, colorReset)
}
}
if existing.codexToken == "" {
if token := api.DetectCodexToken(logger); token != "" {
fmt.Printf(" %s ok %s Codex auth token detected on this system\n", colorGreen, colorReset)
if promptYesNo(reader, "Enable Codex tracking?", true) {
fmt.Fprintf(f, "\n# Codex OAuth token\nCODEX_TOKEN=%s\n", token)
fmt.Printf(" %s ok %s Added Codex provider to .env (auto-detected)\n", colorGreen, colorReset)
}
} else if promptYesNo(reader, "Add Codex provider?", false) {
token := collectCodexToken(reader, logger)
fmt.Fprintf(f, "\n# Codex OAuth token\nCODEX_TOKEN=%s\n", token)
fmt.Printf(" %s ok %s Added Codex provider to .env\n", colorGreen, colorReset)
}
}
if !existing.antigravityEnabled {
if promptYesNo(reader, "Add Antigravity (Windsurf) provider?", false) {
fmt.Fprintf(f, "\n# Antigravity (Windsurf) - auto-detected from local process\nANTIGRAVITY_ENABLED=true\n")
fmt.Printf(" %s ok %s Added Antigravity provider to .env\n", colorGreen, colorReset)
}
}
if !existing.geminiEnabled {
// Try to detect Gemini CLI credentials
if _, err := os.Stat(filepath.Join(os.Getenv("HOME"), ".gemini", "oauth_creds.json")); err == nil {
fmt.Printf(" %s ok %s Gemini CLI credentials detected on this system\n", colorGreen, colorReset)
if promptYesNo(reader, "Enable Gemini tracking?", true) {
fmt.Fprintf(f, "\n# Gemini CLI - auto-detected from ~/.gemini/oauth_creds.json\nGEMINI_ENABLED=true\n")
fmt.Printf(" %s ok %s Added Gemini provider to .env (auto-detected)\n", colorGreen, colorReset)
}
} else if promptYesNo(reader, "Add Gemini CLI provider?", false) {
fmt.Fprintf(f, "\n# Gemini CLI - auto-detected from ~/.gemini/oauth_creds.json\nGEMINI_ENABLED=true\n")
fmt.Printf(" %s ok %s Added Gemini provider to .env\n", colorGreen, colorReset)
fmt.Printf(" %sNote: Install Gemini CLI and run 'gemini' to authenticate%s\n", colorDim, colorReset)
}
}
return nil
}
// --- Input helpers ---
func readLine(reader *bufio.Reader) string {
line, _ := reader.ReadString('\n')
return strings.TrimSpace(line)
}
func promptWithDefault(reader *bufio.Reader, prompt, defaultVal string) string {
fmt.Printf(" %s %s[%s]%s: ", prompt, colorDim, defaultVal, colorReset)
input := readLine(reader)
if input == "" {
return defaultVal
}
return input
}
func promptYesNo(reader *bufio.Reader, prompt string, defaultYes bool) bool {
suffix := "(y/N)"
if defaultYes {
suffix = "(Y/n)"
}
fmt.Printf(" %s %s: ", prompt, suffix)
input := strings.ToLower(readLine(reader))
if input == "" {
return defaultYes
}
return strings.HasPrefix(input, "y")
}
func promptSecret(reader *bufio.Reader, prompt string) string {
for {
fmt.Printf(" %s: ", prompt)
val := readLine(reader)
if val != "" {
masked := maskValue(val)
fmt.Printf(" %s ok %s %s%s%s\n", colorGreen, colorReset, colorDim, masked, colorReset)
return val
}
fmt.Printf(" %sCannot be empty%s\n", colorRed, colorReset)
}
}
func promptChoice(reader *bufio.Reader, prompt string, options []string) int {
fmt.Printf(" %s%s%s\n", colorBold, prompt, colorReset)
for i, opt := range options {
fmt.Printf(" %s%d)%s %s\n", colorCyan, i+1, colorReset, opt)
}
for {
fmt.Printf(" %s>%s ", colorBold, colorReset)
input := readLine(reader)
n, err := strconv.Atoi(input)
if err == nil && n >= 1 && n <= len(options) {
return n
}
fmt.Printf(" %sPlease enter 1-%d%s\n", colorRed, len(options), colorReset)
}
}
func maskValue(val string) string {
if len(val) > 10 {
return val[:6] + "..." + val[len(val)-4:]
}
if len(val) > 3 {
return val[:3] + "..."
}
return "***"
}
func generatePassword() string {
b := make([]byte, 6)
if _, err := rand.Read(b); err != nil {
// Fallback to timestamp-based
return fmt.Sprintf("onwatch%d", time.Now().UnixNano()%100000)
}
return hex.EncodeToString(b)
}