use crate::completion;
use crate::error::Result;
use crate::output::{BOLD, CYAN, GRAY, GREEN, RESET, YELLOW};
pub fn init_command() -> Result<()> {
println!("Initializing autom8...");
println!();
let (config_dir, config_created) = crate::config::ensure_config_dir()?;
if config_created {
println!(" {GREEN}Created{RESET} {}", config_dir.display());
} else {
println!(" {GRAY}Exists{RESET} {}", config_dir.display());
}
let global_config_path = crate::config::global_config_path()?;
crate::config::save_global_config(&crate::config::Config::default())?;
println!(
"Created default configuration at {}",
global_config_path.display()
);
let (project_dir, project_created) = crate::config::ensure_project_config_dir()?;
if project_created {
println!(" {GREEN}Created{RESET} {}", project_dir.display());
println!(" {GREEN}Created{RESET} {}/spec/", project_dir.display());
println!(" {GREEN}Created{RESET} {}/runs/", project_dir.display());
} else {
println!(" {GRAY}Exists{RESET} {}", project_dir.display());
}
println!();
println!("{GREEN}Initialization complete!{RESET}");
println!();
println!("Config directory structure:");
println!(" {CYAN}{}{RESET}", project_dir.display());
println!(" ├── spec/ (spec markdown and JSON files)");
println!(" └── runs/ (archived run states)");
println!();
println!("{BOLD}Shell completions:{RESET}");
match completion::install_completions() {
Ok(result) => {
println!(
" {GREEN}Installed{RESET} {} completions to {}",
result.shell,
result.path.display()
);
if let Some(instructions) = result.setup_instructions {
println!();
println!("{YELLOW}Note:{RESET} {}", instructions);
}
}
Err(e) => {
let msg = e.to_string();
if msg.contains("Unsupported shell") {
println!(" {YELLOW}Skipped{RESET} Shell completions not available for your shell");
println!(" Supported shells: bash, zsh, fish");
} else if msg.contains("$SHELL") {
println!(" {YELLOW}Skipped{RESET} Could not detect shell ($SHELL not set)");
} else {
println!(
" {YELLOW}Warning{RESET} Could not install completions: {}",
e
);
}
}
}
println!();
println!("{BOLD}Next steps:{RESET}");
println!(" Run {CYAN}autom8{RESET} to start creating a spec");
Ok(())
}