use super::open;
use crate::BoxError;
use asimov_module::ModuleName;
use clientele::{StandardOptions, SysexitsError::*};
use color_print::ceprintln;
pub async fn get(
module_name: &ModuleName,
key: &str,
stored: bool,
_flags: &StandardOptions,
) -> Result<(), BoxError> {
let module = open(module_name).await?;
module.require_variables()?;
module.variable(key)?;
if stored {
return match tokio::fs::read_to_string(module.var_file(key)).await {
Ok(value) => {
println!("{}", value.trim());
Ok(())
},
Err(e) if e.kind() == tokio::io::ErrorKind::NotFound => {
ceprintln!("<s,r>error:</> no value is stored for variable `{key}`");
Err(EX_CONFIG.into())
},
Err(e) => {
ceprintln!("<s,r>error:</> failed to read variable `{key}`: {e}");
Err(EX_IOERR.into())
},
};
}
match module.manifest.variable(key, Some(module.profile)) {
Ok(value) => {
println!("{}", value.trim());
Ok(())
},
Err(e @ asimov_module::ReadVarError::UnconfiguredVar(_)) => {
ceprintln!("<s,r>error:</> {e}");
Err(EX_CONFIG.into())
},
Err(e) => {
ceprintln!("<s,r>error:</> {e}");
Err(EX_IOERR.into())
},
}
}