Skip to main content

commit_wizard/cli/
mod.rs

1//! Command-line interface for Commit Wizard.
2//!
3//! This module provides the CLI implementation, including command parsing, execution,
4//! and error handling. The main entry point is the [`run()`] function which orchestrates
5//! the entire CLI flow.
6//!
7//! ## Submodules
8//!
9//! - `args`: Command-line argument definitions and parsing
10//! - `cmd`: Executable commands (commit, check, tag, bump, etc.)
11//!
12//! ## Example
13//!
14//! This is the whole `cw` binary (it is compiled as part of the tests, never run):
15//!
16//! ```no_run
17//! fn main() -> std::process::ExitCode {
18//!     commit_wizard::cli::run()
19//! }
20//! ```
21
22#[doc(hidden)]
23pub mod args;
24#[doc(hidden)]
25pub mod cmd;
26
27use std::process::ExitCode;
28/// The result type used by command handlers.
29#[doc(hidden)]
30pub type CliResult<T> = Result<T, Error>;
31
32use crate::{
33    core::{bootstrap::build_app_context, exit_code, report_error},
34    engine::{Error, ErrorCode},
35};
36use clap::Parser;
37use tracing::Level;
38use tracing_subscriber::{EnvFilter, fmt};
39
40/// Run the `cw` command line: parse the arguments, run the command, and report any error.
41///
42/// Returns the process exit code. A non-zero code means the command failed; the error has
43/// already been printed.
44pub fn run() -> ExitCode {
45    match tokio::runtime::Builder::new_multi_thread()
46        .enable_all()
47        .build()
48    {
49        Ok(runtime) => runtime.block_on(async_run()),
50        Err(err) => {
51            let err = ErrorCode::ProcessFailure
52                .error()
53                .with_context_str("runtime", err);
54
55            report_error(&err);
56            exit_code(&err)
57        }
58    }
59}
60
61async fn async_run() -> ExitCode {
62    let cli = args::Cli::parse();
63
64    let default_level = match (cli.global.verbose, cli.global.quiet) {
65        (_, q) if q >= 2 => Level::ERROR,
66        (_, 1) => Level::WARN,
67        (1, _) => Level::INFO,
68        (2, _) => Level::DEBUG,
69        (v, _) if v >= 3 => Level::TRACE,
70        _ => Level::WARN,
71    };
72    let filter = EnvFilter::try_from_default_env()
73        .unwrap_or_else(|_| EnvFilter::new(default_level.as_str()));
74    fmt()
75        .with_env_filter(filter)
76        .with_writer(std::io::stderr)
77        .init();
78
79    let ctx = match build_app_context(cli.global.into()) {
80        Ok(ctx) => ctx,
81        Err(err) => {
82            let err = ErrorCode::ProcessFailure
83                .error()
84                .with_context_str("app_context", err);
85            report_error(&err);
86            return exit_code(&err);
87        }
88    };
89
90    let result = match cli.command {
91        args::Command::Add(args) => cmd::add::run(&ctx, args).await,
92        args::Command::Branch(args) => cmd::branch::run(&ctx, args).await,
93        args::Command::Bump(args) => cmd::bump::run(&ctx, args).await,
94        args::Command::Changelog(args) => cmd::changelog::run(&ctx, args).await,
95        args::Command::Check(args) => cmd::check::run(&ctx, args).await,
96        args::Command::Commit(args) => cmd::commit::run(&ctx, args).await,
97        args::Command::Config(args) => cmd::config::run(&ctx, args).await,
98        args::Command::Doctor(args) => cmd::doctor::run(&ctx, args).await,
99        args::Command::Fix(args) => cmd::fix::run(&ctx, args).await,
100        args::Command::Init(args) => cmd::init::run(&ctx, args).await,
101        args::Command::Push(args) => cmd::push::run(&ctx, args).await,
102        args::Command::Tag(args) => cmd::tag::run(&ctx, args).await,
103    };
104
105    if let Err(err) = result {
106        report_error(&err);
107        return exit_code(&err);
108    }
109
110    ExitCode::SUCCESS
111}