界面已本地化;技能介绍与说明文档按所选语言由 AI 自动翻译,代码、命令、路径和专业术语保留原文,以原作者文档为准。
探索技能
文档与办公 · SKILL.md

go-cobra-applications

go-cobra-applications

【文档与办公】Build, refactor, test, and review Cobra-based Go applications with intentional command trees, fresh constructors, suggestions, typed boundaries, scoped flags, local or remote configuration, explicit precedence, safe live reload, context propagation, stable streams, shell completion, and generated documentation. Use for Cobra or Viper CLI architecture…

独立 GitHub 快照 · 自动分类 · 以原作者文档为准

技能说明

GitHub

name: go-cobra-applications description: Build, refactor, test, and review Cobra-based Go applications with intentional command trees, fresh constructors, suggestions, typed boundaries, scoped flags, local or remote configuration, explicit precedence, safe live reload, context propagation, stable streams, shell completion, and generated documentation. Use for Cobra or Viper CLI architecture and implementation. license: MIT compatibility: Codex, Claude Code, and other Agent Skills-compatible clients. metadata: version: "0.4.0" displayName: Go Cobra Applications category: Go tags: go,golang,cli,cobra,viper,command-tree,configuration

Go Cobra Applications

Keep Cobra and Viper at the application edge. Construct a fresh command tree per execution and translate CLI state into typed application calls.

Core Workflow

  1. Model the tree around user resources, workflows, and operations.
  2. Define invocation contracts, argument validation, examples, and flag scope.
  3. Construct fresh root and subcommands with explicit dependencies and streams.
  4. Keep handlers thin, propagate context, and return errors.
  5. Resolve defaults, config, environment, and flags into a validated typed config.
  6. Test parsing, inheritance, precedence, help, errors, and repeated execution.
  7. Generate completion and documentation from the same runtime tree.

Read Next

TaskLoad
Build or restructure a Cobra applicationguidelines.md, workflows/build-cobra-command-tree.md
Apply command-tree, flag, config, and testing rulesreferences/cobra-applications/rules.md
Use constructors and focused implementation examplesreferences/cobra-applications/examples.md
Inject services or resolve configurationreferences/cobra-applications/patterns.md
Review a Cobra/Viper applicationreferences/cobra-applications/checklist.md
Understand architecture, completion, and docsreferences/cobra-applications/knowledge.md

Guardrails

  • Avoid mutable package-global commands and init-driven application wiring.
  • Domain packages must not import Cobra or Viper.
  • Use persistent flags only when descendants genuinely share the option.
  • Treat configuration as validated runtime policy, not mutable application storage.
  • Validate reload candidates and atomically publish only last-known-good state.
  • Show usage for syntax errors, not routine operational failures.
  • Verify generator, completion, docs, and configuration APIs against pinned versions.

Source Notes

Guidance is transformed and paraphrased from Ricardo Gerardi, Powerful Command-Line Applications in Go (Pragmatic Bookshelf, 2021), especially Chapter 7. Examples are original adaptations.

Book: https://pragprog.com/titles/rggo/powerful-command-line-applications-in-go/

The book's 2021 generator flow is historical. Verify current APIs and tooling against https://cobra.dev/ and https://github.com/spf13/viper before use.

Suggestion, remote configuration, and live-reload guidance also incorporates transformed material from Marian Montagnino, Building Modern CLI Applications in Go (Packt, 2023), especially Chapter 4.

Parser-boundary guidance also incorporates transformed material from Inanc Gumus, Go by Example: Programmer's Guide to Idiomatic and Testable Programs (Manning, 2025), Chapter 4.

我的收藏