From 62b648ceb83e63f43073ee338a0223d5e23107f7 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 8 Jun 2019 05:13:38 +1200 Subject: [PATCH] Add save sink --- src/cli.rs | 1 + src/commands.rs | 1 + src/commands/save.rs | 76 ++++++++++++++++++++++++++++++++++++++++ src/commands/to_array.rs | 7 ---- 4 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 src/commands/save.rs diff --git a/src/cli.rs b/src/cli.rs index 78567d6e8..e8aa1935c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -75,6 +75,7 @@ pub async fn cli() -> Result<(), Box> { context.add_sinks(vec![ sink("autoview", autoview::autoview), sink("clip", clip::clip), + sink("save", save::save), sink("tree", tree::tree), ]); } diff --git a/src/commands.rs b/src/commands.rs index da09db4b5..3b620f09d 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -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; diff --git a/src/commands/save.rs b/src/commands/save.rs new file mode 100644 index 000000000..1b66294a0 --- /dev/null +++ b/src/commands/save.rs @@ -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(()) +} diff --git a/src/commands/to_array.rs b/src/commands/to_array.rs index 93a027297..1f7e67d24 100644 --- a/src/commands/to_array.rs +++ b/src/commands/to_array.rs @@ -8,10 +8,3 @@ pub fn to_array(args: CommandArgs) -> Result { .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 -}