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-02 03:18:39 +01:00
|
|
|
use nu_protocol::{Category, 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 {
|
2021-11-17 05:22:37 +01:00
|
|
|
Signature::build("get")
|
|
|
|
.required(
|
|
|
|
"cell_path",
|
|
|
|
SyntaxShape::CellPath,
|
|
|
|
"the cell path to the data",
|
|
|
|
)
|
2022-01-02 03:18:39 +01:00
|
|
|
.switch(
|
|
|
|
"ignore-errors",
|
|
|
|
"return nothing if path can't be found",
|
|
|
|
Some('i'),
|
|
|
|
)
|
2021-11-17 05:22:37 +01:00
|
|
|
.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> {
|
2021-10-25 08:31:39 +02:00
|
|
|
let cell_path: CellPath = call.req(engine_state, stack, 0)?;
|
2022-01-02 03:18:39 +01:00
|
|
|
let ignore_errors = call.has_flag("ignore-errors");
|
2021-10-02 04:59:11 +02:00
|
|
|
|
2022-01-02 03:18:39 +01:00
|
|
|
let output = input
|
2021-12-19 08:46:13 +01:00
|
|
|
.follow_cell_path(&cell_path.members, call.head)
|
2022-01-02 03:18:39 +01:00
|
|
|
.map(|x| x.into_pipeline_data());
|
|
|
|
|
|
|
|
if ignore_errors {
|
|
|
|
match output {
|
|
|
|
Ok(output) => Ok(output),
|
|
|
|
Err(_) => Ok(Value::Nothing { span: call.head }.into_pipeline_data()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output
|
|
|
|
}
|
2021-10-02 04:59:11 +02:00
|
|
|
}
|
|
|
|
}
|