2019-05-10 18:59:12 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-06-03 09:41:28 +02:00
|
|
|
use crate::object::{dir_entry_dict, Primitive, Value};
|
2019-06-08 00:35:07 +02:00
|
|
|
use crate::parser::lexer::Spanned;
|
2019-05-13 19:30:51 +02:00
|
|
|
use crate::prelude::*;
|
2019-06-03 09:41:28 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
pub fn ls(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
|
2019-06-03 09:41:28 +02:00
|
|
|
let mut full_path = PathBuf::from(cwd);
|
|
|
|
match &args.positional.get(0) {
|
2019-06-08 00:35:07 +02:00
|
|
|
Some(Spanned {
|
|
|
|
item: Value::Primitive(Primitive::String(s)),
|
|
|
|
..
|
|
|
|
}) => full_path.push(Path::new(s)),
|
2019-06-03 09:41:28 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
2019-05-11 10:08:21 +02:00
|
|
|
|
2019-06-08 00:35:07 +02:00
|
|
|
let entries = std::fs::read_dir(&full_path);
|
|
|
|
|
|
|
|
let entries = match entries {
|
|
|
|
Err(e) => {
|
|
|
|
if let Some(s) = args.positional.get(0) {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
e.to_string(),
|
|
|
|
e.to_string(),
|
|
|
|
s.span,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::string(e.to_string()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(o) => o,
|
|
|
|
};
|
2019-05-16 02:21:46 +02:00
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
let mut shell_entries = VecDeque::new();
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
for entry in entries {
|
|
|
|
let value = Value::Object(dir_entry_dict(&entry?)?);
|
|
|
|
shell_entries.push_back(ReturnValue::Value(value))
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
2019-05-22 09:12:03 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
Ok(shell_entries.boxed())
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|