@mytechtoday/augment-extensions 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/augment-extensions/coding-standards/c/CHANGELOG.md +55 -0
- package/augment-extensions/coding-standards/c/LICENSE +22 -0
- package/augment-extensions/coding-standards/c/README.md +167 -0
- package/augment-extensions/coding-standards/c/config/defaults.json +26 -0
- package/augment-extensions/coding-standards/c/config/examples/embedded.yaml +25 -0
- package/augment-extensions/coding-standards/c/config/examples/systems.json +31 -0
- package/augment-extensions/coding-standards/c/config/schema.json +244 -0
- package/augment-extensions/coding-standards/c/docs/API.md +613 -0
- package/augment-extensions/coding-standards/c/docs/CONFIGURATION.md +259 -0
- package/augment-extensions/coding-standards/c/docs/USER_GUIDE.md +567 -0
- package/augment-extensions/coding-standards/c/examples/drivers/Makefile +33 -0
- package/augment-extensions/coding-standards/c/examples/drivers/README.md +192 -0
- package/augment-extensions/coding-standards/c/examples/drivers/dma-example.c +224 -0
- package/augment-extensions/coding-standards/c/examples/drivers/example.dts +64 -0
- package/augment-extensions/coding-standards/c/examples/drivers/platform-driver.c +174 -0
- package/augment-extensions/coding-standards/c/examples/embedded/README.md +167 -0
- package/augment-extensions/coding-standards/c/examples/embedded/gpio-control.c +172 -0
- package/augment-extensions/coding-standards/c/examples/embedded/timer-isr.c +198 -0
- package/augment-extensions/coding-standards/c/examples/embedded/uart-communication.c +212 -0
- package/augment-extensions/coding-standards/c/examples/kernel/Makefile +82 -0
- package/augment-extensions/coding-standards/c/examples/kernel/README.md +168 -0
- package/augment-extensions/coding-standards/c/examples/kernel/char-device.c +198 -0
- package/augment-extensions/coding-standards/c/examples/kernel/proc-file.c +131 -0
- package/augment-extensions/coding-standards/c/examples/kernel/simple-module.c +111 -0
- package/augment-extensions/coding-standards/c/examples/legacy/Makefile +62 -0
- package/augment-extensions/coding-standards/c/examples/legacy/README.md +255 -0
- package/augment-extensions/coding-standards/c/examples/legacy/c89-to-c11-migration.c +268 -0
- package/augment-extensions/coding-standards/c/examples/legacy/compatibility-layer.c +239 -0
- package/augment-extensions/coding-standards/c/examples/networking/Makefile +35 -0
- package/augment-extensions/coding-standards/c/examples/networking/README.md +207 -0
- package/augment-extensions/coding-standards/c/examples/networking/protocol-parser.c +270 -0
- package/augment-extensions/coding-standards/c/examples/networking/tcp-server.c +197 -0
- package/augment-extensions/coding-standards/c/examples/networking/udp-multicast.c +220 -0
- package/augment-extensions/coding-standards/c/examples/realtime/Makefile +53 -0
- package/augment-extensions/coding-standards/c/examples/realtime/README.md +199 -0
- package/augment-extensions/coding-standards/c/examples/realtime/deadline-monitoring.c +260 -0
- package/augment-extensions/coding-standards/c/examples/realtime/priority-scheduling.c +258 -0
- package/augment-extensions/coding-standards/c/examples/systems/Makefile +34 -0
- package/augment-extensions/coding-standards/c/examples/systems/README.md +123 -0
- package/augment-extensions/coding-standards/c/examples/systems/ipc-pipes.c +181 -0
- package/augment-extensions/coding-standards/c/examples/systems/process-management.c +153 -0
- package/augment-extensions/coding-standards/c/examples/systems/signal-handling.c +162 -0
- package/augment-extensions/coding-standards/c/module.json +149 -0
- package/augment-extensions/coding-standards/c/rules/categories/drivers.md +635 -0
- package/augment-extensions/coding-standards/c/rules/categories/embedded.md +510 -0
- package/augment-extensions/coding-standards/c/rules/categories/kernel.md +653 -0
- package/augment-extensions/coding-standards/c/rules/categories/legacy.md +526 -0
- package/augment-extensions/coding-standards/c/rules/categories/networking.md +735 -0
- package/augment-extensions/coding-standards/c/rules/categories/realtime.md +631 -0
- package/augment-extensions/coding-standards/c/rules/categories/systems.md +586 -0
- package/augment-extensions/coding-standards/c/rules/universal/const-correctness.md +275 -0
- package/augment-extensions/coding-standards/c/rules/universal/documentation.md +251 -0
- package/augment-extensions/coding-standards/c/rules/universal/error-handling.md +250 -0
- package/augment-extensions/coding-standards/c/rules/universal/header-guards.md +254 -0
- package/augment-extensions/coding-standards/c/rules/universal/memory-safety.md +233 -0
- package/augment-extensions/coding-standards/c/rules/universal/naming.md +146 -0
- package/augment-extensions/coding-standards/c/src/conflict-detector.ts +461 -0
- package/augment-extensions/coding-standards/c/src/prompt-generator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-evaluator.ts +307 -0
- package/augment-extensions/coding-standards/c/src/rule-override.ts +427 -0
- package/augment-extensions/coding-standards/c/src/template-engine.ts +217 -0
- package/augment-extensions/coding-standards/c/templates/prompts/drivers.txt +191 -0
- package/augment-extensions/coding-standards/c/templates/prompts/embedded.txt +164 -0
- package/augment-extensions/coding-standards/c/templates/prompts/kernel.txt +175 -0
- package/augment-extensions/coding-standards/c/templates/prompts/legacy.txt +280 -0
- package/augment-extensions/coding-standards/c/templates/prompts/networking.txt +259 -0
- package/augment-extensions/coding-standards/c/templates/prompts/realtime.txt +219 -0
- package/augment-extensions/coding-standards/c/templates/prompts/systems.txt +147 -0
- package/augment-extensions/coding-standards/c/tests/integration/category-specific.test.ts +356 -0
- package/augment-extensions/coding-standards/c/tests/integration/end-to-end-workflow.test.ts +377 -0
- package/augment-extensions/coding-standards/c/tests/performance/benchmarks.test.ts +407 -0
- package/augment-extensions/coding-standards/c/tests/unit/config-manager.test.ts +345 -0
- package/augment-extensions/coding-standards/c/tests/unit/conflict-detector.test.ts +294 -0
- package/augment-extensions/coding-standards/c/tests/unit/prompt-generator.test.ts +174 -0
- package/augment-extensions/coding-standards/c/tests/unit/registry.test.ts +313 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-evaluator.test.ts +318 -0
- package/augment-extensions/coding-standards/c/tests/unit/rule-override.test.ts +326 -0
- package/augment-extensions/coding-standards/c/tests/unit/template-engine.test.ts +314 -0
- package/augment-extensions/coding-standards/go/CHARACTER-COUNT-REPORT.md +135 -0
- package/augment-extensions/coding-standards/go/PHASE1-COMPLETION.md +146 -0
- package/augment-extensions/coding-standards/go/PHASE4-COMPLETION.md +184 -0
- package/augment-extensions/coding-standards/go/README.md +200 -0
- package/augment-extensions/coding-standards/go/VALIDATION-CHECKLIST.md +154 -0
- package/augment-extensions/coding-standards/go/config/examples/example-cli.json +15 -0
- package/augment-extensions/coding-standards/go/config/examples/example-microservices.json +21 -0
- package/augment-extensions/coding-standards/go/config/examples/example-multi-category.yaml +24 -0
- package/augment-extensions/coding-standards/go/config/examples/example-web.json +15 -0
- package/augment-extensions/coding-standards/go/config/schema.json +110 -0
- package/augment-extensions/coding-standards/go/docs/CATEGORIES.md +221 -0
- package/augment-extensions/coding-standards/go/docs/CONFIGURATION.md +198 -0
- package/augment-extensions/coding-standards/go/docs/TROUBLESHOOTING.md +285 -0
- package/augment-extensions/coding-standards/go/examples/cli/cobra-app.go +287 -0
- package/augment-extensions/coding-standards/go/examples/cloud-native-app.go +217 -0
- package/augment-extensions/coding-standards/go/examples/devops-tool.go +250 -0
- package/augment-extensions/coding-standards/go/examples/distributed-system.go +247 -0
- package/augment-extensions/coding-standards/go/examples/microservices/grpc-service.go +253 -0
- package/augment-extensions/coding-standards/go/examples/rest-api.go +270 -0
- package/augment-extensions/coding-standards/go/examples/web/http-server.go +224 -0
- package/augment-extensions/coding-standards/go/module.json +139 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/api-versioning.md +149 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rate-limiting.md +209 -0
- package/augment-extensions/coding-standards/go/rules/categories/api-development/rest-api-design.md +183 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/cloud-config.md +193 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/health-checks.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/cloud-native/kubernetes.md +180 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/automation.md +179 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/ci-cd-integration.md +147 -0
- package/augment-extensions/coding-standards/go/rules/categories/devops-tooling/infrastructure-as-code.md +231 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/caching.md +150 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/consensus.md +187 -0
- package/augment-extensions/coding-standards/go/rules/categories/distributed-systems/event-sourcing.md +246 -0
- package/augment-extensions/coding-standards/go/rules/cli/command-parsing.md +264 -0
- package/augment-extensions/coding-standards/go/rules/cli/configuration.md +268 -0
- package/augment-extensions/coding-standards/go/rules/cli/cross-platform.md +324 -0
- package/augment-extensions/coding-standards/go/rules/microservices/distributed-tracing.md +253 -0
- package/augment-extensions/coding-standards/go/rules/microservices/grpc.md +257 -0
- package/augment-extensions/coding-standards/go/rules/microservices/metrics.md +278 -0
- package/augment-extensions/coding-standards/go/rules/microservices/service-discovery.md +249 -0
- package/augment-extensions/coding-standards/go/rules/universal/code-organization.md +221 -0
- package/augment-extensions/coding-standards/go/rules/universal/documentation.md +269 -0
- package/augment-extensions/coding-standards/go/rules/universal/performance.md +323 -0
- package/augment-extensions/coding-standards/go/rules/universal/testing.md +162 -0
- package/augment-extensions/coding-standards/go/rules/web/graceful-shutdown.md +249 -0
- package/augment-extensions/coding-standards/go/rules/web/http-handlers.md +164 -0
- package/augment-extensions/coding-standards/go/rules/web/middleware.md +234 -0
- package/augment-extensions/coding-standards/go/rules/web/routing.md +251 -0
- package/augment-extensions/coding-standards/go/templates/prompts/api.md +160 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cli.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/cloud-native.md +121 -0
- package/augment-extensions/coding-standards/go/templates/prompts/devops.md +146 -0
- package/augment-extensions/coding-standards/go/templates/prompts/distributed.md +133 -0
- package/augment-extensions/coding-standards/go/templates/prompts/microservices.md +225 -0
- package/augment-extensions/coding-standards/go/templates/prompts/web.md +181 -0
- package/augment-extensions/coding-standards/go/tests/integration/module-integration.test.ts +164 -0
- package/augment-extensions/coding-standards/go/tests/unit/category-selection.test.ts +147 -0
- package/augment-extensions/coding-standards/go/tests/unit/module-structure.test.ts +154 -0
- package/augment-extensions/coding-standards/go/tests/validate-character-count.ps1 +13 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.ps1 +148 -0
- package/augment-extensions/coding-standards/go/tests/validate-examples.sh +135 -0
- package/cli/dist/analysis/ast-parser.d.ts +47 -0
- package/cli/dist/analysis/ast-parser.d.ts.map +1 -0
- package/cli/dist/analysis/ast-parser.js +161 -0
- package/cli/dist/analysis/ast-parser.js.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts +27 -0
- package/cli/dist/analysis/complexity-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/complexity-analyzer.js +189 -0
- package/cli/dist/analysis/complexity-analyzer.js.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts +23 -0
- package/cli/dist/analysis/dependency-analyzer.d.ts.map +1 -0
- package/cli/dist/analysis/dependency-analyzer.js +237 -0
- package/cli/dist/analysis/dependency-analyzer.js.map +1 -0
- package/cli/dist/analysis/index.d.ts +9 -0
- package/cli/dist/analysis/index.d.ts.map +1 -0
- package/cli/dist/analysis/index.js +25 -0
- package/cli/dist/analysis/index.js.map +1 -0
- package/cli/dist/analysis/security-scanner.d.ts +11 -0
- package/cli/dist/analysis/security-scanner.d.ts.map +1 -0
- package/cli/dist/analysis/security-scanner.js +294 -0
- package/cli/dist/analysis/security-scanner.js.map +1 -0
- package/cli/dist/analysis/types.d.ts +151 -0
- package/cli/dist/analysis/types.d.ts.map +1 -0
- package/cli/dist/analysis/types.js +6 -0
- package/cli/dist/analysis/types.js.map +1 -0
- package/cli/dist/cli.js +24 -0
- package/cli/dist/cli.js.map +1 -1
- package/cli/dist/commands/code-analysis.d.ts +11 -0
- package/cli/dist/commands/code-analysis.d.ts.map +1 -0
- package/cli/dist/commands/code-analysis.js +412 -0
- package/cli/dist/commands/code-analysis.js.map +1 -0
- package/modules.md +99 -3
- package/package.json +14 -2
- package/cli/dist/commands/agent.d.ts +0 -37
- package/cli/dist/commands/agent.d.ts.map +0 -1
- package/cli/dist/commands/agent.js +0 -222
- package/cli/dist/commands/agent.js.map +0 -1
- package/cli/dist/commands/beads.d.ts +0 -64
- package/cli/dist/commands/beads.d.ts.map +0 -1
- package/cli/dist/commands/beads.js +0 -377
- package/cli/dist/commands/beads.js.map +0 -1
- package/cli/dist/commands/change.d.ts +0 -54
- package/cli/dist/commands/change.d.ts.map +0 -1
- package/cli/dist/commands/change.js +0 -243
- package/cli/dist/commands/change.js.map +0 -1
- package/cli/dist/commands/clean.d.ts +0 -15
- package/cli/dist/commands/clean.d.ts.map +0 -1
- package/cli/dist/commands/clean.js +0 -63
- package/cli/dist/commands/clean.js.map +0 -1
- package/cli/dist/commands/clone.d.ts +0 -15
- package/cli/dist/commands/clone.d.ts.map +0 -1
- package/cli/dist/commands/clone.js +0 -49
- package/cli/dist/commands/clone.js.map +0 -1
- package/cli/dist/commands/config.d.ts +0 -33
- package/cli/dist/commands/config.d.ts.map +0 -1
- package/cli/dist/commands/config.js +0 -166
- package/cli/dist/commands/config.js.map +0 -1
- package/cli/dist/commands/context.d.ts +0 -38
- package/cli/dist/commands/context.d.ts.map +0 -1
- package/cli/dist/commands/context.js +0 -205
- package/cli/dist/commands/context.js.map +0 -1
- package/cli/dist/commands/create.d.ts +0 -18
- package/cli/dist/commands/create.d.ts.map +0 -1
- package/cli/dist/commands/create.js +0 -178
- package/cli/dist/commands/create.js.map +0 -1
- package/cli/dist/commands/diff.d.ts +0 -19
- package/cli/dist/commands/diff.d.ts.map +0 -1
- package/cli/dist/commands/diff.js +0 -104
- package/cli/dist/commands/diff.js.map +0 -1
- package/cli/dist/commands/doctor.d.ts +0 -14
- package/cli/dist/commands/doctor.d.ts.map +0 -1
- package/cli/dist/commands/doctor.js +0 -62
- package/cli/dist/commands/doctor.js.map +0 -1
- package/cli/dist/commands/export.d.ts +0 -28
- package/cli/dist/commands/export.d.ts.map +0 -1
- package/cli/dist/commands/export.js +0 -135
- package/cli/dist/commands/export.js.map +0 -1
- package/cli/dist/commands/import.d.ts +0 -23
- package/cli/dist/commands/import.d.ts.map +0 -1
- package/cli/dist/commands/import.js +0 -118
- package/cli/dist/commands/import.js.map +0 -1
- package/cli/dist/commands/prompt.d.ts +0 -45
- package/cli/dist/commands/prompt.d.ts.map +0 -1
- package/cli/dist/commands/prompt.js +0 -223
- package/cli/dist/commands/prompt.js.map +0 -1
- package/cli/dist/commands/spec.d.ts +0 -57
- package/cli/dist/commands/spec.d.ts.map +0 -1
- package/cli/dist/commands/spec.js +0 -279
- package/cli/dist/commands/spec.js.map +0 -1
- package/cli/dist/commands/stats.d.ts +0 -18
- package/cli/dist/commands/stats.d.ts.map +0 -1
- package/cli/dist/commands/stats.js +0 -85
- package/cli/dist/commands/stats.js.map +0 -1
- package/cli/dist/commands/task.d.ts +0 -65
- package/cli/dist/commands/task.d.ts.map +0 -1
- package/cli/dist/commands/task.js +0 -282
- package/cli/dist/commands/task.js.map +0 -1
- package/cli/dist/commands/template.d.ts +0 -17
- package/cli/dist/commands/template.d.ts.map +0 -1
- package/cli/dist/commands/template.js +0 -55
- package/cli/dist/commands/template.js.map +0 -1
- package/cli/dist/utils/agent-config.d.ts +0 -129
- package/cli/dist/utils/agent-config.d.ts.map +0 -1
- package/cli/dist/utils/agent-config.js +0 -297
- package/cli/dist/utils/agent-config.js.map +0 -1
- package/cli/dist/utils/beads-graph.d.ts +0 -17
- package/cli/dist/utils/beads-graph.d.ts.map +0 -1
- package/cli/dist/utils/beads-graph.js +0 -150
- package/cli/dist/utils/beads-graph.js.map +0 -1
- package/cli/dist/utils/beads-integration.d.ts +0 -112
- package/cli/dist/utils/beads-integration.d.ts.map +0 -1
- package/cli/dist/utils/beads-integration.js +0 -312
- package/cli/dist/utils/beads-integration.js.map +0 -1
- package/cli/dist/utils/beads-reporter.d.ts +0 -17
- package/cli/dist/utils/beads-reporter.d.ts.map +0 -1
- package/cli/dist/utils/beads-reporter.js +0 -160
- package/cli/dist/utils/beads-reporter.js.map +0 -1
- package/cli/dist/utils/cache-manager.d.ts +0 -55
- package/cli/dist/utils/cache-manager.d.ts.map +0 -1
- package/cli/dist/utils/cache-manager.js +0 -150
- package/cli/dist/utils/cache-manager.js.map +0 -1
- package/cli/dist/utils/change-manager.d.ts +0 -70
- package/cli/dist/utils/change-manager.d.ts.map +0 -1
- package/cli/dist/utils/change-manager.js +0 -412
- package/cli/dist/utils/change-manager.js.map +0 -1
- package/cli/dist/utils/config-manager-enhanced.d.ts +0 -66
- package/cli/dist/utils/config-manager-enhanced.d.ts.map +0 -1
- package/cli/dist/utils/config-manager-enhanced.js +0 -77
- package/cli/dist/utils/config-manager-enhanced.js.map +0 -1
- package/cli/dist/utils/context-manager.d.ts +0 -96
- package/cli/dist/utils/context-manager.d.ts.map +0 -1
- package/cli/dist/utils/context-manager.js +0 -258
- package/cli/dist/utils/context-manager.js.map +0 -1
- package/cli/dist/utils/diff-engine.d.ts +0 -78
- package/cli/dist/utils/diff-engine.d.ts.map +0 -1
- package/cli/dist/utils/diff-engine.js +0 -233
- package/cli/dist/utils/diff-engine.js.map +0 -1
- package/cli/dist/utils/export-system.d.ts +0 -101
- package/cli/dist/utils/export-system.d.ts.map +0 -1
- package/cli/dist/utils/export-system.js +0 -289
- package/cli/dist/utils/export-system.js.map +0 -1
- package/cli/dist/utils/health-checker.d.ts +0 -66
- package/cli/dist/utils/health-checker.d.ts.map +0 -1
- package/cli/dist/utils/health-checker.js +0 -285
- package/cli/dist/utils/health-checker.js.map +0 -1
- package/cli/dist/utils/import-system.d.ts +0 -74
- package/cli/dist/utils/import-system.d.ts.map +0 -1
- package/cli/dist/utils/import-system.js +0 -317
- package/cli/dist/utils/import-system.js.map +0 -1
- package/cli/dist/utils/module-cloner.d.ts +0 -40
- package/cli/dist/utils/module-cloner.d.ts.map +0 -1
- package/cli/dist/utils/module-cloner.js +0 -136
- package/cli/dist/utils/module-cloner.js.map +0 -1
- package/cli/dist/utils/prompt-manager.d.ts +0 -90
- package/cli/dist/utils/prompt-manager.d.ts.map +0 -1
- package/cli/dist/utils/prompt-manager.js +0 -302
- package/cli/dist/utils/prompt-manager.js.map +0 -1
- package/cli/dist/utils/spec-manager.d.ts +0 -65
- package/cli/dist/utils/spec-manager.d.ts.map +0 -1
- package/cli/dist/utils/spec-manager.js +0 -329
- package/cli/dist/utils/spec-manager.js.map +0 -1
- package/cli/dist/utils/stats-collector.d.ts +0 -74
- package/cli/dist/utils/stats-collector.d.ts.map +0 -1
- package/cli/dist/utils/stats-collector.js +0 -164
- package/cli/dist/utils/stats-collector.js.map +0 -1
- package/cli/dist/utils/template-engine.d.ts +0 -47
- package/cli/dist/utils/template-engine.d.ts.map +0 -1
- package/cli/dist/utils/template-engine.js +0 -204
- package/cli/dist/utils/template-engine.js.map +0 -1
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# Go Web Services - Routing Patterns
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Routing is fundamental to web services, mapping HTTP requests to handlers. This document defines best practices for implementing routing in Go web services.
|
|
6
|
+
|
|
7
|
+
## Core Principles
|
|
8
|
+
|
|
9
|
+
1. **RESTful Design**: Follow REST principles for resource-based APIs
|
|
10
|
+
2. **URL Parameters**: Use path parameters for resource identifiers
|
|
11
|
+
3. **Query Parameters**: Use query strings for filtering and pagination
|
|
12
|
+
4. **Method Routing**: Route based on HTTP methods (GET, POST, PUT, DELETE)
|
|
13
|
+
5. **Versioning**: Support API versioning through URLs or headers
|
|
14
|
+
|
|
15
|
+
## Rules
|
|
16
|
+
|
|
17
|
+
### GOL.3.1.3.1: Use Established Routing Libraries
|
|
18
|
+
|
|
19
|
+
**Rule**: Use proven routing libraries like `gorilla/mux`, `chi`, or `echo` for complex routing needs.
|
|
20
|
+
|
|
21
|
+
**Severity**: WARNING
|
|
22
|
+
|
|
23
|
+
**Rationale**: Standard library `http.ServeMux` is limited; established libraries provide better features.
|
|
24
|
+
|
|
25
|
+
**✅ Good - Using chi**:
|
|
26
|
+
```go
|
|
27
|
+
import "github.com/go-chi/chi/v5"
|
|
28
|
+
|
|
29
|
+
func setupRoutes() http.Handler {
|
|
30
|
+
r := chi.NewRouter()
|
|
31
|
+
|
|
32
|
+
// Middleware
|
|
33
|
+
r.Use(middleware.Logger)
|
|
34
|
+
r.Use(middleware.Recoverer)
|
|
35
|
+
|
|
36
|
+
// Routes
|
|
37
|
+
r.Get("/health", handleHealth)
|
|
38
|
+
|
|
39
|
+
r.Route("/api/v1", func(r chi.Router) {
|
|
40
|
+
r.Use(authMiddleware)
|
|
41
|
+
|
|
42
|
+
r.Route("/users", func(r chi.Router) {
|
|
43
|
+
r.Get("/", listUsers) // GET /api/v1/users
|
|
44
|
+
r.Post("/", createUser) // POST /api/v1/users
|
|
45
|
+
r.Get("/{id}", getUser) // GET /api/v1/users/123
|
|
46
|
+
r.Put("/{id}", updateUser) // PUT /api/v1/users/123
|
|
47
|
+
r.Delete("/{id}", deleteUser) // DELETE /api/v1/users/123
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
return r
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**✅ Good - Using gorilla/mux**:
|
|
56
|
+
```go
|
|
57
|
+
import "github.com/gorilla/mux"
|
|
58
|
+
|
|
59
|
+
func setupRoutes() http.Handler {
|
|
60
|
+
r := mux.NewRouter()
|
|
61
|
+
|
|
62
|
+
// API v1 routes
|
|
63
|
+
api := r.PathPrefix("/api/v1").Subrouter()
|
|
64
|
+
api.Use(authMiddleware)
|
|
65
|
+
|
|
66
|
+
// User routes
|
|
67
|
+
api.HandleFunc("/users", listUsers).Methods("GET")
|
|
68
|
+
api.HandleFunc("/users", createUser).Methods("POST")
|
|
69
|
+
api.HandleFunc("/users/{id}", getUser).Methods("GET")
|
|
70
|
+
api.HandleFunc("/users/{id}", updateUser).Methods("PUT")
|
|
71
|
+
api.HandleFunc("/users/{id}", deleteUser).Methods("DELETE")
|
|
72
|
+
|
|
73
|
+
return r
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### GOL.3.1.3.2: Follow RESTful Resource Naming
|
|
78
|
+
|
|
79
|
+
**Rule**: Use plural nouns for resources and standard HTTP methods for operations.
|
|
80
|
+
|
|
81
|
+
**Severity**: WARNING
|
|
82
|
+
|
|
83
|
+
**Rationale**: Consistent RESTful design improves API usability and predictability.
|
|
84
|
+
|
|
85
|
+
**RESTful Patterns**:
|
|
86
|
+
- `GET /users` - List all users
|
|
87
|
+
- `GET /users/{id}` - Get specific user
|
|
88
|
+
- `POST /users` - Create new user
|
|
89
|
+
- `PUT /users/{id}` - Update entire user
|
|
90
|
+
- `PATCH /users/{id}` - Partial update user
|
|
91
|
+
- `DELETE /users/{id}` - Delete user
|
|
92
|
+
|
|
93
|
+
**✅ Good**:
|
|
94
|
+
```go
|
|
95
|
+
r.Route("/api/v1/users", func(r chi.Router) {
|
|
96
|
+
r.Get("/", listUsers) // List users
|
|
97
|
+
r.Post("/", createUser) // Create user
|
|
98
|
+
r.Get("/{id}", getUser) // Get user by ID
|
|
99
|
+
r.Put("/{id}", updateUser) // Update user
|
|
100
|
+
r.Delete("/{id}", deleteUser) // Delete user
|
|
101
|
+
|
|
102
|
+
// Nested resources
|
|
103
|
+
r.Get("/{id}/posts", getUserPosts) // Get user's posts
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
**❌ Bad**:
|
|
108
|
+
```go
|
|
109
|
+
// Non-RESTful, verb-based URLs
|
|
110
|
+
r.Get("/getUser", getUser)
|
|
111
|
+
r.Post("/createUser", createUser)
|
|
112
|
+
r.Post("/deleteUser", deleteUser)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### GOL.3.1.3.3: Extract URL Parameters Safely
|
|
116
|
+
|
|
117
|
+
**Rule**: Always validate URL parameters before use.
|
|
118
|
+
|
|
119
|
+
**Severity**: ERROR
|
|
120
|
+
|
|
121
|
+
**Rationale**: Invalid parameters can cause panics or security vulnerabilities.
|
|
122
|
+
|
|
123
|
+
**✅ Good**:
|
|
124
|
+
```go
|
|
125
|
+
func getUser(w http.ResponseWriter, r *http.Request) {
|
|
126
|
+
// Extract parameter
|
|
127
|
+
idStr := chi.URLParam(r, "id")
|
|
128
|
+
if idStr == "" {
|
|
129
|
+
http.Error(w, "Missing user ID", http.StatusBadRequest)
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Validate and convert
|
|
134
|
+
id, err := strconv.ParseInt(idStr, 10, 64)
|
|
135
|
+
if err != nil {
|
|
136
|
+
http.Error(w, "Invalid user ID", http.StatusBadRequest)
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Use validated parameter
|
|
141
|
+
user, err := getUserByID(r.Context(), id)
|
|
142
|
+
if err != nil {
|
|
143
|
+
if errors.Is(err, ErrNotFound) {
|
|
144
|
+
http.Error(w, "User not found", http.StatusNotFound)
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
http.Error(w, "Internal error", http.StatusInternalServerError)
|
|
148
|
+
return
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
json.NewEncoder(w).Encode(user)
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### GOL.3.1.3.4: Implement API Versioning
|
|
156
|
+
|
|
157
|
+
**Rule**: Version APIs using URL path prefixes (e.g., `/api/v1`, `/api/v2`).
|
|
158
|
+
|
|
159
|
+
**Severity**: WARNING
|
|
160
|
+
|
|
161
|
+
**Rationale**: Versioning enables backward compatibility and gradual migration.
|
|
162
|
+
|
|
163
|
+
**✅ Good**:
|
|
164
|
+
```go
|
|
165
|
+
func setupRoutes() http.Handler {
|
|
166
|
+
r := chi.NewRouter()
|
|
167
|
+
|
|
168
|
+
// Version 1 API
|
|
169
|
+
r.Route("/api/v1", func(r chi.Router) {
|
|
170
|
+
r.Get("/users", listUsersV1)
|
|
171
|
+
r.Get("/users/{id}", getUserV1)
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
// Version 2 API with breaking changes
|
|
175
|
+
r.Route("/api/v2", func(r chi.Router) {
|
|
176
|
+
r.Get("/users", listUsersV2)
|
|
177
|
+
r.Get("/users/{id}", getUserV2)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
return r
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### GOL.3.1.3.5: Use Query Parameters for Filtering
|
|
185
|
+
|
|
186
|
+
**Rule**: Use query parameters for filtering, sorting, and pagination.
|
|
187
|
+
|
|
188
|
+
**Severity**: INFO
|
|
189
|
+
|
|
190
|
+
**✅ Good**:
|
|
191
|
+
```go
|
|
192
|
+
func listUsers(w http.ResponseWriter, r *http.Request) {
|
|
193
|
+
ctx := r.Context()
|
|
194
|
+
|
|
195
|
+
// Parse query parameters
|
|
196
|
+
query := r.URL.Query()
|
|
197
|
+
|
|
198
|
+
// Pagination
|
|
199
|
+
page, _ := strconv.Atoi(query.Get("page"))
|
|
200
|
+
if page < 1 {
|
|
201
|
+
page = 1
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
limit, _ := strconv.Atoi(query.Get("limit"))
|
|
205
|
+
if limit < 1 || limit > 100 {
|
|
206
|
+
limit = 20
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Filtering
|
|
210
|
+
role := query.Get("role")
|
|
211
|
+
status := query.Get("status")
|
|
212
|
+
|
|
213
|
+
// Sorting
|
|
214
|
+
sortBy := query.Get("sort")
|
|
215
|
+
if sortBy == "" {
|
|
216
|
+
sortBy = "created_at"
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Build filter
|
|
220
|
+
filter := UserFilter{
|
|
221
|
+
Page: page,
|
|
222
|
+
Limit: limit,
|
|
223
|
+
Role: role,
|
|
224
|
+
Status: status,
|
|
225
|
+
SortBy: sortBy,
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
users, total, err := listUsersWithFilter(ctx, filter)
|
|
229
|
+
if err != nil {
|
|
230
|
+
http.Error(w, "Internal error", http.StatusInternalServerError)
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Return paginated response
|
|
235
|
+
response := PaginatedResponse{
|
|
236
|
+
Data: users,
|
|
237
|
+
Total: total,
|
|
238
|
+
Page: page,
|
|
239
|
+
Limit: limit,
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
json.NewEncoder(w).Encode(response)
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## References
|
|
247
|
+
|
|
248
|
+
- [RESTful API Design](https://restfulapi.net/)
|
|
249
|
+
- [chi router](https://github.com/go-chi/chi)
|
|
250
|
+
- [gorilla/mux](https://github.com/gorilla/mux)
|
|
251
|
+
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# REST API Development Go Application AI Template
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
You are building a RESTful API in Go with proper versioning, rate limiting, pagination, and error handling.
|
|
6
|
+
|
|
7
|
+
## Key Requirements
|
|
8
|
+
|
|
9
|
+
### REST API Design
|
|
10
|
+
- Use appropriate HTTP methods (GET, POST, PUT, PATCH, DELETE)
|
|
11
|
+
- Return correct HTTP status codes for different scenarios
|
|
12
|
+
- Implement consistent error response format
|
|
13
|
+
- Support pagination for collection endpoints
|
|
14
|
+
- Implement filtering and sorting query parameters
|
|
15
|
+
|
|
16
|
+
### API Versioning
|
|
17
|
+
- Use URL path versioning (/api/v1, /api/v2)
|
|
18
|
+
- Maintain backward compatibility for old versions
|
|
19
|
+
- Document version deprecation with headers
|
|
20
|
+
- Support content negotiation as alternative
|
|
21
|
+
- Only version breaking changes, not additions
|
|
22
|
+
|
|
23
|
+
### Rate Limiting
|
|
24
|
+
- Implement token bucket algorithm for smooth limiting
|
|
25
|
+
- Return rate limit headers (X-RateLimit-*)
|
|
26
|
+
- Apply different limits for different endpoints
|
|
27
|
+
- Support API key-based rate limiting with tiers
|
|
28
|
+
- Use Redis for distributed rate limiting
|
|
29
|
+
|
|
30
|
+
### Error Handling
|
|
31
|
+
- Return structured error responses with details
|
|
32
|
+
- Include error codes for programmatic handling
|
|
33
|
+
- Provide helpful error messages
|
|
34
|
+
- Log errors with appropriate context
|
|
35
|
+
|
|
36
|
+
## Code Structure
|
|
37
|
+
|
|
38
|
+
```go
|
|
39
|
+
type API struct {
|
|
40
|
+
router *mux.Router
|
|
41
|
+
rateLimiter *RateLimiter
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type ErrorResponse struct {
|
|
45
|
+
Error string `json:"error"`
|
|
46
|
+
Message string `json:"message"`
|
|
47
|
+
Code string `json:"code,omitempty"`
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
type PaginatedResponse struct {
|
|
51
|
+
Data interface{} `json:"data"`
|
|
52
|
+
Page int `json:"page"`
|
|
53
|
+
PageSize int `json:"page_size"`
|
|
54
|
+
TotalItems int `json:"total_items"`
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Example Patterns
|
|
59
|
+
|
|
60
|
+
### REST Endpoints
|
|
61
|
+
```go
|
|
62
|
+
func SetupRoutes(r *mux.Router) {
|
|
63
|
+
v1 := r.PathPrefix("/api/v1").Subrouter()
|
|
64
|
+
v1.HandleFunc("/users", CreateUser).Methods("POST")
|
|
65
|
+
v1.HandleFunc("/users", ListUsers).Methods("GET")
|
|
66
|
+
v1.HandleFunc("/users/{id}", GetUser).Methods("GET")
|
|
67
|
+
v1.HandleFunc("/users/{id}", UpdateUser).Methods("PUT")
|
|
68
|
+
v1.HandleFunc("/users/{id}", DeleteUser).Methods("DELETE")
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Error Handling
|
|
73
|
+
```go
|
|
74
|
+
func CreateUser(w http.ResponseWriter, r *http.Request) {
|
|
75
|
+
var user User
|
|
76
|
+
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
|
77
|
+
writeError(w, http.StatusBadRequest, "Invalid request body")
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if err := validateUser(&user); err != nil {
|
|
82
|
+
writeError(w, http.StatusUnprocessableEntity, err.Error())
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
writeJSON(w, http.StatusCreated, user)
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Pagination
|
|
91
|
+
```go
|
|
92
|
+
func ListUsers(w http.ResponseWriter, r *http.Request) {
|
|
93
|
+
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
|
94
|
+
if page < 1 {
|
|
95
|
+
page = 1
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
pageSize, _ := strconv.Atoi(r.URL.Query().Get("page_size"))
|
|
99
|
+
if pageSize < 1 || pageSize > 100 {
|
|
100
|
+
pageSize = 20
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
users, total, _ := db.ListUsers(page, pageSize)
|
|
104
|
+
|
|
105
|
+
response := PaginatedResponse{
|
|
106
|
+
Data: users,
|
|
107
|
+
Page: page,
|
|
108
|
+
PageSize: pageSize,
|
|
109
|
+
TotalItems: total,
|
|
110
|
+
TotalPages: (total + pageSize - 1) / pageSize,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
writeJSON(w, http.StatusOK, response)
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Rate Limiting
|
|
118
|
+
```go
|
|
119
|
+
func RateLimitMiddleware(limiter *RateLimiter) func(http.Handler) http.Handler {
|
|
120
|
+
return func(next http.Handler) http.Handler {
|
|
121
|
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
122
|
+
key := getClientKey(r)
|
|
123
|
+
l := limiter.GetLimiter(key)
|
|
124
|
+
|
|
125
|
+
if !l.Allow() {
|
|
126
|
+
w.Header().Set("X-RateLimit-Limit", "100")
|
|
127
|
+
w.Header().Set("X-RateLimit-Remaining", "0")
|
|
128
|
+
w.Header().Set("Retry-After", "60")
|
|
129
|
+
writeError(w, http.StatusTooManyRequests, "Rate limit exceeded")
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
next.ServeHTTP(w, r)
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Best Practices
|
|
140
|
+
|
|
141
|
+
1. **Consistency**: Use consistent naming and response formats
|
|
142
|
+
2. **Documentation**: Provide OpenAPI/Swagger documentation
|
|
143
|
+
3. **Security**: Implement authentication and authorization
|
|
144
|
+
4. **Validation**: Validate all inputs thoroughly
|
|
145
|
+
5. **Testing**: Write integration tests for all endpoints
|
|
146
|
+
|
|
147
|
+
## Common Pitfalls to Avoid
|
|
148
|
+
|
|
149
|
+
- ❌ Not returning appropriate status codes
|
|
150
|
+
- ❌ Missing pagination on collection endpoints
|
|
151
|
+
- ❌ Inconsistent error response format
|
|
152
|
+
- ❌ Not implementing rate limiting
|
|
153
|
+
- ❌ Breaking changes without version increment
|
|
154
|
+
|
|
155
|
+
## References
|
|
156
|
+
|
|
157
|
+
- [REST API Design Best Practices](https://restfulapi.net/)
|
|
158
|
+
- [HTTP Status Codes](https://httpstatuses.com/)
|
|
159
|
+
- [API Versioning](https://www.freecodecamp.org/news/how-to-version-a-rest-api/)
|
|
160
|
+
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# CLI Tool Project Template
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
You are generating Go code for a **CLI tool** project. This includes command-line applications, developer tools, and system utilities.
|
|
6
|
+
|
|
7
|
+
## Standards
|
|
8
|
+
|
|
9
|
+
Follow these Go coding standards for CLI tools:
|
|
10
|
+
|
|
11
|
+
### Universal Rules
|
|
12
|
+
|
|
13
|
+
1. **Naming Conventions**: Use MixedCaps for exported names, mixedCaps for unexported
|
|
14
|
+
2. **Error Handling**: Return errors explicitly, provide helpful error messages
|
|
15
|
+
3. **Testing**: Write table-driven tests, test command execution
|
|
16
|
+
4. **Code Organization**: Keep packages flat and focused
|
|
17
|
+
5. **Documentation**: Add godoc comments and usage examples
|
|
18
|
+
6. **Performance**: Minimize startup time, lazy-load dependencies
|
|
19
|
+
|
|
20
|
+
### CLI Tools Rules
|
|
21
|
+
|
|
22
|
+
#### Command Parsing
|
|
23
|
+
- Use Cobra library for command structure and parsing
|
|
24
|
+
- Organize functionality into logical subcommands
|
|
25
|
+
- Use typed flags (string, int, bool) with defaults
|
|
26
|
+
- Validate arguments and flags before execution
|
|
27
|
+
- Provide helpful usage examples in command help
|
|
28
|
+
|
|
29
|
+
#### Configuration
|
|
30
|
+
- Use Viper for configuration management
|
|
31
|
+
- Support config files (YAML, JSON, TOML)
|
|
32
|
+
- Support environment variables with consistent naming
|
|
33
|
+
- Follow precedence: Flags > Env Vars > Config File > Defaults
|
|
34
|
+
- Validate configuration on load
|
|
35
|
+
|
|
36
|
+
#### Cross-Platform
|
|
37
|
+
- Use `filepath` package for all path operations
|
|
38
|
+
- Use standard exit codes (0 for success, 1-255 for errors)
|
|
39
|
+
- Handle OS signals (SIGINT, SIGTERM) gracefully
|
|
40
|
+
- Use `os.UserHomeDir()` for home directory
|
|
41
|
+
- Test on Windows, macOS, and Linux
|
|
42
|
+
|
|
43
|
+
## Key Requirements
|
|
44
|
+
|
|
45
|
+
### Command Structure
|
|
46
|
+
```go
|
|
47
|
+
var rootCmd = &cobra.Command{
|
|
48
|
+
Use: "myapp",
|
|
49
|
+
Short: "Brief description",
|
|
50
|
+
Long: "Detailed description with examples",
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
var subCmd = &cobra.Command{
|
|
54
|
+
Use: "sub [arg]",
|
|
55
|
+
Short: "Subcommand description",
|
|
56
|
+
Args: cobra.ExactArgs(1),
|
|
57
|
+
RunE: func(cmd *cobra.Command, args []string) error {
|
|
58
|
+
return executeCommand(args[0])
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
func init() {
|
|
63
|
+
rootCmd.AddCommand(subCmd)
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Flag Definition
|
|
68
|
+
```go
|
|
69
|
+
var (
|
|
70
|
+
outputFormat string
|
|
71
|
+
verbose bool
|
|
72
|
+
maxResults int
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
func init() {
|
|
76
|
+
cmd.Flags().StringVarP(&outputFormat, "output", "o", "table", "Output format")
|
|
77
|
+
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")
|
|
78
|
+
cmd.Flags().IntVar(&maxResults, "limit", 10, "Maximum results")
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Configuration Management
|
|
83
|
+
```go
|
|
84
|
+
func initConfig() {
|
|
85
|
+
viper.SetConfigName("config")
|
|
86
|
+
viper.AddConfigPath("$HOME/.myapp")
|
|
87
|
+
viper.AddConfigPath(".")
|
|
88
|
+
|
|
89
|
+
viper.SetEnvPrefix("MYAPP")
|
|
90
|
+
viper.AutomaticEnv()
|
|
91
|
+
|
|
92
|
+
viper.SetDefault("server.port", 8080)
|
|
93
|
+
|
|
94
|
+
viper.ReadInConfig()
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Cross-Platform Paths
|
|
99
|
+
```go
|
|
100
|
+
func getConfigPath() (string, error) {
|
|
101
|
+
home, err := os.UserHomeDir()
|
|
102
|
+
if err != nil {
|
|
103
|
+
return "", err
|
|
104
|
+
}
|
|
105
|
+
return filepath.Join(home, ".myapp", "config.yaml"), nil
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Exit Codes
|
|
110
|
+
```go
|
|
111
|
+
const (
|
|
112
|
+
ExitSuccess = 0
|
|
113
|
+
ExitError = 1
|
|
114
|
+
ExitUsageError = 2
|
|
115
|
+
ExitConfigError = 3
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
func main() {
|
|
119
|
+
if err := rootCmd.Execute(); err != nil {
|
|
120
|
+
os.Exit(ExitError)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Signal Handling
|
|
126
|
+
```go
|
|
127
|
+
func runWithCancellation(ctx context.Context) error {
|
|
128
|
+
sigChan := make(chan os.Signal, 1)
|
|
129
|
+
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
|
130
|
+
|
|
131
|
+
select {
|
|
132
|
+
case <-sigChan:
|
|
133
|
+
return fmt.Errorf("cancelled by user")
|
|
134
|
+
case <-ctx.Done():
|
|
135
|
+
return ctx.Err()
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Common Patterns
|
|
141
|
+
|
|
142
|
+
### Argument Validation
|
|
143
|
+
```go
|
|
144
|
+
var createCmd = &cobra.Command{
|
|
145
|
+
Args: cobra.ExactArgs(1),
|
|
146
|
+
PreRunE: func(cmd *cobra.Command, args []string) error {
|
|
147
|
+
if len(args[0]) < 3 {
|
|
148
|
+
return fmt.Errorf("name must be at least 3 characters")
|
|
149
|
+
}
|
|
150
|
+
return nil
|
|
151
|
+
},
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Persistent Flags
|
|
156
|
+
```go
|
|
157
|
+
func init() {
|
|
158
|
+
// Available to all subcommands
|
|
159
|
+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file")
|
|
160
|
+
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose")
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Config File Generation
|
|
165
|
+
```go
|
|
166
|
+
var initConfigCmd = &cobra.Command{
|
|
167
|
+
Use: "init-config",
|
|
168
|
+
Short: "Generate sample config",
|
|
169
|
+
RunE: func(cmd *cobra.Command, args []string) error {
|
|
170
|
+
viper.SetDefault("key", "value")
|
|
171
|
+
return viper.SafeWriteConfigAs("config.yaml")
|
|
172
|
+
},
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Platform-Specific Code
|
|
177
|
+
```go
|
|
178
|
+
// Use build tags for platform-specific files
|
|
179
|
+
//go:build unix
|
|
180
|
+
package main
|
|
181
|
+
|
|
182
|
+
// Or runtime checks
|
|
183
|
+
func openBrowser(url string) error {
|
|
184
|
+
var cmd *exec.Cmd
|
|
185
|
+
switch runtime.GOOS {
|
|
186
|
+
case "darwin":
|
|
187
|
+
cmd = exec.Command("open", url)
|
|
188
|
+
case "windows":
|
|
189
|
+
cmd = exec.Command("cmd", "/c", "start", url)
|
|
190
|
+
default:
|
|
191
|
+
cmd = exec.Command("xdg-open", url)
|
|
192
|
+
}
|
|
193
|
+
return cmd.Start()
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Tools
|
|
198
|
+
|
|
199
|
+
Ensure generated code passes:
|
|
200
|
+
- `go build` - Compiles without errors
|
|
201
|
+
- `golangci-lint run` - Passes all linters
|
|
202
|
+
- `go vet` - No suspicious constructs
|
|
203
|
+
- `gofmt -d .` - Properly formatted
|
|
204
|
+
- Test on multiple platforms (Windows, macOS, Linux)
|
|
205
|
+
|
|
206
|
+
## Output Format
|
|
207
|
+
|
|
208
|
+
Generate complete, production-ready Go code with:
|
|
209
|
+
- Package declaration and imports
|
|
210
|
+
- Root command and subcommands
|
|
211
|
+
- Flag definitions with validation
|
|
212
|
+
- Configuration management with Viper
|
|
213
|
+
- Cross-platform path handling
|
|
214
|
+
- Proper exit codes
|
|
215
|
+
- Signal handling for graceful shutdown
|
|
216
|
+
- Comprehensive help text and examples
|
|
217
|
+
- Godoc comments
|
|
218
|
+
|
|
219
|
+
## References
|
|
220
|
+
|
|
221
|
+
- [Cobra Documentation](https://github.com/spf13/cobra)
|
|
222
|
+
- [Viper Documentation](https://github.com/spf13/viper)
|
|
223
|
+
- [CLI Best Practices](https://clig.dev/)
|
|
224
|
+
- [Go filepath package](https://pkg.go.dev/path/filepath)
|
|
225
|
+
|