golang-cli

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

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-cli-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-cli
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-cli-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes assets (resource) components.

What problem does it solve? Go CLI tools often suffer from inconsistent flag handling, broken configuration layering, wrong exit codes, and stdout/stderr misuse that breaks shell pipelines. This Skill encodes the conventions used by kubectl, docker, and gh so your CLI behaves correctly under automation and scripting. ## Core Features & Use Cases - Command Structure & Flags: Sets up Cobra root commands with SilenceUsage/SilenceErrors, 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 env prefixes and graceful missing-config handling. - Production Behaviors: Covers ldflags version injection, Unix exit codes, signal.NotifyContext graceful shutdown, shell completions, machine-readable output formats, and programmatic CLI testing. - Use Case: When adding a 'serve' subcommand to an existing Go CLI, the Skill reads the current command tree, binds the --port flag to Viper, writes logs to stderr, and adds table-driven tests that capture output via buffers. ## Quick Start Ask the agent to scaffold a new Go CLI with Cobra and Viper including a root command, a serve subcommand, and a version command.

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 for config init, and add one file per subcommand registered via init(). Bind every configurable flag to Viper so flags, env vars, and config files share one precedence chain.

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

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

Cobra vs urfave/cli vs stdlib flag for Go CLIs?

Cobra plus Viper is the default stack for tools with subcommands and layered configuration, powering kubectl, docker, and hugo. For trivial single-purpose tools with few flags and no subcommands, the stdlib flag package is sufficient.

Why does my Go CLI break when piped to other commands?

Log or diagnostic output written to stdout corrupts piped data. Write program output to stdout via cmd.OutOrStdout() and send logs, progress, and errors to stderr, which also lets tests capture output through buffers.

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

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

How do I test Cobra commands without running 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(). Use table-driven tests checking both the captured output string and the returned error.