Architecture
This page explains the architecture of cli-template and the design decisions behind the template.
Project Structure
Section titled “Project Structure”cli-template/├── Cargo.toml # Package manifest├── src/│ ├── main.rs # CLI entry point and Clap parsing│ └── lib.rs # Reusable business logic├── npm/ # npm wrapper for cross-platform distribution├── Dockerfile # Multi-stage Docker build├── Makefile # Development commands├── justfile # Just commands├── lefthook.yml # Git hooks configuration├── .github/│ └── workflows/ # CI/CD workflows└── docs/ # Starlight documentation siteDesign Principles
Section titled “Design Principles”Separation of Concerns
Section titled “Separation of Concerns”src/main.rs— Contains all Clap parsing, CLI argument handling, and themain()entry point. This file is the boundary between the CLI interface and the application logic.src/lib.rs— Contains reusable business logic that can be unit-tested independently of the CLI layer.
Async Runtime
Section titled “Async Runtime”The application uses Tokio as its async runtime. The #[tokio::main] macro on
main() provides a multi-threaded runtime by default, enabling concurrent operations.
Structured Logging
Section titled “Structured Logging”tracing is used for structured, hierarchical logging. Log
levels can be controlled at runtime via the RUST_LOG environment variable:
RUST_LOG=debug cli-template greet --name "World"Dependencies
Section titled “Dependencies”| Dependency | Purpose |
|---|---|
clap |
Type-safe CLI argument parsing |
serde / serde_json |
Serialization and deserialization |
tokio |
Async runtime |
tracing |
Structured logging |
tracing-subscriber |
Logging configuration and filtering |
Build Profiles
Section titled “Build Profiles”The release profile is optimized for distribution:
[profile.release]lto = true # Link-time optimizationstrip = true # Strip symbols from binaryopt-level = 3 # Maximum optimizationDistribution Channels
Section titled “Distribution Channels”| Channel | Description |
|---|---|
| Cargo | Primary distribution via cargo install |
| npm | Cross-platform wrapper with pre-built binaries |
| Docker | Multi-stage Alpine-based container image |
| GitHub Releases | Pre-built binaries for all platforms |
Docker Architecture
Section titled “Docker Architecture”The Dockerfile uses a multi-stage build:
- Builder stage — Full Rust toolchain compiles the release binary.
- Runtime stage — Minimal Alpine image copies only the final binary.
This produces a small final image (~10-15 MB) with no build dependencies.
npm Wrapper
Section titled “npm Wrapper”The npm/ directory contains a TypeScript wrapper that:
- Detects the user’s platform and architecture.
- Downloads the appropriate pre-built binary from GitHub Releases.
- Creates a thin shell script that invokes the binary.
This enables npm install -g as a cross-platform installation method.