Skip to main content

commit_wizard/core/
bootstrap.rs

1//! Application initialization and context building.
2//!
3//! This module handles the bootstrap process: converting CLI arguments into the application
4//! [`Context`], resolving configuration, and initializing runtime state.
5
6use std::path::PathBuf;
7
8use crate::{
9    core::context::Context,
10    engine::{
11        error::Result,
12        models::runtime::{Runtime, mode::RunMode},
13    },
14};
15
16/// All inputs needed to build an application context from CLI arguments.
17pub struct AppContextArgs {
18    pub verbose: u8,
19    pub quiet: u8,
20    pub json: bool,
21    pub format: String,
22    pub dry_run: bool,
23    pub output_color: String,
24    pub cwd: PathBuf,
25    pub ci: bool,
26    pub non_interactive: bool,
27    pub auto_yes: bool,
28    pub force: bool,
29    pub config_path: Option<PathBuf>,
30    pub registry: Option<String>,
31    pub registry_ref: Option<String>,
32    pub registry_section: Option<String>,
33}
34
35pub fn build_app_context(args: AppContextArgs) -> Result<Context> {
36    let mode = RunMode::from_flags(args.ci, args.non_interactive);
37
38    let output_color = if args.output_color == "auto" {
39        if mode == RunMode::Interactive {
40            scriba::ColorMode::Auto
41        } else {
42            scriba::ColorMode::Never
43        }
44    } else {
45        scriba::ColorMode::from_str(&args.output_color)
46    };
47
48    let output_envelope = if args.json {
49        scriba::EnvelopeMode::Json
50    } else {
51        scriba::EnvelopeMode::None
52    };
53
54    let mut runtime = Runtime::new();
55
56    runtime
57        .set_mode(mode)
58        .set_cwd(args.cwd)
59        .set_explicit_config_path(args.config_path)
60        .set_explicit_registry(args.registry)
61        .set_explicit_registry_ref(args.registry_ref)
62        .set_explicit_registry_section(args.registry_section)
63        .set_dry_run(args.dry_run)
64        .options_mut()
65        .set_auto_yes(args.auto_yes)
66        .set_force(args.force)
67        .set_output_envelope(output_envelope)
68        .set_output_format(scriba::Format::from_str(&args.format))
69        .set_output_color(output_color)
70        .set_log_level(scriba::Level::from_flags(args.verbose, args.quiet));
71
72    let mut ctx = Context::new(runtime);
73
74    let git = ctx.git();
75    if git.is_installed() && git.is_inside_work_tree() {
76        ctx.set_in_git_repo(true);
77
78        if let Some(repo_root) = git.repo_root() {
79            ctx.set_repo_root(repo_root);
80        }
81    }
82
83    ctx.resolve_available_sources()?;
84    ctx.resolve_active_config()?;
85
86    Ok(ctx)
87}