Skip to main content

callisto_cli/commands/
init.rs

1use std::process::ExitCode;
2
3use callisto_graph::commands::InitOptions;
4use callisto_model::ApplyPermit;
5use dialoguer::Confirm;
6
7use crate::cli::{GlobalArgs, InitArgs, OutputFormat};
8use crate::error::CliError;
9use crate::output::write_json;
10use crate::render;
11use crate::runner::CliCommandRunner;
12use crate::tty;
13use crate::workspace::load_workspace;
14
15pub fn handle(args: InitArgs, global: &GlobalArgs) -> Result<ExitCode, CliError> {
16    let runner = CliCommandRunner;
17    let ws = load_workspace(global, &runner)?;
18
19    if !args.yes && tty::is_interactive() {
20        let confirm = Confirm::new()
21            .with_prompt(format!("Initialize Callisto configuration in `{}`?", ws.root.display()))
22            .default(true)
23            .interact()
24            .map_err(|e| CliError::Other(format!("Interactive prompt failed: {e}")))?;
25
26        if !confirm {
27            println!("Initialization cancelled.");
28            return Ok(ExitCode::SUCCESS);
29        }
30    }
31
32    let opts = InitOptions { yes: args.yes };
33
34    // `--yes` (answered above) and `--dry-run` gate different things: the
35    // former is consent to apply, the latter is permission to write at all.
36    // `--yes --dry-run` must therefore report the apply outcome and write
37    // nothing.
38    let permit = ApplyPermit::granted_unless_dry_run(global.dry_run);
39    let report = callisto_graph::commands::init(&ws, &opts, permit.as_ref())?;
40
41    if permit.is_none() && global.format == OutputFormat::Text {
42        println!("[DRY-RUN] Init plan calculated (no files written):");
43    }
44
45    match global.format {
46        OutputFormat::Json => write_json(&mut std::io::stdout(), &report)?,
47        OutputFormat::Text => render::render_init(&report, &mut std::io::stdout())?,
48    }
49
50    Ok(ExitCode::SUCCESS)
51}