use super::open;
use crate::BoxError;
use asimov_module::ModuleName;
use clientele::StandardOptions;
pub async fn unset(
module_name: &ModuleName,
keys: &[String],
all: bool,
_flags: &StandardOptions,
) -> Result<(), BoxError> {
let module = open(module_name).await?;
let variables = module.require_variables()?;
let keys: Vec<&str> = if all {
variables.iter().map(|var| var.name.as_str()).collect()
} else {
for key in keys {
module.variable(key)?;
}
keys.iter().map(String::as_str).collect()
};
for key in keys {
tokio::fs::remove_file(module.var_file(key))
.await
.or_else(|e| {
if e.kind() == tokio::io::ErrorKind::NotFound {
Ok(())
} else {
Err(e)
}
})
.inspect_err(|e| {
tracing::error!("failed to unset configuration variable `{key}`: {e}")
})?;
}
module.set_permissions().await?;
Ok(())
}