cobra-scaffolder

Generate Cobra/Viper-based Go CLI applications with persistent flags, subcommands, and configuration management. Creates production-ready Go CLI with modern patterns.

509 stars

Best use case

cobra-scaffolder is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Generate Cobra/Viper-based Go CLI applications with persistent flags, subcommands, and configuration management. Creates production-ready Go CLI with modern patterns.

Teams using cobra-scaffolder should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/cobra-scaffolder/SKILL.md --create-dirs "https://raw.githubusercontent.com/a5c-ai/babysitter/main/library/specializations/cli-mcp-development/skills/cobra-scaffolder/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/cobra-scaffolder/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How cobra-scaffolder Compares

Feature / Agentcobra-scaffolderStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Generate Cobra/Viper-based Go CLI applications with persistent flags, subcommands, and configuration management. Creates production-ready Go CLI with modern patterns.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Cobra Scaffolder

Generate a complete Cobra CLI application with Viper configuration, proper Go module structure, and best practices.

## Capabilities

- Generate Go-based Cobra CLI projects
- Create command hierarchy with persistent and local flags
- Integrate Viper for configuration management
- Set up automatic environment variable binding
- Implement shell completion generation
- Configure go.mod and build workflows

## Usage

Invoke this skill when you need to:
- Bootstrap a new CLI application using Cobra
- Create a Go CLI with hierarchical commands
- Integrate Viper for multi-source configuration
- Build cross-platform native binaries

## Inputs

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| projectName | string | Yes | Name of the CLI project (kebab-case) |
| modulePath | string | Yes | Go module path (e.g., github.com/user/project) |
| description | string | Yes | Short description of the CLI |
| commands | array | No | List of commands to scaffold |
| useViper | boolean | No | Integrate Viper config (default: true) |
| useCobra | boolean | No | Use cobra-cli generator patterns (default: true) |

### Command Structure

```json
{
  "commands": [
    {
      "name": "serve",
      "description": "Start the server",
      "persistentFlags": [
        { "name": "config", "shorthand": "c", "type": "string", "usage": "config file path" }
      ],
      "flags": [
        { "name": "port", "shorthand": "p", "type": "int", "default": 8080, "usage": "port to listen on" }
      ],
      "subcommands": ["start", "stop", "status"]
    }
  ]
}
```

## Output Structure

```
<projectName>/
├── go.mod
├── go.sum
├── main.go
├── README.md
├── .goreleaser.yaml
├── cmd/
│   ├── root.go              # Root command with Viper
│   ├── serve.go             # Serve command
│   ├── version.go           # Version command
│   └── completion.go        # Completion command
├── internal/
│   ├── config/
│   │   └── config.go        # Configuration structures
│   ├── server/
│   │   └── server.go        # Server implementation
│   └── logger/
│       └── logger.go        # Logging setup
├── pkg/
│   └── utils/
│       └── helpers.go       # Public utilities
└── tests/
    └── cmd/
        └── root_test.go
```

## Generated Code Patterns

### Root Command (cmd/root.go)

```go
package cmd

import (
    "fmt"
    "os"

    "github.com/spf13/cobra"
    "github.com/spf13/viper"
)

var cfgFile string

var rootCmd = &cobra.Command{
    Use:   "<projectName>",
    Short: "<description>",
    Long:  `<long description>`,
    PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
        return initConfig()
    },
}

func Execute() {
    if err := rootCmd.Execute(); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

func init() {
    rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
        "config file (default is $HOME/.<projectName>.yaml)")
    rootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")

    viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
}

func initConfig() error {
    if cfgFile != "" {
        viper.SetConfigFile(cfgFile)
    } else {
        home, err := os.UserHomeDir()
        cobra.CheckErr(err)

        viper.AddConfigPath(home)
        viper.SetConfigType("yaml")
        viper.SetConfigName(".<projectName>")
    }

    viper.AutomaticEnv()
    viper.SetEnvPrefix("<PROJECT_NAME>")

    if err := viper.ReadInConfig(); err == nil {
        fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
    }
    return nil
}
```

### Command Template (cmd/serve.go)

