golang-cli

Standardize Go CLI development with Cobra, Viper, and signal handling.

4|Updated May 17, 2026
One-click install
npx skills add https://github.com/hellopoisonx/aim --skill golang-cli-hellopoisonx
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/hellopoisonx/aim/tree/main/skills/golang-cli
Command: npx skills add https://github.com/hellopoisonx/aim --skill golang-cli-hellopoisonx

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes assets (resource) and references (resource) components.

What problem does it solve?

This Skill helps you develop, extend, or audit Go command-line tools with predictable behavior, correct configuration handling, and automation-friendly I/O so your CLI works reliably in scripts and pipelines.

Core Features & Use Cases

  • Command design & structure: build consistent Cobra command trees with proper root/subcommand organization and minimal main() entry points.
  • Flags, validation, and completions: implement robust flag binding, required/constraint rules, argument validators, and shell completion support.
  • Configuration layering with Viper: ensure correct precedence across flags, environment variables, and config files while handling missing config safely.
  • Operational correctness: enforce stdout/stderr discipline, Unix exit code conventions, version injection via ldflags, and graceful cancellation via signal.NotifyContext.
  • Testing patterns: test commands by executing them programmatically and capturing output using Cobra’s output writers for deterministic assertions.

Quick Start

Ask the agent to build a Cobra-based Go CLI with a root command, add a serve subcommand, initialize Viper in PersistentPreRunE, bind flags to Viper, implement argument validation, support shell completion, and ensure logs go to stderr while results go to stdout.

Frequently Asked Questions about golang-cli

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a Go CLI with Cobra so it handles configuration and output correctly?

To structure a Go CLI with Cobra, you build consistent command trees and initialize Viper in `PersistentPreRunE` to handle configuration layering. This ensures proper flag binding, stdout/stderr separation, and predictable behavior under automation.

What is the correct way to manage Viper configuration precedence in a Go CLI?

Viper configuration precedence in a Go CLI is managed by layering flags, environment variables, and config files. Applying this layering ensures correct overrides while safely handling missing config files without breaking command execution.

How do I handle graceful cancellation and signal handling in a Go command-line tool?

Graceful cancellation in a Go command-line tool is handled using `signal.NotifyContext`. This mechanism intercepts interruption signals, allowing your Cobra commands to clean up resources and exit gracefully during operational automation.

Why should stdout and stderr be separated in Unix CLI tools, and how is it tested?

Stdout and stderr separation in Unix CLI tools ensures logs go to stderr while results go to stdout, preventing pipeline corruption. It is tested by executing commands programmatically and capturing output using Cobra's output writers for deterministic assertions.

Can I inject version information into a Go CLI during the build process?

You can inject version information into a Go CLI during the build process using `ldflags`. This approach passes version variables at compilation time, ensuring your command-line tool reports accurate versioning natively.

Does this approach support adding shell completion to an existing Cobra-based CLI?

Yes, this approach supports adding shell completion to an existing Cobra-based CLI. It provides guidance for implementing robust flag binding, argument validators, and shell completion setup to extend your current command structure.