跳到主要内容
知仓学习社ZHICANG

go-cobra-applications

Build, refactor, test, and review Cobra-based Go applications with intentional command trees, fresh constructors, suggestions, typed boundaries, sco…

不碰外部(只输出文字)无严重或高危命中hashgraph-online/awesome-codex-plugins

它会碰到什么

扫了多少9 个文本文件,38 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

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

| Task | Load |

|---|---|

| Build or restructure a Cobra application | guidelines.md, workflows/build-cobra-command-tree.md |

| Apply command-tree, flag, config, and testing rules | references/cobra-applications/rules.md |

| Use constructors and focused implementation examples | references/cobra-applications/examples.md |

| Inject services or resolve configuration | references/cobra-applications/patterns.md |

| Review a Cobra/Viper application | references/cobra-applications/checklist.md |

| Understand architecture, completion, and docs | references/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.

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。