use std::path::PathBuf;
use eyre::Result;
use crate::config::config_file::ConfigFile;
use crate::config::config_file::mise_toml::MiseToml;
use crate::config::{ConfigPathOptions, resolve_target_config_path};
#[derive(Debug, usage_rs::Args)]
#[usage(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub(crate) struct Unset {
#[usage(verbatim_doc_comment, value_name = "ENV_KEY")]
keys: Vec<String>,
#[usage(short, long, visible_alias = "path", value_hint = usage_rs::ValueHint::FilePath)]
file: Option<PathBuf>,
#[usage(short, long, overrides = "file")]
global: bool,
}
const AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
# Remove NODE_ENV from the current directory's config
$ <bold>mise unset NODE_ENV</bold>
# Remove NODE_ENV from the global config
$ <bold>mise unset NODE_ENV -g</bold>
"#
);
impl Unset {
pub(crate) async fn run(self) -> Result<()> {
let filename = resolve_target_config_path(ConfigPathOptions {
global: self.global,
path: self.file.clone(),
env: None,
cwd: None,
prefer_toml: true,
prevent_home_local: true,
})?;
let mut config = MiseToml::from_file(&filename).unwrap_or_default();
for name in self.keys.iter() {
config.remove_env(name)?;
}
config.save()
}
}