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//! The typical entry point for a binary using this crate:
15//!
16//! ```ignore
17//! use commit_wizard::cli::run;
18//! fn main() {
19//!     std::process::exit(run().into());
20//! }
21//! ```
22
23pub mod args;
24pub mod cmd;
25
26use std::process::ExitCode;
27pub type CliResult<T> = Result<T, Error>;
28
29use crate::{
30    core::{bootstrap::build_app_context, exit_code, report_error},
31    engine::{Error, ErrorCode},
32};
33use clap::Parser;
34use tracing::Level;
35use tracing_subscriber::{EnvFilter, fmt};
36
37pub fn run() -> ExitCode {
38    match tokio::runtime::Builder::new_multi_thread()
39        .enable_all()
40        .build()
41    {
42        Ok(runtime) => runtime.block_on(async_run()),
43        Err(err) => {
44            let err = ErrorCode::ProcessFailure
45                .error()
46                .with_context_str("runtime", err);
47
48            report_error(&err);
49            exit_code(&err)
50        }
51    }
52}
53
54async fn async_run() -> ExitCode {
55    let cli = args::Cli::parse();
56
57    let default_level = match (cli.global.verbose, cli.global.quiet) {
58        (_, q) if q >= 2 => Level::ERROR,
59        (_, 1) => Level::WARN,
60        (1, _) => Level::INFO,
61        (2, _) => Level::DEBUG,
62        (v, _) if v >= 3 => Level::TRACE,
63        _ => Level::WARN,
64    };
65    let filter = EnvFilter::try_from_default_env()
66        .unwrap_or_else(|_| EnvFilter::new(default_level.as_str()));
67    fmt()
68        .with_env_filter(filter)
69        .with_writer(std::io::stderr)
70        .init();
71
72    let ctx = match build_app_context(cli.global.into()) {
73        Ok(ctx) => ctx,
74        Err(err) => {
75            let err = ErrorCode::ProcessFailure
76                .error()
77                .with_context_str("app_context", err);
78            report_error(&err);
79            return exit_code(&err);
80        }
81    };
82
83    let result = match cli.command {
84        args::Command::Add(args) => cmd::add::run(&ctx, args).await,
85        args::Command::Bump(args) => cmd::bump::run(&ctx, args).await,
86        args::Command::Check(args) => cmd::check::run(&ctx, args).await,
87        args::Command::Commit(args) => cmd::commit::run(&ctx, args).await,
88        args::Command::Config(args) => cmd::config::run(&ctx, args).await,
89        args::Command::Doctor(args) => cmd::doctor::run(&ctx, args).await,
90        args::Command::Init(args) => cmd::init::run(&ctx, args).await,
91        args::Command::Push(args) => cmd::push::run(&ctx, args).await,
92        args::Command::Tag(args) => cmd::tag::run(&ctx, args).await,
93    };
94
95    if let Err(err) = result {
96        report_error(&err);
97        return exit_code(&err);
98    }
99
100    ExitCode::SUCCESS
101}