OsString and paths

This commit is contained in:
Sam Hedin 2020-06-21 21:25:40 +02:00
parent 4a4d8c78fb
commit e6eedf8824
3 changed files with 10 additions and 8 deletions

View File

@ -4,12 +4,13 @@ use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, UntaggedValue, Signature}; use nu_protocol::{ReturnSuccess, UntaggedValue, Signature};
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use std::ffi::OsString;
pub struct Autoenv; pub struct Autoenv;
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct Allowed { pub struct Allowed {
pub dirs: IndexMap<String, String>, pub dirs: IndexMap<OsString, String>,
} }
#[async_trait] #[async_trait]
impl WholeStreamCommand for Autoenv { impl WholeStreamCommand for Autoenv {

View File

@ -6,7 +6,7 @@ use nu_protocol::SyntaxShape;
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::io::Read; use std::io::Read;
use std::{collections::hash_map::DefaultHasher, fs, path::PathBuf}; use std::{collections::hash_map::DefaultHasher, ffi::OsStr, fs, path::PathBuf};
pub struct AutoenvTrust; pub struct AutoenvTrust;
#[async_trait] #[async_trait]
@ -33,12 +33,12 @@ impl WholeStreamCommand for AutoenvTrust {
Some(Value { Some(Value {
value: UntaggedValue::Primitive(Primitive::String(ref path)), value: UntaggedValue::Primitive(Primitive::String(ref path)),
tag: _, tag: _,
}) => path.clone(), }) => PathBuf::from(path),
_ => std::env::current_dir()?.to_string_lossy().to_string(), _ => std::env::current_dir()?,
}; };
let mut env_file_to_allow = dir_to_allow.clone(); let mut env_file_to_allow = dir_to_allow.clone();
env_file_to_allow.push_str("/.nu-env"); env_file_to_allow.push(".nu-env");
let content = std::fs::read_to_string(env_file_to_allow)?; 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);
@ -67,7 +67,7 @@ impl WholeStreamCommand for AutoenvTrust {
}); });
allowed allowed
.dirs .dirs
.insert(dir_to_allow, hasher.finish().to_string()); .insert(dir_to_allow.as_os_str(), 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");

View File

@ -34,9 +34,10 @@ impl WholeStreamCommand for AutoenvUnTrust {
Some(Value { Some(Value {
value: UntaggedValue::Primitive(Primitive::String(ref path)), value: UntaggedValue::Primitive(Primitive::String(ref path)),
tag: _, tag: _,
}) => path.clone(), }) => PathBuf::from(path),
_ => std::env::current_dir()?.to_string_lossy().to_string(), _ => std::env::current_dir()?,
}; };
let config_path = config::default_path_for(&Some(PathBuf::from("nu-env.toml")))?; let config_path = config::default_path_for(&Some(PathBuf::from("nu-env.toml")))?;
let mut file = match std::fs::OpenOptions::new() let mut file = match std::fs::OpenOptions::new()