From 74fbf786c4337c5c0b817ee871c0d2c1a75ca24c Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Sun, 21 Jun 2020 20:59:21 +0200 Subject: [PATCH] Add autoenv untrust command --- crates/nu-cli/src/cli.rs | 1 + crates/nu-cli/src/commands.rs | 2 ++ crates/nu-cli/src/commands/autoenv.rs | 6 ++++ crates/nu-cli/src/commands/autoenv_trust.rs | 32 ++++++++++----------- crates/nu-cli/tests/commands/mod.rs | 1 + 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 164b12c80b..3b866004c2 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -347,6 +347,7 @@ pub fn create_default_context( whole_stream_command(Histogram), whole_stream_command(Autoenv), whole_stream_command(AutoenvTrust), + whole_stream_command(AutoenvUnTrust), whole_stream_command(Math), whole_stream_command(Average), whole_stream_command(Minimum), diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index cda75fb276..d4b3b1a09c 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -9,6 +9,7 @@ pub(crate) mod append; pub(crate) mod args; pub(crate) mod autoenv; pub(crate) mod autoenv_trust; +pub(crate) mod autoenv_untrust; pub(crate) mod autoview; pub(crate) mod build_string; pub(crate) mod cal; @@ -139,6 +140,7 @@ pub(crate) use alias::Alias; pub(crate) use append::Append; pub(crate) use autoenv::Autoenv; pub(crate) use autoenv_trust::AutoenvTrust; +pub(crate) use autoenv_untrust::AutoenvUnTrust; pub(crate) use build_string::BuildString; pub(crate) use cal::Cal; pub(crate) use calc::Calc; diff --git a/crates/nu-cli/src/commands/autoenv.rs b/crates/nu-cli/src/commands/autoenv.rs index 1abcbf9239..f8ec280f80 100644 --- a/crates/nu-cli/src/commands/autoenv.rs +++ b/crates/nu-cli/src/commands/autoenv.rs @@ -2,9 +2,15 @@ use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, UntaggedValue, Signature}; +use serde::Deserialize; +use serde::Serialize; pub struct Autoenv; +#[derive(Deserialize, Serialize)] +pub struct Allowed { + pub dirs: IndexMap, +} #[async_trait] impl WholeStreamCommand for Autoenv { fn name(&self) -> &str { diff --git a/crates/nu-cli/src/commands/autoenv_trust.rs b/crates/nu-cli/src/commands/autoenv_trust.rs index ab6e843d27..3b2c20ad62 100644 --- a/crates/nu-cli/src/commands/autoenv_trust.rs +++ b/crates/nu-cli/src/commands/autoenv_trust.rs @@ -1,20 +1,14 @@ +use super::autoenv::Allowed; use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::SyntaxShape; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; -use serde::Deserialize; -use serde::Serialize; use std::hash::{Hash, Hasher}; -use std::io::{Read, Write}; +use std::io::Read; use std::{collections::hash_map::DefaultHasher, fs, path::PathBuf}; pub struct AutoenvTrust; -#[derive(Deserialize, Serialize)] -struct Allowed { - pub dirs: IndexMap, -} - #[async_trait] impl WholeStreamCommand for AutoenvTrust { fn name(&self) -> &str { @@ -34,7 +28,6 @@ impl WholeStreamCommand for AutoenvTrust { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - let tag = args.call_info.name_tag.clone(); let dir_to_allow = match args.call_info.evaluate(registry).await?.args.nth(0) { Some(Value { @@ -45,29 +38,36 @@ impl WholeStreamCommand for AutoenvTrust { }; let mut env_file_to_allow = dir_to_allow.clone(); - env_file_to_allow.push_str(".nu-env"); + 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); let config_path = config::default_path_for(&Some(PathBuf::from("nu-env.toml")))?; - let mut file = std::fs::OpenOptions::new() + let mut file = match std::fs::OpenOptions::new() .read(true) .create(true) .write(true) .open(config_path.clone()) - .unwrap(); + { + Ok(p) => p, + Err(_) => { + return Err(ShellError::untagged_runtime_error( + "Couldn't open nu-env.toml", + )); + } + }; + 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.insert( - dir_to_allow, - hasher.finish().to_string(), - ); + allowed + .dirs + .insert(dir_to_allow, 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/tests/commands/mod.rs b/crates/nu-cli/tests/commands/mod.rs index 0b7a6dd75a..b5fff2c776 100644 --- a/crates/nu-cli/tests/commands/mod.rs +++ b/crates/nu-cli/tests/commands/mod.rs @@ -3,6 +3,7 @@ mod append; mod average; mod autoenv; mod autoenv_trust; +mod autoenv_untrust; mod cal; mod calc; mod cd;