cli.rs
Overview
This file implements the command-line interface (CLI) for the Acki Nacki Proxy Manager. It leverages the clap crate to define and parse command-line arguments and subcommands, enabling users to interact with and configure the proxy manager through a structured CLI. The CLI supports specifying a proxy configuration file and executing subcommands related to Docker integration and process ID (PID) management.
Detailed Components
Static Variable: LONG_VERSION
pub static LONG_VERSION: LazyLock<String>
Purpose: Stores a lazily initialized detailed version string for the CLI, including package version and build metadata.
Initialization: Uses Rust's
LazyLockto defer formatting until first access.Contents:
CARGO_PKG_VERSION: Package version.BUILD_GIT_BRANCH: Git branch at build time.BUILD_GIT_COMMIT: Git commit hash.BUILD_GIT_DATE: Git commit date.BUILD_TIME: Build timestamp.
Usage: Provided to
clapas the long version string displayed in CLI help or version outputs.
Struct: CliArgs
#[derive(Parser, Debug, Clone)]
pub struct CliArgs {
pub proxy_config: PathBuf,
pub command: Command,
}
Derives:
Parserfromclapfor argument parsing,DebugandClonefor debugging and cloning capabilities.Attributes:
Uses
#[command(author, long_version = &**LONG_VERSION, about)]to automatically populate CLI metadata from Cargo manifest and static version string.
Fields:
proxy_config: PathBufCLI option
-cor--proxy-config.Specifies the path to the proxy configuration file (default:
./proxy.yaml).This file is expected to exist prior to running the program.
command: CommandRepresents subcommands supported by the CLI.
Uses
#[command(subcommand)]to delegate parsing to theCommandenum.
Usage Example:
proxy-manager -c ./myproxy.yaml docker --socket unix:///var/run/docker.sock --container mycontainer
This command sets the proxy configuration file to ./myproxy.yaml and runs the docker subcommand with specified socket and container.
Enum: Command
#[derive(Subcommand, Debug, Clone)]
pub enum Command {
Docker { socket: String, container: String },
PidPath { pid_path: PathBuf },
Pid { pid: u64 },
}
Derives:
Subcommandfromclapto parse different subcommands, also implementsDebugandClone.Variants:
Docker
Description: Commands related to Docker integration.
Fields:
socket: StringDocker socket path (default:
unix:///var/run/docker.sock).Used to communicate with Docker daemon.
container: StringName of the proxy container.
Usage Example:
proxy-manager docker --socket unix:///var/run/docker.sock --container proxy-containerPidPath
Description: Manages the path to the proxy process ID (PID) file.
Field:
pid_path: PathBufPath to the proxy PID file (default:
/var/run/proxy.pid).
Usage Example:
proxy-manager pid-path --pid-path /custom/path/proxy.pidPid
Description: Handles operations involving a specific proxy PID.
Field:
pid: u64The process ID of the proxy.
Usage Example:
proxy-manager pid --pid 12345
Implementation Details
Uses
clapmacros (Parser,Subcommand) for declarative CLI argument and subcommand definitions, simplifying argument parsing logic.LazyLockis employed for efficient one-time initialization of the build metadata string.Defaults are provided for commonly used paths and Docker socket locations to improve usability.
The CLI structure supports extensibility by allowing additional subcommands or options to be added easily.
Interaction with Other System Components
The
proxy_configpath points to a configuration file expected to be consumed by other parts of the proxy manager system, which handle proxy setup based on these settings.The
Dockersubcommand interfaces with Docker-related components by specifying the Docker socket and container, likely to manage proxy containers.The
PidPathandPidsubcommands are related to process management, interacting with system-level PID files or process IDs maintained by the proxy manager.This CLI serves as the entry point for user interaction, translating command-line inputs into structured commands processed by the core application logic.
Diagram: CLI Structure and Relationships
classDiagram
class CliArgs {
+proxy_config: PathBuf
+command: Command
}
class Command {
<<enum>>
+Docker
+PidPath
+Pid
}
class Docker {
+socket: String
+container: String
}
class PidPath {
+pid_path: PathBuf
}
class Pid {
+pid: u64
}
CliArgs "1" --> "1" Command
Command <|-- Docker
Command <|-- PidPath
Command <|-- Pid