Files
gastown/internal/cmd/init.go
Steve Yegge 4048cdc373 fix(lint): resolve all errcheck warnings
Fix ~50 errcheck warnings across the codebase:

- Add explicit `_ =` for intentionally ignored error returns (cleanup,
  best-effort operations, etc.)
- Use `defer func() { _ = ... }()` pattern for defer statements
- Handle tmux SetEnvironment, KillSession, SendKeysRaw returns
- Handle mail router.Send returns
- Handle os.RemoveAll, os.Rename in cleanup paths
- Handle rand.Read returns for ID generation
- Handle fmt.Fprint* returns when writing to io.Writer
- Fix for-select with single case to use for-range
- Handle cobra MarkFlagRequired returns

All tests pass. Code compiles without errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-19 12:44:42 -08:00

129 lines
3.4 KiB
Go

package cmd
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/gastown/internal/git"
"github.com/steveyegge/gastown/internal/rig"
"github.com/steveyegge/gastown/internal/style"
)
var initForce bool
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize current directory as a Gas Town rig",
Long: `Initialize the current directory for use as a Gas Town rig.
This creates the standard agent directories (polecats/, witness/, refinery/,
mayor/) and updates .git/info/exclude to ignore them.
The current directory must be a git repository. Use --force to reinitialize
an existing rig structure.`,
RunE: runInit,
}
func init() {
initCmd.Flags().BoolVarP(&initForce, "force", "f", false, "Reinitialize existing structure")
rootCmd.AddCommand(initCmd)
}
func runInit(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("getting current directory: %w", err)
}
// Check if it's a git repository
g := git.NewGit(cwd)
if _, err := g.CurrentBranch(); err != nil {
return fmt.Errorf("not a git repository (run 'git init' first)")
}
// Check if already initialized
polecatsDir := filepath.Join(cwd, "polecats")
if _, err := os.Stat(polecatsDir); err == nil && !initForce {
return fmt.Errorf("rig already initialized (use --force to reinitialize)")
}
fmt.Printf("%s Initializing Gas Town rig in %s\n\n",
style.Bold.Render("⚙️"), style.Dim.Render(cwd))
// Create agent directories
created := 0
for _, dir := range rig.AgentDirs {
dirPath := filepath.Join(cwd, dir)
if err := os.MkdirAll(dirPath, 0755); err != nil {
return fmt.Errorf("creating %s: %w", dir, err)
}
// Create .gitkeep to ensure directory is tracked if needed
gitkeep := filepath.Join(dirPath, ".gitkeep")
if _, err := os.Stat(gitkeep); os.IsNotExist(err) {
_ = os.WriteFile(gitkeep, []byte(""), 0644)
}
fmt.Printf(" ✓ Created %s/\n", dir)
created++
}
// Update .git/info/exclude
if err := updateGitExclude(cwd); err != nil {
fmt.Printf(" %s Could not update .git/info/exclude: %v\n",
style.Dim.Render("⚠"), err)
} else {
fmt.Printf(" ✓ Updated .git/info/exclude\n")
}
fmt.Printf("\n%s Rig initialized with %d directories.\n",
style.Bold.Render("✓"), created)
fmt.Println()
fmt.Println("Next steps:")
fmt.Printf(" 1. Add this rig to a town: %s\n",
style.Dim.Render("gt rig add <name> <git-url>"))
fmt.Printf(" 2. Create a polecat: %s\n",
style.Dim.Render("gt polecat add <name>"))
return nil
}
func updateGitExclude(repoPath string) error {
excludePath := filepath.Join(repoPath, ".git", "info", "exclude")
// Ensure directory exists
excludeDir := filepath.Dir(excludePath)
if err := os.MkdirAll(excludeDir, 0755); err != nil {
return fmt.Errorf("creating .git/info: %w", err)
}
// Read existing content
content, err := os.ReadFile(excludePath)
if err != nil && !os.IsNotExist(err) {
return err
}
// Check if already has Gas Town section
if strings.Contains(string(content), "Gas Town") {
return nil // Already configured
}
// Append agent dirs
additions := "\n# Gas Town agent directories\n"
for _, dir := range rig.AgentDirs {
// Get first component (e.g., "polecats" from "polecats")
// or "refinery" from "refinery/rig"
base := filepath.Dir(dir)
if base == "." {
base = dir
}
additions += base + "/\n"
}
// Write back
return os.WriteFile(excludePath, append(content, []byte(additions)...), 0644)
}