Config commands to engine p (#3408)

* config get command

* config remove command

* config set command

* config command

* config commands
This commit is contained in:
Fernando Herrera 2021-05-12 03:01:16 +01:00 committed by GitHub
parent 3795c2a39d
commit 758c128147
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 88 additions and 115 deletions

View File

@ -1,7 +1,7 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
use nu_protocol::{Signature, UntaggedValue};
pub struct SubCommand;
@ -18,7 +18,7 @@ impl WholeStreamCommand for SubCommand {
"clear the config"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
clear(args)
}
@ -31,22 +31,22 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn clear(args: CommandArgs) -> Result<ActionStream, ShellError> {
pub fn clear(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let ctx = EvaluationContext::from_args(&args);
let result = if let Some(global_cfg) = &mut args.configs().lock().global_config {
global_cfg.vars.clear();
global_cfg.write()?;
ctx.reload_config(global_cfg)?;
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::Row(global_cfg.vars.clone().into()).into_value(args.call_info.name_tag),
)))
let value = UntaggedValue::Row(global_cfg.vars.clone().into()).into_value(name);
Ok(OutputStream::one(value))
} else {
Ok(vec![ReturnSuccess::value(UntaggedValue::Error(
crate::commands::config::err_no_global_cfg_present(),
))]
.into_iter()
.to_action_stream())
let value = UntaggedValue::Error(crate::commands::config::err_no_global_cfg_present())
.into_value(name);
Ok(OutputStream::one(value))
};
result

View File

@ -2,8 +2,7 @@ use crate::prelude::*;
use nu_engine::CommandArgs;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
use nu_stream::ActionStream;
use nu_protocol::{Signature, UntaggedValue};
pub struct Command;
@ -20,22 +19,19 @@ impl WholeStreamCommand for Command {
"Configuration management."
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
if let Some(global_cfg) = &args.configs().lock().global_config {
let result = global_cfg.vars.clone();
Ok(vec![ReturnSuccess::value(
UntaggedValue::Row(result.into()).into_value(name),
)]
.into_iter()
.to_action_stream())
let value = UntaggedValue::Row(result.into()).into_value(name);
Ok(OutputStream::one(value))
} else {
Ok(vec![ReturnSuccess::value(UntaggedValue::Error(
crate::commands::config::err_no_global_cfg_present(),
))]
.into_iter()
.to_action_stream())
let value = UntaggedValue::Error(crate::commands::config::err_no_global_cfg_present())
.into_value(name);
Ok(OutputStream::one(value))
}
}
}

View File

@ -1,15 +1,10 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
pub struct SubCommand;
#[derive(Deserialize)]
pub struct Arguments {
column_path: ColumnPath,
}
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"config get"
@ -27,7 +22,7 @@ impl WholeStreamCommand for SubCommand {
"Gets a value from the config"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
get(args)
}
@ -40,11 +35,12 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn get(args: CommandArgs) -> Result<ActionStream, ShellError> {
pub fn get(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let ctx = EvaluationContext::from_args(&args);
let args = args.evaluate_once()?;
let (Arguments { column_path }, _) = args.process()?;
let column_path = args.req(0)?;
let result = if let Some(global_cfg) = &ctx.configs.lock().global_config {
let result = UntaggedValue::row(global_cfg.vars.clone()).into_value(&name);
@ -53,15 +49,14 @@ pub fn get(args: CommandArgs) -> Result<ActionStream, ShellError> {
Value {
value: UntaggedValue::Table(list),
..
} => list.into_iter().to_action_stream(),
x => ActionStream::one(ReturnSuccess::value(x)),
} => OutputStream::from_stream(list.into_iter()),
x => OutputStream::one(x),
})
} else {
Ok(vec![ReturnSuccess::value(UntaggedValue::Error(
crate::commands::config::err_no_global_cfg_present(),
))]
.into_iter()
.to_action_stream())
let value = UntaggedValue::Error(crate::commands::config::err_no_global_cfg_present())
.into_value(name);
Ok(OutputStream::one(value))
};
result

View File

@ -1,7 +1,7 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue};
use nu_protocol::{Primitive, Signature, UntaggedValue};
pub struct SubCommand;
@ -18,7 +18,7 @@ impl WholeStreamCommand for SubCommand {
"return the path to the config file"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
path(args)
}
@ -31,16 +31,18 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn path(args: CommandArgs) -> Result<ActionStream, ShellError> {
pub fn path(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
if let Some(global_cfg) = &mut args.configs().lock().global_config {
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::Primitive(Primitive::FilePath(global_cfg.file_path.clone())),
)))
let value = UntaggedValue::Primitive(Primitive::FilePath(global_cfg.file_path.clone()))
.into_value(name);
Ok(OutputStream::one(value))
} else {
Ok(vec![ReturnSuccess::value(UntaggedValue::Error(
crate::commands::config::err_no_global_cfg_present(),
))]
.into_iter()
.to_action_stream())
let value = UntaggedValue::Error(crate::commands::config::err_no_global_cfg_present())
.into_value(name);
Ok(OutputStream::one(value))
}
}

View File

