golang-cli

Builds and reviews Go CLI applications using Cobra and Viper conventions.

Updated May 9, 2026
One-click install
npx skills add https://github.com/LuminaVault/LuminaVaultShared --skill golang-cli-luminavault
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/LuminaVault/LuminaVaultShared/tree/main/.agents/skills/golang-cli
Command: npx skills add https://github.com/LuminaVault/LuminaVaultShared --skill golang-cli-luminavault

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires github.com/spf13/cobra, github.com/spf13/viper, github.com/fatih/color, github.com/fsnotify/fsnotify, and includes assets (resource) components.

What problem does it solve? Go CLI tools often suffer from inconsistent flag handling, broken configuration layering, polluted stdout streams, and hardcoded versions. This Skill encodes the conventions used by kubectl, docker, and gh so an AI agent builds, extends, or reviews Go CLIs that behave correctly under Unix pipelines and automation. ## Core Features & Use Cases - Command & Flag Structure: Sets up Cobra root commands with SilenceUsage/SilenceErrors, persistent vs local flags, required flags, mutual exclusion, and argument validators. - Configuration Layering: Wires Viper precedence (flags > env vars > config file > defaults) with env prefixes, key replacers, and graceful handling of missing config files. - Production Behaviors: Covers ldflags version injection, Unix exit codes, stdout/stderr discipline, signal.NotifyContext graceful shutdown, shell completions, and programmatic CLI testing. - Use Case: Ask the agent to add a 'serve' subcommand with a --port flag configurable via MYAPP_PORT and ~/.myapp.yaml, and it produces the full Cobra/Viper wiring with tests. ## Quick Start Ask the agent to scaffold a Go CLI with Cobra and Viper including a root command, a serve subcommand, and a version command injected via ldflags.

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?▼

Place commands in cmd/myapp/ with one file per command, keep main.go limited to calling Execute(), and initialize Viper in the root command's PersistentPreRunE. Bind every configurable flag with viper.BindPFlag so flags, env vars, and config files share one source.

How do I make a Go CLI flag configurable via environment variable?▼

Bind the flag to Viper with viper.BindPFlag, call viper.SetEnvPrefix to namespace variables (e.g., MYAPP_PORT), and enable viper.AutomaticEnv. Viper then resolves values in the order: flag, env var, config file, default.

Should I use Cobra or the standard flag package for a Go CLI?▼

Use Cobra with Viper for tools with subcommands, layered configuration, or shell completion needs. The standard library flag package is sufficient for trivial single-purpose tools with no subcommands and few flags.

Why does my Go CLI print usage text on every error?▼

Cobra prints full usage on any RunE error by default. Set SilenceUsage: true and SilenceErrors: true on the root command so errors are handled by your own logic and full help only appears for --help.

How do I test Cobra commands without executing the binary?▼

Execute commands programmatically: create a bytes.Buffer, call cmd.SetOut and cmd.SetErr with it, pass arguments via cmd.SetArgs, then run Execute. Commands must write through cmd.OutOrStdout() instead of os.Stdout for capture to work.

How do I handle Ctrl+C gracefully in a Go CLI server?▼

Use signal.NotifyContext with os.Interrupt and syscall.SIGTERM to derive a cancellable context. On cancellation, call srv.Shutdown with a timeout context (10-30 seconds) and treat http.ErrServerClosed as a normal termination.