Add autoenv untrust command

This commit is contained in:
Sam Hedin 2020-06-21 20:59:21 +02:00
parent 2224d07b8a
commit 74fbf786c4
5 changed files with 26 additions and 16 deletions

View File

@ -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),

View File

@ -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;

View File

@ -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<String, String>,
}
#[async_trait]
impl WholeStreamCommand for Autoenv {
fn name(&self) -> &str {

View File

@ -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<String, String>,
}
#[async_trait]
impl WholeStreamCommand for AutoenvTrust {
fn name(&self) -> &str {
@ -34,7 +28,6 @@ impl WholeStreamCommand for AutoenvTrust {
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
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");

View File

@ -3,6 +3,7 @@ mod append;
mod average;
mod autoenv;
mod autoenv_trust;
mod autoenv_untrust;
mod cal;
mod calc;
mod cd;