/golang-cli:golang-clifrom golang-cli
Builds and reviews Go command-line tools with cobra and viper: command tree and hooks, flag/env/config precedence, stdout/stderr, exit codes, signal shutdown, version embedding and tests. Use when adding or changing a cobra or viper CLI, or when a flag, env var or config file is ignored.
Ships in golang-cli. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Not for general Go idiom or error design (golang), benchmarks and profiling (golang-performance), or service config that no CLI reads.
Cobra owns the command tree, flags, and argument validation. Viper owns config resolution across flags, environment, files, and defaults. They are independent. Use cobra alone for a flag-only tool, viper alone for a daemon with no subcommands, and both when flags, env vars, and a config file all feed the same settings. Use the standard flag package for a one-command tool with a few flags, since it needs no dependency.
Check the module's locked versions in go.mod and the API against pkg.go.dev for those versions. The notes below were verified against cobra 1.10.2 and viper 1.21.0. Re-check newer releases with go list -m -u all.
Execute is called, how viper is created (global or injected), and the current tests. Extend that shape instead of adding a second pattern.main thin. It builds a signal context, calls a run(ctx, args, stdout, stderr) int function, and calls os.Exit with the result. Tests can then call run directly.newRootCmd(v *viper.Viper) *cobra.Command with subcommands added inside. A fresh tree per call keeps tests isolated, because cobra keeps parsed flag values on the command between Execute calls.PersistentPreRunE binds flags, sets up env handling, and reads the config file, so every subcommand sees the same resolved values. Read references/viper.md for precedence and the traps.RunE. Set SilenceUsage and SilenceErrors on the root, and let run print the error to stderr and choose the exit code. Read references/cobra.md for hook order, argument validators, and flag groups.cmd.OutOrStdout(), diagnostics and logs to cmd.ErrOrStderr(). Piped output stays clean and tests can capture both.run. Pass arguments, a context, and buffers, then assert on output and exit code. Cover the precedence you rely on (flag over env over file over default).func newRootCmd(v *viper.Viper) *cobra.Command {
root := &cobra.Command{
Use: "myapp",
Version: buildVersion(),
SilenceUsage: true, // no usage dump on runtime errors
SilenceErrors: true, // run() prints the error once
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := v.BindPFlags(cmd.Flags()); err != nil { // the running command's flags, inherited ones included
return err
}
v.SetEnvPrefix("MYAPP")
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
v.AutomaticEnv()
if f := v.GetString("config"); f != "" {
v.SetConfigFile(f)
return v.ReadInConfig() // an explicit file that is missing is an error
}
return nil
},
}
root.PersistentFlags().String("config", "", "config file")
root.AddCommand(newServeCmd(v))
return root
}
func run(ctx context.Context, args []string, stdout, stderr io.Writer) int {
root := newRootCmd(viper.New())
root.SetOut(stdout)
root.SetErr(stderr)
root.SetArgs(args)
if err := root.ExecuteContext(ctx); err != nil {
fmt.Fprintln(stderr, "error:", err)
return 1
}
return 0
}
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
code := run(ctx, os.Args[1:], os.Stdout, os.Stderr)
stop()
os.Exit(code)
}
Commands read cmd.Context() and stop when it is cancelled, which is how Ctrl-C reaches long-running work.
ExitCode() method that run maps with errors.As. By Unix convention 2 means a usage error. Cobra reports usage errors and runtime errors alike, so mapping them takes your own check.--output json (or similar) when scripts consume the result, and keep progress text off stdout.go build -ldflags "-X main.version=1.2.3" on a var version string. When it is empty, fall back to debug.ReadBuildInfo()'s Main.Version, which holds the module version for go install [email protected] builds and (devel) for local builds. Cobra's Version field adds --version.| Mistake | What happens | Fix |
|---|---|---|
Run instead of RunE | Errors have no path out except os.Exit or panic, which skip defers | Use RunE and return the error |
os.Exit inside a command | Deferred cleanup and cobra's error handling are skipped | Return the error and exit only in main |
Writing with fmt.Println or os.Stdout | Tests can't capture output | Use cmd.OutOrStdout() and cmd.ErrOrStderr() |
Child PersistentPreRunE defined | It replaces the parent's hook, so config setup silently doesn't run | Call the parent's hook, or set cobra.EnableTraverseRunHooks = true |
Global viper in code under test | State leaks between tests | Create viper.New() and pass it in |
Missing SetEnvKeyReplacer with nested keys | database.host looks for the env var MYAPP_DATABASE.HOST | Replace . and - with _ |
Reusing one command tree across Execute calls | Flag values from the first run persist | Build a fresh tree per test |
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub adminhead/claude-plugins --plugin golang-cli