use std::path::Path;
use crate::domain::shell::Shell;
use crate::util::path::make_command_exists;
use crate::{compute_precache_fingerprint, resolve_config, CmdOutcome, CmdResult};
pub(crate) fn handle(
shell: String,
list_commands: bool,
resolved: Option<String>,
config_flag: Option<&Path>,
path_prepend: Option<&Path>,
) -> CmdResult {
use crate::app::precache;
let s: Shell = shell.parse().map_err(|e: crate::domain::shell::ShellParseError| {
Box::<dyn std::error::Error>::from(e.to_string())
})?;
let shell_name = format!("{s:?}").to_lowercase();
let (config_path, config) = resolve_config(config_flag)?;
if list_commands {
if !config.precache.path_only {
let cmds = precache::collect_unique_commands(&config);
print!("{}", cmds.join(","));
}
return Ok(CmdOutcome::Ok);
}
let fp = compute_precache_fingerprint(&config_path, &shell_name);
if let Some(resolved_str) = resolved {
let cache = precache::build_cache_from_resolved(&config, &fp, &resolved_str);
let json = precache::cache_to_json(&cache);
println!("{}", precache::export_statement(&shell_name, &json));
return Ok(CmdOutcome::Ok);
}
let command_exists = make_command_exists(path_prepend, None);
let cache = precache::build_cache(&config, &fp, &command_exists);
let json = precache::cache_to_json(&cache);
println!("{}", precache::export_statement(&shell_name, &json));
Ok(CmdOutcome::Ok)
}