← ClaudeAtlas

cobra-viperlisted

Expert skill for building CLI applications with Cobra and Viper, authored by spf13 — the original creator of both libraries. Covers command-first architecture, decoupled business logic, configuration management, environment variable binding, context-aware commands, and in-memory CLI testing. Use when building or reviewing any Go CLI application that uses Cobra and/or Viper.
chafingdishposition355/go-skills · ★ 1 · AI & Automation · score 64
Install: claude install-skill chafingdishposition355/go-skills
# Go CLI Architecture: Cobra & Viper Idiomatic patterns and best practices for building robust, configuration-driven command-line interfaces using Cobra and Viper. ## When to Activate - Writing a new CLI application in Go - Adding commands, subcommands, or flags to an existing Cobra application - Integrating Viper for configuration file, environment variable, or flag management - Reviewing or refactoring CLI code that uses Cobra and/or Viper - Designing the command structure or configuration schema for a CLI tool - Testing CLI commands ## Core Philosophy ### The Command-First Architecture Treat your application binary as a router for commands. The CLI framework (Cobra) should solely handle flags, arguments, and routing. Your core business logic should remain completely unaware of the CLI layer, making it highly testable and reusable. ### Unified Configuration Configuration should be environment-aware and unified. Viper acts as the single source of truth, merging defaults, config files, environment variables, and command-line flags into a cohesive state before passing it to the application logic. ## CLI Package Organization **Anti-Pattern:** Hiding all your commands and core logic deep inside an `internal/` directory tree, or shoving everything into `main.go`. ### Discoverable, Flat Structures Command routing and business logic should live in standard, logically named packages. The `cmd/` package handles the CLI surface area, while other top-level packages handle t