All tests pass

This commit is contained in:
Jonathan Turner
2019-08-09 17:36:43 +12:00
parent aadacc2d36
commit 83030094e0
11 changed files with 236 additions and 145 deletions

View File

@@ -115,6 +115,7 @@ impl CommandArgs {
callback: fn(T, RunnableContext) -> Result<OutputStream, ShellError>,
) -> Result<RunnableArgs<T>, ShellError> {
let shell_manager = self.shell_manager.clone();
let source_map = self.call_info.source_map.clone();
let host = self.host.clone();
let args = self.evaluate_once(registry)?;
let (input, args) = args.split();
@@ -128,6 +129,7 @@ impl CommandArgs {
commands: registry.clone(),
shell_manager,
name: name_span,
source_map,
host,
},
callback,
@@ -146,6 +148,7 @@ impl CommandArgs {
};
let shell_manager = self.shell_manager.clone();
let source_map = self.call_info.source_map.clone();
let host = self.host.clone();
let args = self.evaluate_once(registry)?;
let (input, args) = args.split();
@@ -159,6 +162,7 @@ impl CommandArgs {
commands: registry.clone(),
shell_manager,
name: name_span,
source_map,
host,
},
raw_args,
@@ -172,6 +176,7 @@ pub struct RunnableContext {
pub shell_manager: ShellManager,
pub host: Arc<Mutex<dyn Host>>,
pub commands: CommandRegistry,
pub source_map: SourceMap,
pub name: Span,
}

View File

@@ -1,57 +1,56 @@
use crate::errors::ShellError;
use crate::object::dir_entry_dict;
use crate::prelude::*;
use std::path::{Path, PathBuf};
//pub fn ls(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
// let args = args.evaluate_once(registry)?;
// let path = PathBuf::from(args.shell_manager.path());
// let mut full_path = PathBuf::from(path);
// match &args.nth(0) {
// Some(Tagged {
// item: Value::Primitive(Primitive::String(s)),
// ..
// }) => full_path.push(Path::new(&s)),
// _ => {}
// }
// let entries = std::fs::read_dir(&full_path);
// let entries = match entries {
// Err(e) => {
// if let Some(s) = args.nth(0) {
// return Err(ShellError::labeled_error(
// e.to_string(),
// e.to_string(),
// s.span(),
// ));
// } else {
// return Err(ShellError::labeled_error(
// e.to_string(),
// e.to_string(),
// args.name_span(),
// ));
// }
// }
// Ok(o) => o,
// };
// let mut shell_entries = VecDeque::new();
// for entry in entries {
// let entry = entry?;
// let filepath = entry.path();
// let filename = filepath.strip_prefix(&full_path).unwrap();
// let value = dir_entry_dict(
// filename,
// &entry.metadata()?,
// Tag::unknown_origin(args.call_info.name_span),
// )?;
// shell_entries.push_back(ReturnSuccess::value(value))
// }
// Ok(shell_entries.to_output_stream())
pub fn ls(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let shell_manager = args.shell_manager.clone();
let args = args.evaluate_once(registry)?;
let path = PathBuf::from(args.shell_manager.path());
let mut full_path = PathBuf::from(path);
match &args.nth(0) {
Some(Tagged {
item: Value::Primitive(Primitive::String(s)),
..
}) => full_path.push(Path::new(&s)),
_ => {}
}
let entries = std::fs::read_dir(&full_path);
let entries = match entries {
Err(e) => {
if let Some(s) = args.nth(0) {
return Err(ShellError::labeled_error(
e.to_string(),
e.to_string(),
s.span(),
));
} else {
return Err(ShellError::labeled_error(
e.to_string(),
e.to_string(),
args.name_span(),
));
}
}
Ok(o) => o,
};
let mut shell_entries = VecDeque::new();
for entry in entries {
let entry = entry?;
let filepath = entry.path();
let filename = filepath.strip_prefix(&full_path).unwrap();
let value = dir_entry_dict(
filename,
&entry.metadata()?,
Tag::unknown_origin(args.call_info.name_span),
)?;
shell_entries.push_back(ReturnSuccess::value(value))
}
Ok(shell_entries.to_output_stream())
// pub fn ls(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
// let args = args.evaluate_once(registry)?;
// args.shell_manager.ls(args, args.input)
shell_manager.ls(args)
}

View File

@@ -6,13 +6,13 @@ use crate::commands::StaticCommand;
use crate::errors::ShellError;
use crate::object::Value;
use crate::prelude::*;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
pub struct Save;
#[derive(Deserialize)]
pub struct SaveArgs {
path: Tagged<PathBuf>,
path: Option<Tagged<PathBuf>>,
raw: bool,
}
@@ -23,7 +23,7 @@ impl StaticCommand for Save {
fn signature(&self) -> Signature {
Signature::build("save")
.required("path", SyntaxType::Path)
.optional("path", SyntaxType::Path)
.switch("raw")
}
@@ -44,71 +44,172 @@ pub fn save(
context: RunnableContext,
) -> Result<OutputStream, ShellError> {
let mut full_path = context.cwd();
full_path.push(path.item());
let stream = async_stream_block! {
let input: Vec<Tagged<Value>> = context.input.values.collect().await;
let contents = match full_path.extension() {
Some(x) if x == "csv" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to csv requires a single object (or use --raw)",
));
}
to_csv_to_string(&value_to_csv_value(&input[0])).unwrap()
}
Some(x) if x == "toml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to toml requires a single object (or use --raw)",
));
}
toml::to_string(&value_to_toml_value(&input[0])).unwrap()
}
Some(x) if x == "json" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to json requires a single object (or use --raw)",
));
}
serde_json::to_string(&value_to_json_value(&input[0])).unwrap()
}
Some(x) if x == "yml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to yml requires a single object (or use --raw)",
));
}
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
}
Some(x) if x == "yaml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to yaml requires a single object (or use --raw)",
));
}
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
}
_ => {
let mut save_data = String::new();
if input.len() > 0 {
let mut first = true;
for i in input.iter() {
if !first {
save_data.push_str("\n");
} else {
first = false;
if path.is_none() {
let source_map = context.source_map.clone();
let stream = async_stream_block! {
let input: Vec<Tagged<Value>> = context.input.values.collect().await;
// If there is no filename, check the metadata for the origin filename
if input.len() > 0 {
let origin = input[0].origin();
match origin.map(|x| source_map.get(&x)).flatten() {
Some(path) => match path {
SpanSource::File(file) => {
full_path.push(Path::new(file));
}
save_data.push_str(&i.as_string().unwrap());
_ => {
// yield Err(ShellError::labeled_error(
// "Save requires a filepath",
// "needs path",
// context.name,
// ));
}
},
None => {
// yield Err(ShellError::labeled_error(
// "Save requires a filepath",
// "needs path",
// context.name,
// ));
}
}
save_data
} else {
// yield Err(ShellError::labeled_error(
// "Save requires a filepath",
// "needs path",
// context.name,
// ));
}
let contents = match full_path.extension() {
Some(x) if x == "csv" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to csv requires a single object (or use --raw)",
));
}
to_csv_to_string(&value_to_csv_value(&input[0])).unwrap()
}
Some(x) if x == "toml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to toml requires a single object (or use --raw)",
));
}
toml::to_string(&value_to_toml_value(&input[0])).unwrap()
}
Some(x) if x == "json" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to json requires a single object (or use --raw)",
));
}
serde_json::to_string(&value_to_json_value(&input[0])).unwrap()
}
Some(x) if x == "yml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to yml requires a single object (or use --raw)",
));
}
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
}
Some(x) if x == "yaml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to yaml requires a single object (or use --raw)",
));
}
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
}
_ => {
let mut save_data = String::new();
if input.len() > 0 {
let mut first = true;
for i in input.iter() {
if !first {
save_data.push_str("\n");
} else {
first = false;
}
save_data.push_str(&i.as_string().unwrap());
}
}
save_data
}
};
let _ = std::fs::write(full_path, contents);
};
let _ = std::fs::write(full_path, contents);
};
Ok(OutputStream::new(stream))
} else {
full_path.push(path.unwrap().item());
Ok(OutputStream::new(stream))
let stream = async_stream_block! {
let input: Vec<Tagged<Value>> = context.input.values.collect().await;
let contents = match full_path.extension() {
Some(x) if x == "csv" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to csv requires a single object (or use --raw)",
));
}
to_csv_to_string(&value_to_csv_value(&input[0])).unwrap()
}
Some(x) if x == "toml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to toml requires a single object (or use --raw)",
));
}
toml::to_string(&value_to_toml_value(&input[0])).unwrap()
}
Some(x) if x == "json" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to json requires a single object (or use --raw)",
));
}
serde_json::to_string(&value_to_json_value(&input[0])).unwrap()
}
Some(x) if x == "yml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to yml requires a single object (or use --raw)",
));
}
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
}
Some(x) if x == "yaml" && !save_raw => {
if input.len() != 1 {
yield Err(ShellError::string(
"saving to yaml requires a single object (or use --raw)",
));
}
serde_yaml::to_string(&value_to_yaml_value(&input[0])).unwrap()
}
_ => {
let mut save_data = String::new();
if input.len() > 0 {
let mut first = true;
for i in input.iter() {
if !first {
save_data.push_str("\n");
} else {
first = false;
}
save_data.push_str(&i.as_string().unwrap());
}
}
save_data
}
};
let _ = std::fs::write(full_path, contents);
};
Ok(OutputStream::new(stream))
}
}

View File

@@ -31,7 +31,6 @@ pub fn table(_args: TableArgs, context: RunnableContext) -> Result<OutputStream,
if input.len() > 0 {
let mut host = context.host.lock().unwrap();
let view = TableView::from_list(&input);
println!("{:#?}", view);
if let Some(view) = view {
handle_unexpected(&mut *host, |host| crate::format::print_view(&view, host));
}