Files
cburn/cmd/setup.go
teernisse 5aa184a7ab feat: add CLI commands for usage analysis, cost breakdown, and setup
Implement the complete Cobra command tree (11 subcommands):

- cmd/root.go: Root command with persistent flags (--days, --project,
  --model, --no-cache, --data-dir, --quiet, --no-subagents). Shared
  loadData() orchestrates the full pipeline: tries cache-assisted
  loading first, falls back to uncached parse on cache failure,
  reports progress to stderr. applyFilters() applies project/model
  substring filters and computes the time window.

- cmd/summary.go: Default command (also "cburn summary"). Renders a
  bordered metrics table with token breakdown (5 types), cost with
  cache savings, and per-day rates with period-over-period deltas.

- cmd/costs.go: Detailed cost analysis — breaks down costs by token
  type (output, cache_write_1h, input, cache_write_5m, cache_read)
  with share percentages, period comparison bar chart, and per-model
  cost breakdown (input/output/cache/total columns).

- cmd/daily.go: Daily usage table (date, weekday, sessions, prompts,
  tokens, cost) sorted most-recent-first.

- cmd/hourly.go: Activity heatmap showing prompt distribution across
  24 hours with Unicode block bars, reports peak hour.

- cmd/models.go: Model usage ranking with API call counts, token
  volumes, cost, and usage share percentage.

- cmd/projects.go: Project ranking by cost with session/prompt/token
  counts.

- cmd/sessions.go: Session list sorted by recency with --limit flag
  (default 20). Shows start time, project, duration, tokens, cost.
  Marks subagent sessions with "(sub)" suffix.

- cmd/config_cmd.go: Displays current configuration across all
  sections (general, admin API, appearance, budget) with auto-
  detected plan ceiling.

- cmd/setup.go: Interactive first-run wizard — configures Admin API
  key, default time range (7/30/90 days), and color theme (Flexoki
  Dark, Catppuccin Mocha, Tokyo Night, Terminal). Saves to
  ~/.config/cburn/config.toml.

- cmd/tui.go: Launches the interactive Bubble Tea TUI dashboard,
  passing through all filter flags and applying the configured theme.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-19 13:02:33 -05:00

118 lines
2.5 KiB
Go

package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"cburn/internal/config"
"cburn/internal/source"
"github.com/spf13/cobra"
)
var setupCmd = &cobra.Command{
Use: "setup",
Short: "First-time setup wizard",
RunE: runSetup,
}
func init() {
rootCmd.AddCommand(setupCmd)
}
func runSetup(_ *cobra.Command, _ []string) error {
reader := bufio.NewReader(os.Stdin)
// Load existing config or defaults
cfg, _ := config.Load()
// Count sessions
files, _ := source.ScanDir(flagDataDir)
projectCount := source.CountProjects(files)
fmt.Println()
fmt.Println(" Welcome to cburn!")
fmt.Println()
if len(files) > 0 {
fmt.Printf(" Found %s sessions in %s (%d projects)\n\n",
formatNumber(int64(len(files))), flagDataDir, projectCount)
}
// 1. API key
fmt.Println(" 1. Anthropic Admin API key")
fmt.Println(" For real cost data from the billing API.")
existing := config.GetAdminAPIKey(cfg)
if existing != "" {
fmt.Printf(" Current: %s\n", maskAPIKey(existing))
}
fmt.Print(" > ")
apiKey, _ := reader.ReadString('\n')
apiKey = strings.TrimSpace(apiKey)
if apiKey != "" {
cfg.AdminAPI.APIKey = apiKey
}
fmt.Println()
// 2. Default time range
fmt.Println(" 2. Default time range")
fmt.Println(" (1) 7 days")
fmt.Println(" (2) 30 days [default]")
fmt.Println(" (3) 90 days")
fmt.Print(" > ")
choice, _ := reader.ReadString('\n')
choice = strings.TrimSpace(choice)
switch choice {
case "1":
cfg.General.DefaultDays = 7
case "3":
cfg.General.DefaultDays = 90
default:
cfg.General.DefaultDays = 30
}
fmt.Println()
// 3. Theme
fmt.Println(" 3. Color theme")
fmt.Println(" (1) Flexoki Dark [default]")
fmt.Println(" (2) Catppuccin Mocha")
fmt.Println(" (3) Tokyo Night")
fmt.Println(" (4) Terminal (ANSI 16)")
fmt.Print(" > ")
themeChoice, _ := reader.ReadString('\n')
themeChoice = strings.TrimSpace(themeChoice)
switch themeChoice {
case "2":
cfg.Appearance.Theme = "catppuccin-mocha"
case "3":
cfg.Appearance.Theme = "tokyo-night"
case "4":
cfg.Appearance.Theme = "terminal"
default:
cfg.Appearance.Theme = "flexoki-dark"
}
// Save
if err := config.Save(cfg); err != nil {
return fmt.Errorf("saving config: %w", err)
}
fmt.Println()
fmt.Printf(" Saved to %s\n", config.ConfigPath())
fmt.Println(" Run `cburn setup` anytime to reconfigure.")
fmt.Println()
return nil
}
func maskAPIKey(key string) string {
if len(key) > 16 {
return key[:8] + "..." + key[len(key)-4:]
}
if len(key) > 4 {
return key[:4] + "..."
}
return "****"
}