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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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