From 992522af6665333d8360ed8e8c052ce2e9ed9477 Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Sun, 21 Jun 2020 14:57:13 +0200 Subject: [PATCH] autoenv trust toml --- Cargo.lock | 40 +++++++++++ crates/nu-cli/Cargo.toml | 1 + crates/nu-cli/src/commands/autoenv_trust.rs | 74 ++++++++++++++++++--- crates/nu-cli/src/env/TODO.org | 4 +- 4 files changed, 107 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 660f439981..6d3d64ef39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index c55dfa655a..caf30fe2ce 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -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" diff --git a/crates/nu-cli/src/commands/autoenv_trust.rs b/crates/nu-cli/src/commands/autoenv_trust.rs index 7a8fa652e0..4c64f6a885 100644 --- a/crates/nu-cli/src/commands/autoenv_trust.rs +++ b/crates/nu-cli/src/commands/autoenv_trust.rs @@ -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 { - 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::() { + Ok(toml_doc) => { + let table = match toml_doc.get("allowed-files") { + Some(v) => v.clone(), + None => r#"[allowed-files]"#.parse::().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::().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), + ))) } -} \ No newline at end of file + fn is_binary(&self) -> bool { + false + } + fn examples(&self) -> Vec { + Vec::new() + } +} diff --git a/crates/nu-cli/src/env/TODO.org b/crates/nu-cli/src/env/TODO.org index 9f72ca3f2d..911020794a 100644 --- a/crates/nu-cli/src/env/TODO.org +++ b/crates/nu-cli/src/env/TODO.org @@ -30,4 +30,6 @@ returning =None=, which completely skips running the code for dealing with direc There was previously functionality to track overwritten environment variables, which was used to restore any values that are overwritten. 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.) \ No newline at end of file + (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) \ No newline at end of file