Skip to content

Architecture

This page explains the architecture of cli-template and the design decisions behind the template.

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 site
  • src/main.rs — Contains all Clap parsing, CLI argument handling, and the main() 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.

The application uses Tokio as its async runtime. The #[tokio::main] macro on main() provides a multi-threaded runtime by default, enabling concurrent operations.

tracing is used for structured, hierarchical logging. Log levels can be controlled at runtime via the RUST_LOG environment variable:

Terminal window
RUST_LOG=debug cli-template greet --name "World"
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

The release profile is optimized for distribution:

[profile.release]
lto = true # Link-time optimization
strip = true # Strip symbols from binary
opt-level = 3 # Maximum optimization
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

The Dockerfile uses a multi-stage build:

  1. Builder stage — Full Rust toolchain compiles the release binary.
  2. Runtime stage — Minimal Alpine image copies only the final binary.

This produces a small final image (~10-15 MB) with no build dependencies.

The npm/ directory contains a TypeScript wrapper that:

  1. Detects the user’s platform and architecture.
  2. Downloads the appropriate pre-built binary from GitHub Releases.
  3. Creates a thin shell script that invokes the binary.

This enables npm install -g as a cross-platform installation method.