Project Structure¶
Understanding the CLI Template project layout and organization.
Directory Overview¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
Core Components¶
Application Entry Point¶
main.go - Application bootstrap - Version information injection - Error handling and exit codes
1 2 3 4 5 6 7 | |
Command Structure¶
cmd/ Directory - Each command is a separate file - Tests are co-located with commands - Follows Cobra command patterns
cmd/root.go - Defines the root command - Global flags and configuration - Version information display
cmd/hello.go - Example subcommand implementation - Demonstrates flag usage - Shows command structure patterns - Separates business logic into testable action functions
Build Configuration¶
Makefile - Standardized build tasks - Development workflow automation - Cross-platform compatibility
.goreleaser.yaml - Multi-platform build configuration - Release artifact generation - Distribution automation
Quality Assurance¶
.golangci.yml - Comprehensive linting rules - Code quality enforcement - Security checks
*_test.go Files - Unit tests for each component - Table-driven test patterns - Coverage reporting
File Organization Principles¶
Commands (cmd/)¶
Each command follows this pattern:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Tests (*_test.go)¶
Test files follow Go conventions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Configuration Files¶
Linting (.golangci.yml)¶
Organized by categories: - Run settings: Timeout, module handling - Enabled linters: Code quality tools - Linter settings: Individual tool configuration - Issue filtering: Test-specific exclusions
Release (.goreleaser.yaml)¶
Structured for automation: - Before hooks: Pre-build preparation - Builds: Multi-platform compilation - Archives: Distribution packaging - Release: GitHub integration
Documentation (mkdocs.yml)¶
Content organization: - Site metadata: Name, description, URLs - Theme configuration: Material design - Navigation structure: Hierarchical content - Plugin configuration: Additional features
Development Workflow¶
Adding New Commands¶
- Create
cmd/newcommand.go - Implement command structure
- Add to root command in
init() - Create
cmd/newcommand_test.go - Update documentation
Modifying Build Process¶
- Update
Makefilefor new tasks - Modify
.goreleaser.yamlfor releases - Adjust CI workflows if needed
- Test with
make all
Documentation Updates¶
- Edit relevant
.mdfiles indocs/ - Update
mkdocs.ymlnavigation - Test locally with
mkdocs serve - Commit changes for auto-deployment
Best Practices¶
Code Organization¶
- Single responsibility: One command per file
- Clear naming: Descriptive file and function names
- Consistent structure: Follow established patterns
- Proper imports: Group standard, third-party, and local
Testing Strategy¶
- Unit tests: Test individual functions
- Integration tests: Test command execution
- Table-driven tests: Multiple scenarios
- Error cases: Test failure conditions
Documentation¶
- Code comments: Document public APIs
- README files: Overview and quick start
- Detailed docs: Comprehensive guides
- Examples: Real-world usage patterns