refactor(cmd): extract common manager creation boilerplate
Add getRig() helper in rig_helpers.go that encapsulates the common boilerplate for finding town root, loading rigs config, and retrieving a rig. This reduces duplication across 5 get*Manager functions: - getPolecatManager - getSessionManager - getCrewManager - getRefineryManager - getWitnessManager Closes gt-7sqi. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
36
internal/cmd/rig_helpers.go
Normal file
36
internal/cmd/rig_helpers.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/steveyegge/gastown/internal/config"
|
||||
"github.com/steveyegge/gastown/internal/git"
|
||||
"github.com/steveyegge/gastown/internal/rig"
|
||||
"github.com/steveyegge/gastown/internal/workspace"
|
||||
)
|
||||
|
||||
// getRig finds the town root and retrieves the specified rig.
|
||||
// This is the common boilerplate extracted from get*Manager functions.
|
||||
// Returns the town root path and rig instance.
|
||||
func getRig(rigName string) (string, *rig.Rig, error) {
|
||||
townRoot, err := workspace.FindFromCwdOrError()
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("not in a Gas Town workspace: %w", err)
|
||||
}
|
||||
|
||||
rigsConfigPath := filepath.Join(townRoot, "mayor", "rigs.json")
|
||||
rigsConfig, err := config.LoadRigsConfig(rigsConfigPath)
|
||||
if err != nil {
|
||||
rigsConfig = &config.RigsConfig{Rigs: make(map[string]config.RigEntry)}
|
||||
}
|
||||
|
||||
g := git.NewGit(townRoot)
|
||||
rigMgr := rig.NewManager(townRoot, rigsConfig, g)
|
||||
r, err := rigMgr.GetRig(rigName)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("rig '%s' not found", rigName)
|
||||
}
|
||||
|
||||
return townRoot, r, nil
|
||||
}
|
||||
Reference in New Issue
Block a user