commit-wizard 0.0.4

A lightweight CLI assistant for Conventional Commits, semantic versioning, and changelog automation.
Documentation
//! Command-line interface for Commit Wizard.
//!
//! This module provides the CLI implementation, including command parsing, execution,
//! and error handling. The main entry point is the [`run()`] function which orchestrates
//! the entire CLI flow.
//!
//! ## Submodules
//!
//! - `args`: Command-line argument definitions and parsing
//! - `cmd`: Executable commands (commit, check, tag, bump, etc.)
//!
//! ## Example
//!
//! This is the whole `cw` binary (it is compiled as part of the tests, never run):
//!
//! ```no_run
//! fn main() -> std::process::ExitCode {
//!     commit_wizard::cli::run()
//! }
//! ```

#[doc(hidden)]
pub mod args;
#[doc(hidden)]
pub mod cmd;

use std::process::ExitCode;
/// The result type used by command handlers.
#[doc(hidden)]
pub type CliResult<T> = Result<T, Error>;

use crate::{
    core::{bootstrap::build_app_context, exit_code, report_error},
    engine::{Error, ErrorCode},
};
use clap::Parser;
use tracing::Level;
use tracing_subscriber::{EnvFilter, fmt};

/// Run the `cw` command line: parse the arguments, run the command, and report any error.
///
/// Returns the process exit code. A non-zero code means the command failed; the error has
/// already been printed.
pub fn run() -> ExitCode {
    match tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
    {
        Ok(runtime) => runtime.block_on(async_run()),
        Err(err) => {
            let err = ErrorCode::ProcessFailure
                .error()
                .with_context_str("runtime", err);

            report_error(&err);
            exit_code(&err)
        }
    }
}

async fn async_run() -> ExitCode {
    let cli = args::Cli::parse();

    let default_level = match (cli.global.verbose, cli.global.quiet) {
        (_, q) if q >= 2 => Level::ERROR,
        (_, 1) => Level::WARN,
        (1, _) => Level::INFO,
        (2, _) => Level::DEBUG,
        (v, _) if v >= 3 => Level::TRACE,
        _ => Level::WARN,
    };
    let filter = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new(default_level.as_str()));
    fmt()
        .with_env_filter(filter)
        .with_writer(std::io::stderr)
        .init();

    let ctx = match build_app_context(cli.global.into()) {
        Ok(ctx) => ctx,
        Err(err) => {
            let err = ErrorCode::ProcessFailure
                .error()
                .with_context_str("app_context", err);
            report_error(&err);
            return exit_code(&err);
        }
    };

    let result = match cli.command {
        args::Command::Add(args) => cmd::add::run(&ctx, args).await,
        args::Command::Branch(args) => cmd::branch::run(&ctx, args).await,
        args::Command::Bump(args) => cmd::bump::run(&ctx, args).await,
        args::Command::Changelog(args) => cmd::changelog::run(&ctx, args).await,
        args::Command::Check(args) => cmd::check::run(&ctx, args).await,
        args::Command::Commit(args) => cmd::commit::run(&ctx, args).await,
        args::Command::Config(args) => cmd::config::run(&ctx, args).await,
        args::Command::Doctor(args) => cmd::doctor::run(&ctx, args).await,
        args::Command::Fix(args) => cmd::fix::run(&ctx, args).await,
        args::Command::Init(args) => cmd::init::run(&ctx, args).await,
        args::Command::Push(args) => cmd::push::run(&ctx, args).await,
        args::Command::Tag(args) => cmd::tag::run(&ctx, args).await,
    };

    if let Err(err) = result {
        report_error(&err);
        return exit_code(&err);
    }

    ExitCode::SUCCESS
}