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

View File

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