Add local-first refactor foundation

This commit is contained in:
2026-05-25 00:45:46 -04:00
parent 161e871c53
commit 517c6b987b
45 changed files with 11545 additions and 697 deletions

View File

@@ -8,6 +8,8 @@ import (
"time"
"github.com/AmanTahiliani/box-box/internal/api"
"github.com/AmanTahiliani/box-box/internal/ingest"
"github.com/AmanTahiliani/box-box/internal/store"
"github.com/AmanTahiliani/box-box/internal/ui"
"github.com/AmanTahiliani/box-box/internal/web"
tea "github.com/charmbracelet/bubbletea"
@@ -16,6 +18,11 @@ import (
func main() {
webMode := flag.Bool("web", false, "Start web companion server instead of TUI")
port := flag.Int("port", 8080, "Port for web server (used with --web)")
ingestYear := flag.Int("ingest-year", 0, "Ingest OpenF1 meetings for a season year")
ingestMeeting := flag.Int("ingest-meeting", 0, "Ingest OpenF1 sessions for a meeting key")
ingestSession := flag.Int("ingest-session", 0, "Ingest Race Hub datasets for a session key")
dryRun := flag.Bool("dry-run", false, "Preview ingestion without writing domain rows")
dbPath := flag.String("db", "", "Domain database path (default: ~/.local/share/box-box/boxbox.db)")
flag.Parse()
var client *api.OpenF1Client
@@ -29,6 +36,28 @@ func main() {
// Clean up old file-based cache (one-time migration).
go api.CleanupOldFileCache()
ingestFlags := 0
if *ingestYear != 0 {
ingestFlags++
}
if *ingestMeeting != 0 {
ingestFlags++
}
if *ingestSession != 0 {
ingestFlags++
}
if ingestFlags > 0 {
if ingestFlags > 1 {
fmt.Fprintln(os.Stderr, "box-box: only one of --ingest-year, --ingest-meeting, or --ingest-session may be set")
os.Exit(1)
}
if err := runIngestion(client, *ingestYear, *ingestMeeting, *ingestSession, *dryRun, *dbPath); err != nil {
fmt.Fprintf(os.Stderr, "box-box ingest error: %v\n", err)
os.Exit(1)
}
return
}
if *webMode {
log.SetOutput(os.Stderr) // web mode logs to stderr, not file
fmt.Printf("box-box web → http://localhost:%d\n", *port)
@@ -56,3 +85,34 @@ func main() {
os.Exit(1)
}
}
func runIngestion(client *api.OpenF1Client, year, meetingKey, sessionKey int, dryRun bool, dbPath string) error {
log.SetOutput(os.Stderr)
path := dbPath
if path == "" {
path = store.DefaultDBPath()
}
st, err := store.Open(path)
if err != nil {
return fmt.Errorf("open domain database: %w", err)
}
defer st.Close()
opts := ingest.DefaultOptions()
opts.DryRun = dryRun
opts.Progress = ingest.NewProgress(os.Stderr)
svc := ingest.NewService(st, ingest.NewOpenF1Source(client), opts)
switch {
case year != 0:
_, err = svc.IngestYear(year)
case meetingKey != 0:
_, err = svc.IngestMeeting(meetingKey)
case sessionKey != 0:
_, err = svc.IngestSession(sessionKey)
}
return err
}