```go
package cmd

import (
    "fmt"

    "github.com/spf13/cobra"
    "github.com/spf13/viper"
)

var serveCmd = &cobra.Command{
    Use:   "serve",
    Short: "Start the server",
    Long:  `Start the server with the specified configuration.`,
    RunE: func(cmd *cobra.Command, args []string) error {
        port := viper.GetInt("port")
        host := viper.GetString("host")

        fmt.Printf("Starting server on %s:%d\n", host, port)
        return nil
    },
}

func init() {
    rootCmd.AddCommand(serveCmd)

    serveCmd.Flags().IntP("port", "p", 8080, "port to listen on")
    serveCmd.Flags().String("host", "localhost", "host to bind to")

    viper.BindPFlag("port", serveCmd.Flags().Lookup("port"))
    viper.BindPFlag("host", serveCmd.Flags().Lookup("host"))
}
```

## Dependencies

```go
module github.com/user/<projectName>

go 1.21

require (
    github.com/spf13/cobra v1.8.0
    github.com/spf13/viper v1.18.0
)
```

## Workflow

1. **Validate inputs** - Check project name, module path
2. **Create directory structure** - Set up Go project layout
3. **Generate go.mod** - Configure module and dependencies
4. **Create root command** - Viper integration and global flags
5. **Generate commands** - Individual command files
6. **Create internal packages** - Config, logger, etc.
7. **Set up goreleaser** - Cross-platform build config
8. **Create tests** - Command tests with cobra testing

## Best Practices Applied

- Standard Go project layout
- Viper for configuration management
- Persistent flags for shared options
- Environment variable binding
- Built-in completion generation
- Goreleaser for distribution

## References

- Cobra Documentation: https://cobra.dev/
- Cobra GitHub: https://github.com/spf13/cobra
- Viper GitHub: https://github.com/spf13/viper
- Go Project Layout: https://github.com/golang-standards/project-layout

## Target Processes

- cli-application-bootstrap
- cli-command-structure-design
- configuration-management-system

Related Skills

yargs-scaffolder

509
from a5c-ai/babysitter

Generate Yargs-based CLI applications with commands, positional args, middleware, and TypeScript support. Creates a complete scaffolded CLI application with modern patterns.

textual-scaffolder

509
from a5c-ai/babysitter

Generate Textual (Python) TUI application structure with widgets, screens, and CSS styling.

oclif-scaffolder

509
from a5c-ai/babysitter

Generate oclif CLI framework projects with plugin support, topics, hooks, and TypeScript. Creates enterprise-grade CLI applications with extensibility.

commander-js-scaffolder

509
from a5c-ai/babysitter

Generate Commander.js CLI project structure with TypeScript, commands, options, and best practices. Creates a complete scaffolded CLI application ready for development.

click-scaffolder

509
from a5c-ai/babysitter

Generate Click-based Python CLI applications with decorators, groups, context, and modern Python patterns. Creates complete scaffolded CLI with proper project structure.

clap-scaffolder

509
from a5c-ai/babysitter

Generate Clap-based Rust CLI applications with derive macros, subcommands, and modern Rust patterns. Creates production-ready Rust CLI with proper cargo structure.

bubble-tea-scaffolder

509
from a5c-ai/babysitter

Generate Bubble Tea (Go) TUI application structure with models, commands, and views using the Elm architecture.

bats-test-scaffolder

509
from a5c-ai/babysitter

Generate BATS test structure and fixtures for shell script testing with setup/teardown, assertions, and mocking.

argparse-scaffolder

509
from a5c-ai/babysitter

Generate argparse-based Python CLI applications with subparsers, type converters, and standard library patterns. Creates lightweight Python CLIs without external dependencies.

process-builder

509
from a5c-ai/babysitter

Scaffold new babysitter process definitions following SDK patterns, proper structure, and best practices. Guides the 3-phase workflow from research to implementation.

Workflow & Productivity

babysitter

509
from a5c-ai/babysitter

Orchestrate via @babysitter. Use this skill when asked to babysit a run, orchestrate a process or whenever it is called explicitly. (babysit, babysitter, orchestrate, orchestrate a run, workflow, etc.)

yolo

509
from a5c-ai/babysitter

Run Babysitter autonomously with minimal manual interruption.