Use serde for autoenv

This commit is contained in:
Sam Hedin 2020-06-21 19:40:32 +02:00
parent 500192a642
commit 1cf177cbfd
2 changed files with 17 additions and 15 deletions

View File

@ -1,7 +1,7 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue, Value, Signature};
use nu_protocol::{ReturnSuccess, UntaggedValue, Signature};
pub struct Autoenv;

View File

@ -2,13 +2,18 @@ use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
use serde::Deserialize;
use serde::Serialize;
use std::hash::{Hash, Hasher};
use std::io::{Read, Write};
use std::{collections::hash_map::DefaultHasher, fs, path::PathBuf};
use serde_derive::Serialize;
pub struct AutoenvTrust;
#[derive(Deserialize, Serialize)]
struct Allowed {
pub dirs: IndexMap<String, String>,
}
#[async_trait]
impl WholeStreamCommand for AutoenvTrust {
fn name(&self) -> &str {
@ -44,19 +49,16 @@ impl WholeStreamCommand for AutoenvTrust {
let mut doc = String::new();
file.read_to_string(&mut doc)?;
let mut toml_doc = doc.parse::<toml::Value>().unwrap();
let mut empty = toml::value::Value::Table(toml::value::Table::new());
toml_doc
.get_mut("allowed-files")
.unwrap_or_else(|| &mut empty)
.as_table_mut()
.unwrap()
.insert(
current_dir.to_string_lossy().to_string(),
toml::Value::try_from(hasher.finish().to_string())?,
);
let mut allowed: Allowed = toml::from_str(doc.as_str()).unwrap_or_else(|_| Allowed {
dirs: IndexMap::new(),
});
allowed.dirs.insert(
current_dir.to_string_lossy().to_string(),
hasher.finish().to_string(),
);
fs::write(config_path, toml_doc.as_str().unwrap()).expect("Couldn't write to toml file");
fs::write(config_path, toml::to_string(&allowed).unwrap())
.expect("Couldn't write to toml file");
let tag = args.call_info.name_tag.clone();
Ok(OutputStream::one(ReturnSuccess::value(