Files
gastown/internal/doctor/global_state_check.go
Sohail Mohammad 81bfe48ed3 feat: Set and Forget - Seamless Gas Town Integration (#255)
Adds shell integration for automatic Gas Town context detection.

Features:
- `gt enable` / `gt disable` - Global on/off switch
- `gt shell install|remove|status` - Shell integration management
- `gt rig quick-add [path]` - One-command project setup
- `gt uninstall` - Clean removal with options
- Shell hook auto-sets GT_TOWN_ROOT/GT_RIG on cd

Implementation:
- XDG-compliant state storage (~/.local/state/gastown/)
- Safe RC file manipulation with block markers
- Environment overrides (GASTOWN_DISABLED/ENABLED)
- Doctor check for global state validation

Co-authored-by: Sohail Mohammad <sohailm25@gmail.com>
2026-01-08 20:25:01 -08:00

119 lines
2.7 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"
"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 len(data) > 0 && contains(string(data), "Gas Town Integration")
}
func contains(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}