Files
gastown/internal/doctor/global_state_check.go
max 5adb096d9d refactor(doctor): use strings.Contains instead of custom contains()
Replace the hand-rolled contains() function with the standard library
strings.Contains(). Also removes the redundant len(data) > 0 check
since strings.Contains handles empty strings correctly.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-08 20:26:23 -08:00

111 lines
2.6 KiB
Go

// ABOUTME: Doctor check for Gas Town global state configuration.
// ABOUTME: Validates that state directories and shell integration are properly configured.
package doctor
import (
"os"
"path/filepath"
"strings"
"github.com/steveyegge/gastown/internal/shell"
"github.com/steveyegge/gastown/internal/state"
)
type GlobalStateCheck struct {
BaseCheck
}
func NewGlobalStateCheck() *GlobalStateCheck {
return &GlobalStateCheck{
BaseCheck: BaseCheck{
CheckName: "global-state",
CheckDescription: "Validates Gas Town global state and shell integration",
},
}
}
func (c *GlobalStateCheck) Run(ctx *CheckContext) *CheckResult {
result := &CheckResult{
Name: c.Name(),
Status: StatusOK,
}
var details []string
var warnings []string
var errors []string
s, err := state.Load()
if err != nil {
if os.IsNotExist(err) {
result.Message = "Global state not initialized"
result.FixHint = "Run: gt enable"
result.Status = StatusWarning
return result
}
result.Message = "Cannot read global state"
result.Details = []string{err.Error()}
result.Status = StatusError
return result
}
if s.Enabled {
details = append(details, "Gas Town: enabled")
} else {
details = append(details, "Gas Town: disabled")
warnings = append(warnings, "Gas Town is disabled globally")
}
if s.Version != "" {
details = append(details, "Version: "+s.Version)
}
if s.MachineID != "" {
details = append(details, "Machine ID: "+s.MachineID)
}
rcPath := shell.RCFilePath(shell.DetectShell())
if hasShellIntegration(rcPath) {
details = append(details, "Shell integration: installed ("+rcPath+")")
} else {
warnings = append(warnings, "Shell integration not installed")
}
hookPath := filepath.Join(state.ConfigDir(), "shell-hook.sh")
if _, err := os.Stat(hookPath); err == nil {
details = append(details, "Hook script: present")
} else {
if hasShellIntegration(rcPath) {
errors = append(errors, "Hook script missing but shell integration installed")
}
}
result.Details = details
if len(errors) > 0 {
result.Status = StatusError
result.Message = errors[0]
result.FixHint = "Run: gt install --shell"
} else if len(warnings) > 0 {
result.Status = StatusWarning
result.Message = warnings[0]
if !s.Enabled {
result.FixHint = "Run: gt enable"
} else {
result.FixHint = "Run: gt install --shell"
}
} else {
result.Message = "Global state healthy"
}
return result
}
func hasShellIntegration(rcPath string) bool {
data, err := os.ReadFile(rcPath)
if err != nil {
return false
}
return strings.Contains(string(data), "Gas Town Integration")
}