mirror of
https://github.com/nushell/nushell.git
synced 2025-05-19 01:10:48 +02:00
Use serde for autoenv
This commit is contained in:
parent
500192a642
commit
1cf177cbfd
@ -1,7 +1,7 @@
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue, Value, Signature};
|
use nu_protocol::{ReturnSuccess, UntaggedValue, Signature};
|
||||||
|
|
||||||
pub struct Autoenv;
|
pub struct Autoenv;
|
||||||
|
|
||||||
|
@ -2,13 +2,18 @@ use crate::commands::WholeStreamCommand;
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde::Serialize;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::{collections::hash_map::DefaultHasher, fs, path::PathBuf};
|
use std::{collections::hash_map::DefaultHasher, fs, path::PathBuf};
|
||||||
use serde_derive::Serialize;
|
|
||||||
|
|
||||||
pub struct AutoenvTrust;
|
pub struct AutoenvTrust;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
struct Allowed {
|
||||||
|
pub dirs: IndexMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl WholeStreamCommand for AutoenvTrust {
|
impl WholeStreamCommand for AutoenvTrust {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
@ -44,19 +49,16 @@ impl WholeStreamCommand for AutoenvTrust {
|
|||||||
let mut doc = String::new();
|
let mut doc = String::new();
|
||||||
file.read_to_string(&mut doc)?;
|
file.read_to_string(&mut doc)?;
|
||||||
|
|
||||||
let mut toml_doc = doc.parse::<toml::Value>().unwrap();
|
let mut allowed: Allowed = toml::from_str(doc.as_str()).unwrap_or_else(|_| Allowed {
|
||||||
let mut empty = toml::value::Value::Table(toml::value::Table::new());
|
dirs: IndexMap::new(),
|
||||||
toml_doc
|
});
|
||||||
.get_mut("allowed-files")
|
allowed.dirs.insert(
|
||||||
.unwrap_or_else(|| &mut empty)
|
current_dir.to_string_lossy().to_string(),
|
||||||
.as_table_mut()
|
hasher.finish().to_string(),
|
||||||
.unwrap()
|
);
|
||||||
.insert(
|
|
||||||
current_dir.to_string_lossy().to_string(),
|
|
||||||
toml::Value::try_from(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();
|
let tag = args.call_info.name_tag.clone();
|
||||||
Ok(OutputStream::one(ReturnSuccess::value(
|
Ok(OutputStream::one(ReturnSuccess::value(
|
||||||
|
Loading…
Reference in New Issue
Block a user