@ -1,16 +1,11 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
pub struct SubCommand;
#[derive(Deserialize)]
pub struct Arguments {
remove: Tagged<String>,
}
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"config remove"
@ -28,7 +23,7 @@ impl WholeStreamCommand for SubCommand {
"Removes a value from the config"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
remove(args)
}
@ -41,9 +36,11 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn remove(args: CommandArgs) -> Result<ActionStream, ShellError> {
pub fn remove(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let ctx = EvaluationContext::from_args(&args);
let (Arguments { remove }, _) = args.process()?;
let args = args.evaluate_once()?;
let remove: Tagged<String> = args.req(0)?;
let key = remove.to_string();
@ -52,11 +49,10 @@ pub fn remove(args: CommandArgs) -> Result<ActionStream, ShellError> {
global_cfg.vars.swap_remove(&key);
global_cfg.write()?;
ctx.reload_config(global_cfg)?;
Ok(vec![ReturnSuccess::value(
UntaggedValue::row(global_cfg.vars.clone()).into_value(remove.tag()),
)]
.into_iter()
.to_action_stream())
let value: Value = UntaggedValue::row(global_cfg.vars.clone()).into_value(remove.tag);
Ok(OutputStream::one(value))
} else {
Err(ShellError::labeled_error(
"Key does not exist in config",
@ -65,11 +61,10 @@ pub fn remove(args: CommandArgs) -> Result<ActionStream, ShellError> {
))
}
} else {
Ok(vec![ReturnSuccess::value(UntaggedValue::Error(
crate::commands::config::err_no_global_cfg_present(),
))]
.into_iter()
.to_action_stream())
let value = UntaggedValue::Error(crate::commands::config::err_no_global_cfg_present())
.into_value(name);
Ok(OutputStream::one(value))
};
result

View File

@ -1,16 +1,10 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
pub struct SubCommand;
#[derive(Deserialize)]
pub struct Arguments {
column_path: ColumnPath,
value: Value,
}
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"config set"
@ -26,7 +20,7 @@ impl WholeStreamCommand for SubCommand {
"Sets a value in the config"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
set(args)
}
@ -56,16 +50,13 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn set(args: CommandArgs) -> Result<ActionStream, ShellError> {
pub fn set(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let ctx = EvaluationContext::from_args(&args);
let (
Arguments {
column_path,
mut value,
},
_,
) = args.process()?;
let args = args.evaluate_once()?;
let column_path = args.req(0)?;
let mut value: Value = args.req(1)?;
let result = if let Some(global_cfg) = &mut ctx.configs.lock().global_config {
let configuration = UntaggedValue::row(global_cfg.vars.clone()).into_value(&name);
@ -85,19 +76,17 @@ pub fn set(args: CommandArgs) -> Result<ActionStream, ShellError> {
global_cfg.write()?;
ctx.reload_config(global_cfg)?;
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::row(global_cfg.vars.clone()).into_value(name),
)))
let value = UntaggedValue::row(global_cfg.vars.clone()).into_value(name);
Ok(OutputStream::one(value))
}
Ok(_) => Ok(ActionStream::empty()),
Ok(_) => Ok(OutputStream::empty()),
Err(reason) => Err(reason),
}
} else {
Ok(vec![ReturnSuccess::value(UntaggedValue::Error(
crate::commands::config::err_no_global_cfg_present(),
))]
.into_iter()
.to_action_stream())
let value = UntaggedValue::Error(crate::commands::config::err_no_global_cfg_present())
.into_value(name);
Ok(OutputStream::one(value))
};
result

View File

@ -1,16 +1,11 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
pub struct SubCommand;
#[derive(Deserialize)]
pub struct Arguments {
set_into: Tagged<String>,
}
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"config set_into"
@ -28,7 +23,7 @@ impl WholeStreamCommand for SubCommand {
"Sets a value in the config"
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
set_into(args)
}
@ -41,20 +36,22 @@ impl WholeStreamCommand for SubCommand {
}
}
pub fn set_into(args: CommandArgs) -> Result<ActionStream, ShellError> {
pub fn set_into(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let ctx = EvaluationContext::from_args(&args);
let (Arguments { set_into: v }, input) = args.process()?;
let args = args.evaluate_once()?;
let rows: Vec<Value> = input.collect();
let key = v.to_string();
let set_into: Tagged<String> = args.req(0)?;
let rows: Vec<Value> = args.input.collect();
let key = set_into.to_string();
let result = if let Some(global_cfg) = &mut ctx.configs.lock().global_config {
if rows.is_empty() {
return Err(ShellError::labeled_error(
"No values given for set_into",
"needs value(s) from pipeline",
v.tag(),
set_into.tag(),
));
} else if rows.len() == 1 {
// A single value
@ -71,15 +68,14 @@ pub fn set_into(args: CommandArgs) -> Result<ActionStream, ShellError> {
global_cfg.write()?;
ctx.reload_config(global_cfg)?;
Ok(ActionStream::one(ReturnSuccess::value(
UntaggedValue::row(global_cfg.vars.clone()).into_value(name),
)))
let value = UntaggedValue::row(global_cfg.vars.clone()).into_value(name);
Ok(OutputStream::one(value))
} else {
Ok(vec![ReturnSuccess::value(UntaggedValue::Error(
crate::commands::config::err_no_global_cfg_present(),
))]
.into_iter()
.to_action_stream())
let value = UntaggedValue::Error(crate::commands::config::err_no_global_cfg_present())
.into_value(name);
Ok(OutputStream::one(value))
};
result