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, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct Unset {
#[clap(verbatim_doc_comment, value_name = "ENV_KEY")]
keys: Vec<String>,
#[clap(short, long, value_hint = clap::ValueHint::FilePath)]
file: Option<PathBuf>,
#[clap(short, long, overrides_with = "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 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()
}
}