2019-05-10 16:59:12 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::process::Process;
|
|
|
|
use crate::object::{DirEntry, ShellObject, Value};
|
2019-05-11 22:59:57 +00:00
|
|
|
use crate::Args;
|
2019-05-11 08:08:21 +00:00
|
|
|
use crate::{Command, CommandSuccess};
|
2019-05-10 16:59:12 +00:00
|
|
|
use derive_new::new;
|
2019-05-11 08:08:21 +00:00
|
|
|
use std::path::PathBuf;
|
2019-05-10 16:59:12 +00:00
|
|
|
use sysinfo::SystemExt;
|
|
|
|
|
|
|
|
#[derive(new)]
|
2019-05-11 08:08:21 +00:00
|
|
|
pub struct LsBlueprint;
|
2019-05-10 16:59:12 +00:00
|
|
|
|
2019-05-11 08:08:21 +00:00
|
|
|
impl crate::CommandBlueprint for LsBlueprint {
|
|
|
|
fn create(
|
|
|
|
&self,
|
2019-05-11 22:59:57 +00:00
|
|
|
args: Args,
|
2019-05-11 08:08:21 +00:00
|
|
|
host: &dyn crate::Host,
|
2019-05-10 16:59:12 +00:00
|
|
|
env: &mut crate::Environment,
|
2019-05-11 22:59:57 +00:00
|
|
|
) -> Result<Box<dyn Command>, ShellError> {
|
|
|
|
Ok(Box::new(Ls {
|
2019-05-11 08:08:21 +00:00
|
|
|
cwd: env.cwd().to_path_buf(),
|
2019-05-11 22:59:57 +00:00
|
|
|
}))
|
2019-05-11 08:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(new)]
|
|
|
|
pub struct Ls {
|
|
|
|
cwd: PathBuf,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl crate::Command for Ls {
|
|
|
|
fn run(&mut self) -> Result<CommandSuccess, ShellError> {
|
2019-05-10 16:59:12 +00:00
|
|
|
let entries =
|
2019-05-11 08:08:21 +00:00
|
|
|
std::fs::read_dir(&self.cwd).map_err((|e| ShellError::new(format!("{:?}", e))))?;
|
2019-05-10 16:59:12 +00:00
|
|
|
|
|
|
|
let mut shell_entries = vec![];
|
|
|
|
|
|
|
|
for entry in entries {
|
|
|
|
let value = Value::object(DirEntry::new(entry?)?);
|
|
|
|
shell_entries.push(value)
|
|
|
|
}
|
|
|
|
|
2019-05-11 08:08:21 +00:00
|
|
|
Ok(CommandSuccess {
|
|
|
|
value: Value::list(shell_entries),
|
|
|
|
action: vec![],
|
|
|
|
})
|
2019-05-10 16:59:12 +00:00
|
|
|
}
|
|
|
|
}
|