2019-05-15 18:14:51 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::base::select;
|
|
|
|
use crate::object::process::Process;
|
2019-05-15 22:23:36 +00:00
|
|
|
use crate::object::{dir_entry_dict, Value};
|
2019-05-15 18:14:51 +00:00
|
|
|
use crate::prelude::*;
|
|
|
|
use crate::Args;
|
|
|
|
use derive_new::new;
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use sysinfo::SystemExt;
|
|
|
|
|
|
|
|
#[derive(new)]
|
|
|
|
pub struct SelectBlueprint;
|
|
|
|
|
|
|
|
impl crate::CommandBlueprint for SelectBlueprint {
|
|
|
|
fn create(
|
|
|
|
&self,
|
|
|
|
args: Vec<Value>,
|
|
|
|
host: &dyn Host,
|
|
|
|
env: &mut Environment,
|
|
|
|
) -> Result<Box<dyn Command>, ShellError> {
|
|
|
|
if args.is_empty() {
|
|
|
|
return Err(ShellError::string("take requires an integer"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let fields: Result<_, _> = args.iter().map(|a| a.as_string()).collect();
|
|
|
|
|
|
|
|
Ok(Box::new(Select { fields: fields? }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(new)]
|
|
|
|
pub struct Select {
|
|
|
|
fields: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl crate::Command for Select {
|
|
|
|
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
|
|
|
|
let objects = stream
|
|
|
|
.iter()
|
2019-05-15 21:44:06 +00:00
|
|
|
.map(|item| Value::Object(select(item, &self.fields)))
|
2019-05-15 18:14:51 +00:00
|
|
|
.map(|item| ReturnValue::Value(item))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(objects)
|
|
|
|
}
|
|
|
|
}
|