Files
beads/cmd/bd/direct_mode.go
Steve Yegge f91d4fa975 Refactor: Introduce CommandContext to consolidate global variables (bd-qobn)
This addresses the code smell of 20+ global variables in main.go by:

1. Creating CommandContext struct in context.go that groups all runtime state:
   - Configuration (DBPath, Actor, JSONOutput, etc.)
   - Runtime state (Store, DaemonClient, HookRunner, etc.)
   - Auto-flush/import state
   - Version tracking
   - Profiling handles

2. Adding accessor functions (getStore, getActor, getDaemonClient, etc.)
   that provide backward-compatible access to the state while allowing
   gradual migration to CommandContext.

3. Updating direct_mode.go to demonstrate the migration pattern using
   accessor functions instead of direct global access.

4. Adding test isolation helpers (ensureCleanGlobalState, enableTestModeGlobals)
   to prevent test interference when multiple tests manipulate global state.

Benefits:
- Reduces global count from 20+ to 1 (cmdCtx)
- Better testability (can inject mock contexts)
- Clearer state ownership (all state in one place)
- Thread safety (mutexes grouped with the data they protect)

Note: Two pre-existing test failures (TestTrackBdVersion_*) are unrelated to
this change and fail both with and without these modifications.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-30 16:29:28 -08:00

114 lines
3.0 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/storage/sqlite"
)
// ensureDirectMode makes sure the CLI is operating in direct-storage mode.
// If the daemon is active, it is cleanly disconnected and the shared store is opened.
func ensureDirectMode(reason string) error {
if getDaemonClient() != nil {
if err := fallbackToDirectMode(reason); err != nil {
return err
}
return nil
}
return ensureStoreActive()
}
// fallbackToDirectMode disables the daemon client and ensures a local store is ready.
func fallbackToDirectMode(reason string) error {
disableDaemonForFallback(reason)
return ensureStoreActive()
}
// disableDaemonForFallback closes the daemon client and updates status metadata.
func disableDaemonForFallback(reason string) {
if client := getDaemonClient(); client != nil {
_ = client.Close()
setDaemonClient(nil)
}
ds := getDaemonStatus()
ds.Mode = "direct"
ds.Connected = false
ds.Degraded = true
if reason != "" {
ds.Detail = reason
}
if ds.FallbackReason == FallbackNone {
ds.FallbackReason = FallbackDaemonUnsupported
}
setDaemonStatus(ds)
if reason != "" {
debug.Logf("Debug: %s\n", reason)
}
}
// ensureStoreActive guarantees that a local SQLite store is initialized and tracked.
func ensureStoreActive() error {
lockStore()
active := isStoreActive() && getStore() != nil
unlockStore()
if active {
return nil
}
path := getDBPath()
if path == "" {
if found := beads.FindDatabasePath(); found != "" {
setDBPath(found)
path = found
} else {
// Check if this is a JSONL-only project
beadsDir := beads.FindBeadsDir()
if beadsDir != "" {
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
if _, err := os.Stat(jsonlPath); err == nil {
// JSONL exists - check if no-db mode is configured
if isNoDbModeConfigured(beadsDir) {
return fmt.Errorf("this project uses JSONL-only mode (no SQLite database).\n" +
"Hint: use 'bd --no-db <command>' or set 'no-db: true' in config.yaml")
}
// JSONL exists but no-db not configured - fresh clone scenario
return fmt.Errorf("found JSONL file but no database: %s\n"+
"Hint: run 'bd init' to create the database and import issues,\n"+
" or use 'bd --no-db' for JSONL-only mode", jsonlPath)
}
}
return fmt.Errorf("no beads database found.\n" +
"Hint: run 'bd init' to create a database in the current directory,\n" +
" or use 'bd --no-db' for JSONL-only mode")
}
}
sqlStore, err := sqlite.New(getRootContext(), path)
if err != nil {
// Check for fresh clone scenario
if isFreshCloneError(err) {
beadsDir := filepath.Dir(path)
handleFreshCloneError(err, beadsDir)
return fmt.Errorf("database not initialized")
}
return fmt.Errorf("failed to open database: %w", err)
}
lockStore()
setStore(sqlStore)
setStoreActive(true)
unlockStore()
if isAutoImportEnabled() {
autoImportIfNewer()
}
return nil
}