go-cli

Build Go command-line tools with Cobra and Viper following Unix conventions.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-cli-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-cli
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-cli
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-cli-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Go CLIs often break shell composability by writing logs to stdout, exiting inside commands, or hardcoding versions. This Skill provides a structured workflow for building, extending, and reviewing Go command-line tools that pipe cleanly, exit predictably, and layer configuration correctly. ## Core Features & Use Cases - Command tree scaffolding: Cobra + Viper layout with one file per command, root command silencing, and config initialization in PersistentPreRunE. - Unix discipline enforcement: stdout/stderr separation via cmd.OutOrStdout(), exit code tables, ldflags version injection, and signal-based graceful shutdown. - Review checklists: Concrete mistake/fix tables for auditing existing CLIs against shell conventions. - Use Case: You are adding a deploy subcommand to an existing Go CLI. The Skill guides you to create it in its own file, bind flags to Viper, add validators and completions, and verify it works via flag, environment variable, and config file. ## Quick Start Use the go-cli skill to scaffold a new Cobra-based CLI with Viper config, a version subcommand, and shell completions.

Frequently Asked Questions about go-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 one file per command under cmd/myapp/, keep main.go limited to calling Execute(), and initialize Viper config in the root command's PersistentPreRunE. Bind every configurable flag with viper.BindPFlag under a SetEnvPrefix namespace.

Cobra vs stdlib flag package for Go CLI tools?

Use Cobra with Viper for anything with subcommands, layered config, or completions. Reserve the stdlib flag package for trivial single-purpose tools with no subcommands, where pulling in Cobra adds unnecessary weight.

How do I make a Go CLI output pipeable in shell scripts?

Write program output only to stdout via cmd.OutOrStdout() and send logs, errors, and diagnostics to stderr. Add an --output flag supporting json, table, and plain formats so scripts can parse results reliably.

How do I embed version info into a Go binary at build time?

Inject version, commit, and date via go build -ldflags -X flags from git tags, then expose them through a version subcommand. Avoid hardcoded version strings so releases always report accurate build metadata.

Why should a Go CLI not call os.Exit inside RunE?

Calling os.Exit inside RunE skips Cobra's error handling and deferred cleanup. Return errors from RunE instead, let main() decide the exit code, and set SilenceUsage so usage text only prints with --help.

Does Viper require a config file to exist?

No. Ignore viper.ConfigFileNotFoundError when reading config so the file stays optional. Precedence remains CLI flags first, then environment variables, then config file, then code defaults.