2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-07-23 22:22:11 +00:00
|
|
|
use crate::context::CommandRegistry;
|
2019-05-15 18:14:51 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-05-22 07:12:03 +00:00
|
|
|
use crate::object::base::select_fields;
|
2019-05-15 18:14:51 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct Pick;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Pick {
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
pick(args, registry)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"pick"
|
|
|
|
}
|
2019-07-23 22:22:11 +00:00
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("pick").required("fields", SyntaxType::Any)
|
2019-05-22 07:12:03 +00:00
|
|
|
}
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pick(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let args = args.evaluate_once(registry)?;
|
|
|
|
let (input, args) = args.parts();
|
2019-05-15 18:14:51 +00:00
|
|
|
|
2019-07-23 22:22:11 +00:00
|
|
|
let fields: Result<Vec<String>, _> = args
|
|
|
|
.positional
|
|
|
|
.iter()
|
|
|
|
.flatten()
|
|
|
|
.map(|a| a.as_string())
|
|
|
|
.collect();
|
|
|
|
|
2019-05-22 07:12:03 +00:00
|
|
|
let fields = fields?;
|
2019-05-15 18:14:51 +00:00
|
|
|
|
2019-07-08 16:44:53 +00:00
|
|
|
let objects = input
|
2019-07-03 20:31:15 +00:00
|
|
|
.values
|
2019-08-05 08:54:29 +00:00
|
|
|
.map(move |value| select_fields(&value.item, &fields, value.tag()));
|
2019-05-15 18:14:51 +00:00
|
|
|
|
2019-07-03 20:31:15 +00:00
|
|
|
Ok(objects.from_input_stream())
|
2019-05-15 18:14:51 +00:00
|
|
|
}
|