Optional directory arg

This commit is contained in:
Sam Hedin 2020-06-21 19:59:48 +02:00
parent 1cf177cbfd
commit 2224d07b8a
2 changed files with 19 additions and 8 deletions

View File

@ -19,7 +19,7 @@ impl WholeStreamCommand for Autoenv {
} }
async fn run( async fn run(
&self, &self,
args: CommandArgs, _args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let registry = registry.clone(); let registry = registry.clone();

View File

@ -1,7 +1,8 @@
use crate::commands::WholeStreamCommand; use crate::commands::WholeStreamCommand;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; 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::Deserialize;
use serde::Serialize; use serde::Serialize;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
@ -21,11 +22,11 @@ impl WholeStreamCommand for AutoenvTrust {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("autoenv trust") Signature::build("autoenv trust").optional("dir", SyntaxShape::String, "Directory to allow")
} }
fn usage(&self) -> &str { 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( async fn run(
@ -33,8 +34,19 @@ impl WholeStreamCommand for AutoenvTrust {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
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(); let mut hasher = DefaultHasher::new();
content.hash(&mut hasher); content.hash(&mut hasher);
@ -53,14 +65,13 @@ impl WholeStreamCommand for AutoenvTrust {
dirs: IndexMap::new(), dirs: IndexMap::new(),
}); });
allowed.dirs.insert( allowed.dirs.insert(
current_dir.to_string_lossy().to_string(), dir_to_allow,
hasher.finish().to_string(), hasher.finish().to_string(),
); );
fs::write(config_path, toml::to_string(&allowed).unwrap()) fs::write(config_path, toml::to_string(&allowed).unwrap())
.expect("Couldn't write to toml file"); .expect("Couldn't write to toml file");
let tag = args.call_info.name_tag.clone();
Ok(OutputStream::one(ReturnSuccess::value( Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(".nu-env trusted!").into_value(tag), UntaggedValue::string(".nu-env trusted!").into_value(tag),
))) )))