forked from extern/nushell
Add save sink
This commit is contained in:
parent
2f590d051a
commit
62b648ceb8
@ -75,6 +75,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
|||||||
context.add_sinks(vec![
|
context.add_sinks(vec![
|
||||||
sink("autoview", autoview::autoview),
|
sink("autoview", autoview::autoview),
|
||||||
sink("clip", clip::clip),
|
sink("clip", clip::clip),
|
||||||
|
sink("save", save::save),
|
||||||
sink("tree", tree::tree),
|
sink("tree", tree::tree),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ crate mod open;
|
|||||||
crate mod pick;
|
crate mod pick;
|
||||||
crate mod ps;
|
crate mod ps;
|
||||||
crate mod reject;
|
crate mod reject;
|
||||||
|
crate mod save;
|
||||||
crate mod size;
|
crate mod size;
|
||||||
crate mod skip;
|
crate mod skip;
|
||||||
crate mod sort_by;
|
crate mod sort_by;
|
||||||
|
76
src/commands/save.rs
Normal file
76
src/commands/save.rs
Normal 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(())
|
||||||
|
}
|
@ -8,10 +8,3 @@ pub fn to_array(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||||||
.flatten_stream()
|
.flatten_stream()
|
||||||
.boxed())
|
.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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user