mirror of
https://github.com/nushell/nushell.git
synced 2025-08-14 02:28:35 +02:00
autoenv trust toml
This commit is contained in:
@ -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()
|
||||
}
|
||||
}
|
||||
|
4
crates/nu-cli/src/env/TODO.org
vendored
4
crates/nu-cli/src/env/TODO.org
vendored
@ -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.)
|
||||
(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)
|
Reference in New Issue
Block a user