golang-cli

Builds and reviews Go CLI applications using Cobra commands and Viper configuration.

1|Updated Jun 2, 2026
One-click install
npx skills add https://github.com/VerifiedOrganic/onboard --skill golang-cli-verifiedorganic
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/VerifiedOrganic/onboard/tree/main/.agents/skills/golang-cli
Command: npx skills add https://github.com/VerifiedOrganic/onboard --skill golang-cli-verifiedorganic

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, logs polluting stdout, and untestable commands. This Skill encodes the conventions used by kubectl, docker, and gh so your CLI behaves correctly under pipes, scripts, and automation from the start. ## Core Features & Use Cases - Command & Flag Structure: Sets up Cobra root commands, subcommands, persistent vs local flags, required flags, mutual exclusion, and argument validators. - Configuration Layering: Wires Viper so values resolve correctly across flags, environment variables, config files, and defaults, with graceful handling of missing config files. - Production Behaviors: Covers ldflags version injection, Unix exit codes, stdout/stderr discipline, signal-based graceful shutdown, shell completions, and programmatic CLI testing. - Use Case: You are scaffolding a new internal deploy tool in Go. Use this Skill to generate the root command with Viper config, a serve subcommand with graceful SIGTERM shutdown, and table-driven tests that capture command output. ## Quick Start Ask the agent to scaffold a Go CLI with Cobra and Viper including a serve subcommand, a version command, and tests for the command output.

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

Keep main.go minimal calling only Execute(), put the root command in root.go with PersistentPreRunE initializing Viper config, and add one file per subcommand registered via init(). Bind every configurable flag with viper.BindPFlag so flags, env vars, and config files share one precedence chain.

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 order: flag, environment variable, config file, default.

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

Use Cobra with Viper for anything with subcommands, many flags, or configuration files — it powers kubectl, docker, and gh. The stdlib flag package is sufficient only 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 whenever RunE returns an error by default. Set SilenceUsage: true on the root command so usage only appears for --help, and set SilenceErrors: true to control error formatting yourself.

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

Execute commands programmatically: create a bytes.Buffer, call cmd.SetOut(buf) and cmd.SetErr(buf), pass arguments with cmd.SetArgs(), then call Execute(). Commands must write via cmd.OutOrStdout() instead of os.Stdout for output to be captured.

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, then call srv.Shutdown with a timeout context when it fires. Treat http.ErrServerClosed as normal termination rather than an error.