role: consolidate RoleContext into RoleInfo

Unified the two overlapping role detection structs:
- RoleContext (prime.go) is now a type alias for RoleInfo
- detectRole() now returns RoleInfo directly
- Added WorkDir field to RoleInfo
- GetRoleWithContext now populates WorkDir

This eliminates code duplication between prime.go and role.go while
maintaining backward compatibility through the type alias.
This commit is contained in:
Steve Yegge
2025-12-25 02:11:36 -08:00
parent 0280a6f945
commit 8f54d6c3c5
2 changed files with 11 additions and 11 deletions

View File

@@ -51,14 +51,9 @@ func init() {
rootCmd.AddCommand(primeCmd)
}
// RoleContext contains information about the detected role.
type RoleContext struct {
Role Role `json:"role"`
Rig string `json:"rig,omitempty"`
Polecat string `json:"polecat,omitempty"`
TownRoot string `json:"town_root"`
WorkDir string `json:"work_dir"`
}
// RoleContext is an alias for RoleInfo for backward compatibility.
// New code should use RoleInfo directly.
type RoleContext = RoleInfo
func runPrime(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
@@ -146,11 +141,12 @@ func runPrime(cmd *cobra.Command, args []string) error {
return nil
}
func detectRole(cwd, townRoot string) RoleContext {
ctx := RoleContext{
func detectRole(cwd, townRoot string) RoleInfo {
ctx := RoleInfo{
Role: RoleUnknown,
TownRoot: townRoot,
WorkDir: cwd,
Source: "cwd",
}
// Get relative path from town root

View File

@@ -18,6 +18,8 @@ const (
)
// RoleInfo contains information about a role and its detection source.
// This is the canonical struct for role detection - used by both GetRole()
// and detectRole() functions.
type RoleInfo struct {
Role Role `json:"role"`
Source string `json:"source"` // "env", "cwd", or "explicit"
@@ -28,6 +30,7 @@ type RoleInfo struct {
CwdRole Role `json:"cwd_role,omitempty"` // Role detected from cwd
Mismatch bool `json:"mismatch,omitempty"` // True if env != cwd detection
TownRoot string `json:"town_root,omitempty"`
WorkDir string `json:"work_dir,omitempty"` // Current working directory
}
var roleCmd = &cobra.Command{
@@ -131,6 +134,7 @@ func GetRole() (RoleInfo, error) {
func GetRoleWithContext(cwd, townRoot string) (RoleInfo, error) {
info := RoleInfo{
TownRoot: townRoot,
WorkDir: cwd,
}
// Check environment variable first
@@ -155,7 +159,7 @@ func GetRoleWithContext(cwd, townRoot string) (RoleInfo, error) {
info.Mismatch = true
}
} else {
// Fall back to cwd detection
// Fall back to cwd detection - copy all fields from cwdCtx
info.Role = cwdCtx.Role
info.Rig = cwdCtx.Rig
info.Polecat = cwdCtx.Polecat