Skip to content

[plan] Migrate all commands from Run to RunE for idiomatic error handling #5282

@github-actions

Description

@github-actions

Objective

Migrate all command implementations from using Run to RunE for more idiomatic error handling across the entire CLI codebase.

Context

Current State: 107 commands use Run with manual error handling, only 4 use RunE

This migration will eliminate repetitive error handling code and adopt Go best practices for error propagation in CLI applications.

Current Pattern (Anti-pattern)

Run: func(cmd *cobra.Command, args []string) {
    if err := cli.NewWorkflow(...); err != nil {
        fmt.Fprintln(os.Stderr, console.FormatErrorMessage(err.Error()))
        os.Exit(1)
    }
}

Target Pattern (Idiomatic)

RunE: func(cmd *cobra.Command, args []string) error {
    return cli.NewWorkflow(...)
}

Approach

  1. Create error formatter wrapper in cmd/gh-aw/main.go:

    rootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
        fmt.Fprintln(os.Stderr, console.FormatErrorMessage(err.Error()))
        return err
    })
  2. Migrate commands in batches to make review manageable:

    • Batch 1: Root command and compile commands
    • Batch 2: Workflow commands (run, status, logs, audit)
    • Batch 3: MCP commands
    • Batch 4: Development utilities
    • Batch 5: Remaining commands
  3. Update each command:

    • Change Run: to RunE:
    • Remove os.Exit(1) calls
    • Return errors directly
    • Keep console formatting for now (can standardize later)
  4. Test each batch to ensure error handling still works correctly

Files to Modify

Core files (16+ files):

  • cmd/gh-aw/main.go - Root command and inline commands
  • pkg/cli/compile_command.go
  • pkg/cli/run_command.go
  • pkg/cli/status_command.go
  • pkg/cli/logs.go
  • pkg/cli/audit_command.go
  • pkg/cli/mcp_command.go
  • pkg/cli/mcp_inspect_command.go
  • pkg/cli/mcp_list_tools_command.go
  • pkg/cli/pr_command.go
  • pkg/cli/trial_command.go
  • pkg/cli/workflow_dispatch_command.go
  • All other command files in pkg/cli/

Acceptance Criteria

  • All commands use RunE instead of Run
  • No os.Exit(1) calls in command functions
  • Errors are returned directly to Cobra
  • Error formatting still uses console.FormatErrorMessage
  • All existing tests pass
  • Error messages remain user-friendly
  • Exit codes are correct (non-zero on errors)

Benefits

  • ✅ More idiomatic Go error handling
  • ✅ Eliminates 107 instances of repetitive error handling
  • ✅ Cleaner, more maintainable code
  • ✅ Consistent error propagation pattern
  • ✅ Easier to add error context in future

Notes

  • This is the highest impact improvement identified in the Go Fan analysis
  • Changes affect the entire CLI codebase
  • Breaking this into batches will make code review manageable
  • Can be done without changing external behavior

AI generated by Plan Command for discussion #5271

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions