diff --git a/crates/nu-cli/src/commands/autoenv.rs b/crates/nu-cli/src/commands/autoenv.rs index f8ec280f80..8d15b9770c 100644 --- a/crates/nu-cli/src/commands/autoenv.rs +++ b/crates/nu-cli/src/commands/autoenv.rs @@ -1,7 +1,7 @@ use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{ReturnSuccess, UntaggedValue, Signature}; +use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; use serde::Deserialize; use serde::Serialize; @@ -9,7 +9,14 @@ pub struct Autoenv; #[derive(Deserialize, Serialize)] pub struct Allowed { - pub dirs: IndexMap, + pub files: IndexMap, +} +impl Allowed { + pub fn new() -> Self { + Allowed { + files: IndexMap::new(), + } + } } #[async_trait] impl WholeStreamCommand for Autoenv { @@ -39,7 +46,7 @@ impl WholeStreamCommand for Autoenv { vec![Example { description: "Allow .nu-env file in current directory", example: "autoenv trust", - result: None + result: None, }] } } diff --git a/crates/nu-cli/src/commands/autoenv_trust.rs b/crates/nu-cli/src/commands/autoenv_trust.rs index 9c37c86192..5153ed2d39 100644 --- a/crates/nu-cli/src/commands/autoenv_trust.rs +++ b/crates/nu-cli/src/commands/autoenv_trust.rs @@ -69,14 +69,12 @@ impl WholeStreamCommand for AutoenvTrust { let mut doc = String::new(); file.read_to_string(&mut doc)?; - let mut allowed: Allowed = toml::from_str(doc.as_str()).unwrap_or_else(|_| Allowed { - dirs: IndexMap::new(), - }); + let mut allowed: Allowed = toml::from_str(doc.as_str()).unwrap_or_else(|_| Allowed::new()); - let file_to_allow = file_to_trust.to_string_lossy().to_string(); + let file_to_untrust = file_to_trust.to_string_lossy().to_string(); allowed - .dirs - .insert(file_to_allow, hasher.finish().to_string()); + .files + .insert(file_to_untrust, hasher.finish().to_string()); fs::write(config_path, toml::to_string(&allowed).unwrap()) .expect("Couldn't write to toml file"); diff --git a/crates/nu-cli/src/commands/autoenv_untrust.rs b/crates/nu-cli/src/commands/autoenv_untrust.rs index 139bf22811..13446041ff 100644 --- a/crates/nu-cli/src/commands/autoenv_untrust.rs +++ b/crates/nu-cli/src/commands/autoenv_untrust.rs @@ -30,13 +30,22 @@ impl WholeStreamCommand for AutoenvUnTrust { ) -> Result { let tag = args.call_info.name_tag.clone(); - let dir_to_untrust = match args.call_info.evaluate(registry).await?.args.nth(0) { + let file_to_untrust = match args.call_info.evaluate(registry).await?.args.nth(0) { Some(Value { value: UntaggedValue::Primitive(Primitive::String(ref path)), tag: _, - }) => path.clone(), - _ => std::env::current_dir()?.to_string_lossy().to_string(), + }) => { + let mut dir = crate::path::absolutize(std::env::current_dir()?, path); + dir.push(".nu-env"); + dir + } + _ => { + let mut dir = std::env::current_dir()?; + dir.push(".nu-env"); + dir + } }; + let config_path = config::default_path_for(&Some(PathBuf::from("nu-env.toml")))?; let mut file = match std::fs::OpenOptions::new() @@ -53,10 +62,11 @@ impl WholeStreamCommand for AutoenvUnTrust { let mut doc = String::new(); file.read_to_string(&mut doc)?; - let mut allowed: Allowed = toml::from_str(doc.as_str()).unwrap_or_else(|_| Allowed { - dirs: IndexMap::new(), - }); - allowed.dirs.remove(&dir_to_untrust); + let mut allowed: Allowed = toml::from_str(doc.as_str()).unwrap_or_else(|_| Allowed::new()); + + + let file_to_untrust = file_to_untrust.to_string_lossy().to_string(); + allowed.files.remove(&file_to_untrust); fs::write(config_path, toml::to_string(&allowed).unwrap()) .expect("Couldn't write to toml file");