diff --git a/crates/nu-cli/src/commands/autoenv.rs b/crates/nu-cli/src/commands/autoenv.rs index d20f65e65e..1abcbf9239 100644 --- a/crates/nu-cli/src/commands/autoenv.rs +++ b/crates/nu-cli/src/commands/autoenv.rs @@ -19,7 +19,7 @@ impl WholeStreamCommand for Autoenv { } async fn run( &self, - args: CommandArgs, + _args: CommandArgs, registry: &CommandRegistry, ) -> Result { let registry = registry.clone(); diff --git a/crates/nu-cli/src/commands/autoenv_trust.rs b/crates/nu-cli/src/commands/autoenv_trust.rs index 0fe5c8867e..ab6e843d27 100644 --- a/crates/nu-cli/src/commands/autoenv_trust.rs +++ b/crates/nu-cli/src/commands/autoenv_trust.rs @@ -1,7 +1,8 @@ use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; +use nu_protocol::SyntaxShape; +use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; use serde::Deserialize; use serde::Serialize; use std::hash::{Hash, Hasher}; @@ -21,11 +22,11 @@ impl WholeStreamCommand for AutoenvTrust { } fn signature(&self) -> Signature { - Signature::build("autoenv trust") + Signature::build("autoenv trust").optional("dir", SyntaxShape::String, "Directory to allow") } fn usage(&self) -> &str { - "Trust a .nu-env file in the current directory" + "Trust a .nu-env file in the current or given directory" } async fn run( @@ -33,8 +34,19 @@ impl WholeStreamCommand for AutoenvTrust { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - let current_dir = std::env::current_dir()?; - let content = std::fs::read_to_string(current_dir.join(".nu-env"))?; + + let tag = args.call_info.name_tag.clone(); + let dir_to_allow = 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 env_file_to_allow = dir_to_allow.clone(); + env_file_to_allow.push_str(".nu-env"); + let content = std::fs::read_to_string(env_file_to_allow)?; let mut hasher = DefaultHasher::new(); content.hash(&mut hasher); @@ -53,14 +65,13 @@ impl WholeStreamCommand for AutoenvTrust { dirs: IndexMap::new(), }); allowed.dirs.insert( - current_dir.to_string_lossy().to_string(), + dir_to_allow, hasher.finish().to_string(), ); fs::write(config_path, toml::to_string(&allowed).unwrap()) .expect("Couldn't write to toml file"); - let tag = args.call_info.name_tag.clone(); Ok(OutputStream::one(ReturnSuccess::value( UntaggedValue::string(".nu-env trusted!").into_value(tag), )))