1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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};
/// Remove environment variable(s) from the config file
///
/// By default, this command selects the nearest configuration directory and
/// modifies its lowest-precedence TOML file, creating `mise.toml` here if none exists.
#[derive(Debug, usage_rs::Args)]
#[usage(
verbatim_doc_comment,
example(
r###"mise unset NODE_ENV"###,
help = r###"Remove NODE_ENV from the selected project config"###
),
example(
r###"mise unset NODE_ENV -g"###,
help = r###"Remove NODE_ENV from the global config"###
)
)]
pub(crate) struct Unset {
/// Environment variable(s) to remove
/// e.g.: NODE_ENV
#[usage(verbatim_doc_comment, value_name = "ENV_KEY")]
keys: Vec<String>,
/// Specify a file to use instead of `mise.toml`
///
/// Can be a file path or directory. If a directory is provided, will create/use mise.toml in that directory.
///
/// Defaults to [`MISE_DEFAULT_CONFIG_FILENAME`](https://mise.jdx.dev/configuration.html#mise_default_config_filename) environment variable, or `mise.toml`.
/// Use [`MISE_GLOBAL_CONFIG_FILE`](https://mise.jdx.dev/configuration.html#mise_global_config_file) to choose a different global config path.
#[usage(short, long, visible_alias = "path", value_hint = usage_rs::ValueHint::FilePath)]
file: Option<PathBuf>,
/// Use the global config file
#[usage(short, long, overrides = "file")]
global: bool,
}
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()
}
}