mirror of
https://github.com/nushell/nushell.git
synced 2025-05-19 17:30:45 +02:00
Optional directory arg
This commit is contained in:
parent
1cf177cbfd
commit
2224d07b8a
@ -19,7 +19,7 @@ impl WholeStreamCommand for Autoenv {
|
||||
}
|
||||
async fn run(
|
||||
&self,
|
||||
args: CommandArgs,
|
||||
_args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let registry = registry.clone();
|
||||
|
@ -1,7 +1,8 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::prelude::*;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
||||
use nu_protocol::SyntaxShape;
|
||||
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::hash::{Hash, Hasher};
|
||||
@ -21,11 +22,11 @@ impl WholeStreamCommand for AutoenvTrust {
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("autoenv trust")
|
||||
Signature::build("autoenv trust").optional("dir", SyntaxShape::String, "Directory to allow")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Trust a .nu-env file in the current directory"
|
||||
"Trust a .nu-env file in the current or given directory"
|
||||
}
|
||||
|
||||
async fn run(
|
||||
@ -33,8 +34,19 @@ impl WholeStreamCommand for AutoenvTrust {
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let current_dir = std::env::current_dir()?;
|
||||
let content = std::fs::read_to_string(current_dir.join(".nu-env"))?;
|
||||
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
let dir_to_allow = match args.call_info.evaluate(registry).await?.args.nth(0) {
|
||||
Some(Value {
|
||||
value: UntaggedValue::Primitive(Primitive::String(ref path)),
|
||||
tag: _,
|
||||
}) => path.clone(),
|
||||
_ => std::env::current_dir()?.to_string_lossy().to_string(),
|
||||
};
|
||||
|
||||
let mut env_file_to_allow = dir_to_allow.clone();
|
||||
env_file_to_allow.push_str(".nu-env");
|
||||
let content = std::fs::read_to_string(env_file_to_allow)?;
|
||||
let mut hasher = DefaultHasher::new();
|
||||
content.hash(&mut hasher);
|
||||
|
||||
@ -53,14 +65,13 @@ impl WholeStreamCommand for AutoenvTrust {
|
||||
dirs: IndexMap::new(),
|
||||
});
|
||||
allowed.dirs.insert(
|
||||
current_dir.to_string_lossy().to_string(),
|
||||
dir_to_allow,
|
||||
hasher.finish().to_string(),
|
||||
);
|
||||
|
||||
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(
|
||||
UntaggedValue::string(".nu-env trusted!").into_value(tag),
|
||||
)))
|
||||
|
Loading…
Reference in New Issue
Block a user