use anyhow::Result;
use tokmd_config as cli;
use tokmd_tokeignore as tokeignore;
#[cfg(feature = "ui")]
use crate::interactive::{self, wizard};
#[cfg(feature = "ui")]
use anyhow::Context;
#[cfg(feature = "ui")]
use std::fs;
pub(crate) fn handle(args: cli::InitArgs) -> Result<()> {
#[cfg(not(feature = "ui"))]
let use_wizard = false;
#[cfg(feature = "ui")]
let use_wizard =
!args.print && !args.non_interactive && interactive::tty::should_be_interactive();
if !use_wizard {
if let Some(path) = tokeignore::init_tokeignore(&args)? {
let template_name = format!("{:?}", args.template).to_lowercase();
eprintln!(
"Initialized {} using '{}' template.",
path.display(),
template_name
);
if matches!(args.template, cli::InitProfile::Default) {
eprintln!(
"Hint: Use --template <NAME> for specific defaults (rust, node, python...)."
);
}
eprintln!("Ready! Run 'tokmd' to scan your code.");
}
return Ok(());
}
#[cfg(feature = "ui")]
{
match wizard::run_init_wizard(&args.dir)? {
Some(result) => {
if result.write_tokeignore {
let profile = wizard::project_type_to_profile(result.project_type);
let modified_args = cli::InitArgs {
dir: args.dir.clone(),
force: args.force,
print: false,
template: profile,
non_interactive: true,
};
tokeignore::init_tokeignore(&modified_args)?;
eprintln!("Created .tokeignore");
}
if result.write_config {
let config_path = args.dir.join("tokmd.toml");
if config_path.exists() && !args.force {
eprintln!("tokmd.toml already exists. Use --force to overwrite.");
} else {
let config_content = wizard::generate_toml_config(&result)?;
fs::write(&config_path, config_content).with_context(|| {
format!("Failed to write {}", config_path.display())
})?;
eprintln!("Created tokmd.toml");
}
}
eprintln!("\nInit complete! Run 'tokmd' to scan your project.");
}
None => {
eprintln!("Init cancelled.");
}
}
}
Ok(())
}