nushell/crates/nu-command/src/filters/get.rs

112 lines
3.3 KiB
Rust
Raw Normal View History

2021-10-02 04:59:11 +02:00
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
2021-10-25 18:58:58 +02:00
use nu_protocol::engine::{Command, EngineState, Stack};
2022-01-16 16:40:11 +01:00
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Signature,
SyntaxShape, Value,
};
2021-10-02 04:59:11 +02:00
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-10-02 04:59:11 +02:00
pub struct Get;
impl Command for Get {
fn name(&self) -> &str {
"get"
}
fn usage(&self) -> &str {
"Extract data using a cell path."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("get")
.required(
"cell_path",
SyntaxShape::CellPath,
"the cell path to the data",
)
2022-01-16 16:40:11 +01:00
.rest("rest", SyntaxShape::CellPath, "additional cell paths")
2022-01-02 03:18:39 +01:00
.switch(
"ignore-errors",
"return nothing if path can't be found",
Some('i'),
)
.category(Category::Filters)
2021-10-02 04:59:11 +02:00
}
fn run(
&self,
2021-10-25 08:31:39 +02:00
engine_state: &EngineState,
stack: &mut Stack,
2021-10-02 04:59:11 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2022-01-16 16:40:11 +01:00
let span = call.head;
2021-10-25 08:31:39 +02:00
let cell_path: CellPath = call.req(engine_state, stack, 0)?;
2022-01-16 16:40:11 +01:00
let rest: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
2022-01-02 03:18:39 +01:00
let ignore_errors = call.has_flag("ignore-errors");
2022-01-16 16:40:11 +01:00
let ctrlc = engine_state.ctrlc.clone();
let metadata = input.metadata();
2021-10-02 04:59:11 +02:00
2022-01-16 16:40:11 +01:00
if rest.is_empty() {
let output = input
.follow_cell_path(&cell_path.members, call.head)
.map(|x| x.into_pipeline_data());
2022-01-02 03:18:39 +01:00
2022-01-16 16:40:11 +01:00
if ignore_errors {
match output {
Ok(output) => Ok(output),
Err(_) => Ok(Value::Nothing { span: call.head }.into_pipeline_data()),
}
} else {
output
2022-01-02 03:18:39 +01:00
}
} else {
2022-01-16 16:40:11 +01:00
let mut output = vec![];
let paths = vec![cell_path].into_iter().chain(rest);
let input = input.into_value(span);
for path in paths {
let val = input.clone().follow_cell_path(&path.members);
if ignore_errors {
if let Ok(val) = val {
output.push(val);
}
} else {
output.push(val?);
}
}
Ok(output.into_iter().into_pipeline_data(ctrlc))
2022-01-02 03:18:39 +01:00
}
.map(|x| x.set_metadata(metadata))
2021-10-02 04:59:11 +02:00
}
2022-01-16 16:40:11 +01:00
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Extract the name of files as a list",
example: "ls | get name",
result: None,
},
Example {
description: "Extract the name of the 3rd entry of a file list",
example: "ls | get name.2",
result: None,
},
Example {
description: "Extract the name of the 3rd entry of a file list (alternative)",
example: "ls | get 2.name",
result: None,
},
Example {
description: "Extract the cpu list from the sys information record",
example: "sys | get cpu",
result: None,
},
]
}
2021-10-02 04:59:11 +02:00
}