golang-cli

Implement Cobra/Viper Go CLIs with Unix I/O and signal handling.

2|Updated Feb 12, 2024
One-click install
npx skills add https://github.com/adibfirman/dotfiles --skill golang-cli-adibfirman
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/adibfirman/dotfiles/tree/main/claude/.claude/skills/technical/golang/golang-cli
Command: npx skills add https://github.com/adibfirman/dotfiles --skill golang-cli-adibfirman

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Golang-cli helps you design Go command-line applications that behave correctly in real Unix environments—so flags, config layering, output, exit codes, and signal handling work reliably under automation and piping.

Core Features & Use Cases

  • Cobra-based command architecture: Structure root commands, subcommands, and flags cleanly for maintainable CLI growth.
  • Viper configuration layering: Ensure precedence across CLI flags, environment variables, config files, and defaults with safe handling when config is missing.
  • Production-grade CLI behavior: Enforce stdout/stderr separation, consistent exit code conventions, version injection via ldflags, and graceful shutdown using context-aware signal handling.
  • Shell completions and CLI testing patterns: Generate completions via Cobra’s built-in generators and test commands by executing in-process while capturing outputs.

Quick Start

Use the golang-cli skill to scaffold a Cobra-based Go CLI root command with Viper config initialization, stdout/stderr discipline, a version command, and a sample subcommand in a way that your AI coding agent can extend safely.

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 and Viper for config layering?

Structure a Go CLI with Cobra and Viper by defining root commands and subcommands, then establishing config precedence across CLI flags, environment variables, config files, and defaults with safe handling when config is missing.

How does Viper configuration layering handle precedence in Go CLIs?

Viper configuration layering enforces precedence across CLI flags, environment variables, config files, and defaults, ensuring deterministic behavior and safe handling when config files are missing from the environment.

How do I handle signal cancellation and exit codes in a Go CLI?

Handle signal cancellation and exit codes in a Go CLI using context-aware signal handling with signal.NotifyContext for graceful shutdown, enforcing consistent exit code conventions and stdout/stderr separation.

What's the best way to inject version information into a Go CLI binary?

Inject version information into a Go CLI binary using ldflags version injection during the build process, allowing deterministic version exposure alongside standard command execution and output stream discipline.

Can I generate shell completions and test Cobra commands in-process?

Generate shell completions via Cobra's built-in generators and test commands by executing them in-process while capturing outputs, ensuring predictable CLI structure without external process dependencies.

Why does my Go CLI fail when piped under Unix automation environments?

Go CLI applications fail under Unix automation when stdout/stderr separation is not enforced and signal handling lacks context cancellation, preventing graceful shutdown during piping and automated execution.