forked from nushell/nushell
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
--data
flag to metadata and set-metadata to allow persisting me…
…tadata This should help work around issues like nushell#7368 without having to propagate metadata everywhere (which would be a much larger refactor).
- Loading branch information
Showing
6 changed files
with
141 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use nu_engine::CallExt; | ||
use nu_protocol::ast::Call; | ||
use nu_protocol::engine::{Command, EngineState, Stack}; | ||
use nu_protocol::{ | ||
Category, Example, PipelineData, PipelineMetadata, Signature, SyntaxShape, Type, Value, Record | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct SetMetadata; | ||
|
||
impl Command for SetMetadata { | ||
fn name(&self) -> &str { | ||
"set-metadata" | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"Assigns the metadata from the metadata argument into the stream" | ||
} | ||
|
||
fn signature(&self) -> nu_protocol::Signature { | ||
Signature::build("set-metadata") | ||
.input_output_types(vec![(Type::Any, Type::Any)]) | ||
.allow_variants_without_examples(true) | ||
.required( | ||
"metadata_var_name", | ||
SyntaxShape::Record(vec![]), | ||
"metadata variable name", | ||
) | ||
.category(Category::Debug) | ||
} | ||
|
||
fn run( | ||
&self, | ||
engine_state: &EngineState, | ||
stack: &mut Stack, | ||
call: &Call, | ||
input: PipelineData, | ||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { | ||
let val: Value = call.req(engine_state, stack, 0)?; | ||
let metadata = get_source(val); | ||
Ok(input.set_metadata(metadata)) | ||
} | ||
|
||
fn examples(&self) -> Vec<Example> { | ||
vec![Example { | ||
description: "Set the metadata of a variable", | ||
example: "let a = (ls | metadata --data); $a.data | set-metadata $md", | ||
result: None, | ||
}] | ||
} | ||
} | ||
|
||
pub fn get_source(arg: Value) -> Option<PipelineMetadata> { | ||
let source_val = match &arg { | ||
Value::Record { val: record, .. } => record | ||
.iter() | ||
.find_map(|(col, val)| (col == "source").then_some(val)), | ||
_ => return None, | ||
}?; | ||
|
||
source_val.as_string().ok()?.parse().ok() | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_examples() { | ||
use crate::test_examples; | ||
|
||
test_examples(SetMetadata {}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters