autoenv trust toml

This commit is contained in:
Sam Hedin 2020-06-21 14:57:13 +02:00
parent a54a596afd
commit 992522af66
4 changed files with 107 additions and 12 deletions

40
Cargo.lock generated
View File

@ -81,6 +81,12 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
[[package]]
name = "ascii"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eab1c04a571841102f5345a8fc0f6bb3d31c315dec879b5c6e42e40ce7ffa34e"
[[package]]
name = "async-attributes"
version = "1.1.1"
@ -489,6 +495,19 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "combine"
version = "3.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da3da6baa321ec19e1cc41d31bf599f00c783d0517095cdaf0332e3fe8d20680"
dependencies = [
"ascii",
"byteorder",
"either",
"memchr",
"unreachable",
]
[[package]]
name = "config"
version = "0.9.3"
@ -2282,6 +2301,7 @@ dependencies = [
"termcolor",
"textwrap",
"toml 0.5.6",
"toml_edit",
"trash",
"typetag",
"umask",
@ -3964,6 +3984,17 @@ dependencies = [
"serde 1.0.110",
]
[[package]]
name = "toml_edit"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09391a441b373597cf0888d2b052dcf82c5be4fee05da3636ae30fb57aad8484"
dependencies = [
"chrono",
"combine",
"linked-hash-map 0.5.3",
]
[[package]]
name = "trash"
version = "1.0.1"
@ -4066,6 +4097,15 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
[[package]]
name = "unreachable"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56"
dependencies = [
"void",
]
[[package]]
name = "uom"
version = "0.26.0"

View File

@ -82,6 +82,7 @@ term = "0.5.2"
termcolor = "1.1.0"
textwrap = {version = "0.11.0", features = ["term_size"]}
toml = "0.5.6"
toml_edit = "0.2.0"
typetag = "0.1.4"
umask = "1.0.0"
unicode-xid = "0.2.0"

View File

@ -1,12 +1,20 @@
use crate::commands::WholeStreamCommand;
use crate::data::value::format_leaf;
use crate::prelude::*;
use directories::ProjectDirs;
use futures::StreamExt;
use std::io::Write;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
use nu_source::AnchorLocation;
use std::fs::OpenOptions;
use std::hash::{Hash, Hasher};
use std::io::Write;
use toml_edit::{Document, value};
use std::io::Read;
use std::{
collections::hash_map::DefaultHasher,
fs::{self, OpenOptions},
path::PathBuf,
};
pub struct AutoenvTrust;
@ -29,15 +37,59 @@ impl WholeStreamCommand for AutoenvTrust {
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let mut file = OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open("autoenv.txt")
.unwrap();
let current_dir = std::env::current_dir()?;
let content = std::fs::read_to_string(current_dir.join(".nu-env"))?;
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 = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(config_path.clone())?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let mut toml_table =
match std::fs::read_to_string(config_path.clone())?.parse::<toml::Value>() {
Ok(toml_doc) => {
let table = match toml_doc.get("allowed-files") {
Some(v) => v.clone(),
None => r#"[allowed-files]"#.parse::<toml::Value>().unwrap(),
};
table.as_table().unwrap().clone()
}
Err(_) => {
let mut table = toml::value::Table::new();
table.insert("allowed-files".to_string(), toml::Value::from(""));
table
// let table = "[allowed-files]".parse::<toml::Value>().unwrap();
// table.as_table().unwrap().clone()
}
};
toml_table.insert(
current_dir.to_string_lossy().to_string(),
toml::Value::try_from(hasher.finish().to_string())?,
);
let toml_string: String = toml::to_string(&toml_table).expect(";");
fs::write(config_path, toml_string).expect("Couldn't write to toml file");
write!(&mut file, "I'm here!\n").unwrap();
let tag = args.call_info.name_tag.clone();
Ok(OutputStream::one(ReturnSuccess::value(UntaggedValue::string("success!").into_value(tag))))
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(".nu-env trusted!").into_value(tag),
)))
}
fn is_binary(&self) -> bool {
false
}
fn examples(&self) -> Vec<Example> {
Vec::new()
}
}

View File

@ -31,3 +31,5 @@ returning =None=, which completely skips running the code for dealing with direc
However, it seems that with the current version the already existing nushell functionality takes care of this, in every case I can come up with.
Because of that, I just removed the code. It's still there, in the venerable git log if anything ever changes `acd3215b` `Remove overwritten values tracking, as it is not needed`.
(I am making a note of this as I half expect someone to come up with a scenario which requires this to be re-added.)
** mknuenv
mknuenv(key=val, key=val)