diff --git a/src/cli.rs b/src/cli.rs index 78567d6e8e..e8aa1935cc 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 da09db4b59..3b620f09d5 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 0000000000..1b66294a08 --- /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 93a0272973..1f7e67d24f 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 -}