mirror of
https://github.com/nushell/nushell
synced 2024-12-28 05:53:09 +00:00
'update' command (#333)
This commit is contained in:
parent
08d316f6a7
commit
e76451866d
3 changed files with 123 additions and 0 deletions
|
@ -119,6 +119,7 @@ pub fn create_default_context() -> EngineState {
|
||||||
ToJson,
|
ToJson,
|
||||||
Touch,
|
Touch,
|
||||||
Use,
|
Use,
|
||||||
|
Update,
|
||||||
Where,
|
Where,
|
||||||
WithEnv,
|
WithEnv,
|
||||||
Wrap,
|
Wrap,
|
||||||
|
|
|
@ -10,6 +10,7 @@ mod range;
|
||||||
mod reverse;
|
mod reverse;
|
||||||
mod select;
|
mod select;
|
||||||
mod shuffle;
|
mod shuffle;
|
||||||
|
mod update;
|
||||||
mod where_;
|
mod where_;
|
||||||
mod wrap;
|
mod wrap;
|
||||||
mod zip;
|
mod zip;
|
||||||
|
@ -26,6 +27,7 @@ pub use range::Range;
|
||||||
pub use reverse::Reverse;
|
pub use reverse::Reverse;
|
||||||
pub use select::Select;
|
pub use select::Select;
|
||||||
pub use shuffle::Shuffle;
|
pub use shuffle::Shuffle;
|
||||||
|
pub use update::Update;
|
||||||
pub use where_::Where;
|
pub use where_::Where;
|
||||||
pub use wrap::Wrap;
|
pub use wrap::Wrap;
|
||||||
pub use zip::Zip;
|
pub use zip::Zip;
|
||||||
|
|
120
crates/nu-command/src/filters/update.rs
Normal file
120
crates/nu-command/src/filters/update.rs
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
use nu_engine::{eval_block, CallExt};
|
||||||
|
use nu_protocol::ast::{Call, CellPath};
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Update;
|
||||||
|
|
||||||
|
impl Command for Update {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"update"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("update")
|
||||||
|
.required(
|
||||||
|
"field",
|
||||||
|
SyntaxShape::CellPath,
|
||||||
|
"the name of the column to update",
|
||||||
|
)
|
||||||
|
.required(
|
||||||
|
"replacement value",
|
||||||
|
SyntaxShape::Any,
|
||||||
|
"the new value to give the cell(s)",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Update an existing column to have a new value."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
update(engine_state, stack, call, input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
description: "Update a column value",
|
||||||
|
example: "echo {'name': 'nu', 'stars': 5} | update name 'Nushell'",
|
||||||
|
result: Some(Value::Record { cols: vec!["name".into(), "stars".into()], vals: vec![Value::test_string("Nushell"), Value::test_int(5)], span: Span::unknown()}),
|
||||||
|
}, Example {
|
||||||
|
description: "Use in block form for more involved updating logic",
|
||||||
|
example: "echo [[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors { get authors | str collect ',' }",
|
||||||
|
result: Some(Value::List { vals: vec![Value::Record { cols: vec!["project".into(), "authors".into()], vals: vec![Value::test_string("nu"), Value::test_string("Andrés,JT,Yehuda")], span: Span::unknown()}], span: Span::unknown()}),
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let span = call.head;
|
||||||
|
|
||||||
|
let cell_path: CellPath = call.req(engine_state, stack, 0)?;
|
||||||
|
let replacement: Value = call.req(engine_state, stack, 1)?;
|
||||||
|
let engine_state = engine_state.clone();
|
||||||
|
let ctrlc = engine_state.ctrlc.clone();
|
||||||
|
|
||||||
|
// Replace is a block, so set it up and run it instead of using it as the replacement
|
||||||
|
if let Ok(block_id) = replacement.as_block() {
|
||||||
|
let block = engine_state.get_block(block_id).clone();
|
||||||
|
|
||||||
|
let mut stack = stack.collect_captures(&block.captures);
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |mut input| {
|
||||||
|
if let Some(var) = block.signature.get_positional(0) {
|
||||||
|
if let Some(var_id) = &var.var_id {
|
||||||
|
stack.add_var(*var_id, input.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let output = eval_block(
|
||||||
|
&engine_state,
|
||||||
|
&mut stack,
|
||||||
|
&block,
|
||||||
|
input.clone().into_pipeline_data(),
|
||||||
|
);
|
||||||
|
|
||||||
|
match output {
|
||||||
|
Ok(pd) => {
|
||||||
|
if let Err(e) =
|
||||||
|
input.replace_data_at_cell_path(&cell_path.members, pd.into_value(span))
|
||||||
|
{
|
||||||
|
return Value::Error { error: e };
|
||||||
|
}
|
||||||
|
|
||||||
|
input
|
||||||
|
}
|
||||||
|
Err(e) => Value::Error { error: e },
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ctrlc,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
input.map(
|
||||||
|
move |mut input| {
|
||||||
|
let replacement = replacement.clone();
|
||||||
|
|
||||||
|
if let Err(e) = input.replace_data_at_cell_path(&cell_path.members, replacement) {
|
||||||
|
return Value::Error { error: e };
|
||||||
|
}
|
||||||
|
|
||||||
|
input
|
||||||
|
},
|
||||||
|
ctrlc,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue