Skip to main content

commit_wizard/core/
bootstrap.rs

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