Add save sink

This commit is contained in:
Jonathan Turner 2019-06-08 05:13:38 +12:00
parent 2f590d051a
commit 62b648ceb8
4 changed files with 78 additions and 7 deletions

View File

@ -75,6 +75,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
context.add_sinks(vec![
sink("autoview", autoview::autoview),
sink("clip", clip::clip),
sink("save", save::save),
sink("tree", tree::tree),
]);
}

View File

@ -15,6 +15,7 @@ crate mod open;
crate mod pick;
crate mod ps;
crate mod reject;
crate mod save;
crate mod size;
crate mod skip;
crate mod sort_by;

76
src/commands/save.rs Normal file
View File

@ -0,0 +1,76 @@
use crate::commands::command::SinkCommandArgs;
use crate::errors::ShellError;
use crate::object::{Primitive, Value};
use crate::prelude::*;
use std::path::{Path, PathBuf};
pub fn save(args: SinkCommandArgs) -> Result<(), ShellError> {
if args.positional.len() == 0 {
return Err(ShellError::string("save requires a filepath"));
}
let cwd = args.ctx.env.lock().unwrap().cwd().to_path_buf();
let mut full_path = PathBuf::from(cwd);
match &args.positional[0] {
Value::Primitive(Primitive::String(s)) => full_path.push(Path::new(s)),
_ => {}
}
let save_raw = match args.positional.get(1) {
Some(Value::Primitive(Primitive::String(s))) if s == "--raw" => true,
_ => false,
};
let contents = match full_path.extension() {
Some(x) if x == "toml" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to toml requires a single object (or use --raw)",
));
}
toml::to_string(&args.input[0]).unwrap()
}
Some(x) if x == "json" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to json requires a single object (or use --raw)",
));
}
serde_json::to_string(&args.input[0]).unwrap()
}
Some(x) if x == "yml" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to yml requires a single object (or use --raw)",
));
}
serde_yaml::to_string(&args.input[0]).unwrap()
}
Some(x) if x == "yaml" && !save_raw => {
if args.input.len() != 1 {
return Err(ShellError::string(
"saving to yaml requires a single object (or use --raw)",
));
}
serde_yaml::to_string(&args.input[0]).unwrap()
}
_ => {
let mut save_data = String::new();
if args.input.len() > 0 {
let mut first = true;
for i in args.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(())
}

View File

@ -8,10 +8,3 @@ pub fn to_array(args: CommandArgs) -> Result<OutputStream, ShellError> {
.flatten_stream()
.boxed())
}
crate async fn stream_to_array(stream: InputStream) -> InputStream {
let out = Value::List(stream.collect().await);
let mut stream = VecDeque::new();
stream.push_back(out);
stream.boxed() as InputStream
}