mirror of
https://github.com/nushell/nushell.git
synced 2025-01-22 14:18:55 +01:00
Autoenv trust and untrust
This commit is contained in:
parent
a77c8d01fa
commit
1ac8cd4392
@ -1,7 +1,7 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::prelude::*;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, UntaggedValue, Signature};
|
||||
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
@ -9,7 +9,14 @@ pub struct Autoenv;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Allowed {
|
||||
pub dirs: IndexMap<String, String>,
|
||||
pub files: IndexMap<String, String>,
|
||||
}
|
||||
impl Allowed {
|
||||
pub fn new() -> Self {
|
||||
Allowed {
|
||||
files: IndexMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[async_trait]
|
||||
impl WholeStreamCommand for Autoenv {
|
||||
@ -39,7 +46,7 @@ impl WholeStreamCommand for Autoenv {
|
||||
vec![Example {
|
||||
description: "Allow .nu-env file in current directory",
|
||||
example: "autoenv trust",
|
||||
result: None
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
@ -69,14 +69,12 @@ impl WholeStreamCommand for AutoenvTrust {
|
||||
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(),
|
||||
});
|
||||
let mut allowed: Allowed = toml::from_str(doc.as_str()).unwrap_or_else(|_| Allowed::new());
|
||||
|
||||
let file_to_allow = file_to_trust.to_string_lossy().to_string();
|
||||
let file_to_untrust = file_to_trust.to_string_lossy().to_string();
|
||||
allowed
|
||||
.dirs
|
||||
.insert(file_to_allow, hasher.finish().to_string());
|
||||
.files
|
||||
.insert(file_to_untrust, hasher.finish().to_string());
|
||||
|
||||
fs::write(config_path, toml::to_string(&allowed).unwrap())
|
||||
.expect("Couldn't write to toml file");
|
||||
|
@ -30,13 +30,22 @@ impl WholeStreamCommand for AutoenvUnTrust {
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
let dir_to_untrust = match args.call_info.evaluate(registry).await?.args.nth(0) {
|
||||
let file_to_untrust = 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 dir = crate::path::absolutize(std::env::current_dir()?, path);
|
||||
dir.push(".nu-env");
|
||||
dir
|
||||
}
|
||||
_ => {
|
||||
let mut dir = std::env::current_dir()?;
|
||||
dir.push(".nu-env");
|
||||
dir
|
||||
}
|
||||
};
|
||||
|
||||
let config_path = config::default_path_for(&Some(PathBuf::from("nu-env.toml")))?;
|
||||
|
||||
let mut file = match std::fs::OpenOptions::new()
|
||||
@ -53,10 +62,11 @@ impl WholeStreamCommand for AutoenvUnTrust {
|
||||
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.remove(&dir_to_untrust);
|
||||
let mut allowed: Allowed = toml::from_str(doc.as_str()).unwrap_or_else(|_| Allowed::new());
|
||||
|
||||
|
||||
let file_to_untrust = file_to_untrust.to_string_lossy().to_string();
|
||||
allowed.files.remove(&file_to_untrust);
|
||||
|
||||
fs::write(config_path, toml::to_string(&allowed).unwrap())
|
||||
.expect("Couldn't write to toml file");
|
||||
|
Loading…
Reference in New Issue
Block a user