From eae08fc1a9ebb4506ee3862244ec41c8035b599d Mon Sep 17 00:00:00 2001 From: Nokome Bentley Date: Mon, 30 Aug 2021 12:25:59 +1200 Subject: [PATCH] feat(CLI): Add option for `convert` to stdout --- rust/src/documents.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/rust/src/documents.rs b/rust/src/documents.rs index e570621b5e..b2f5ee90f5 100644 --- a/rust/src/documents.rs +++ b/rust/src/documents.rs @@ -1248,9 +1248,9 @@ pub mod cli { } } + /// Convert a document to another format #[derive(Debug, StructOpt)] #[structopt( - about = "Convert a document to another format", setting = structopt::clap::AppSettings::DeriveDisplayOrder, setting = structopt::clap::AppSettings::ColoredHelp )] @@ -1259,6 +1259,8 @@ pub mod cli { pub input: PathBuf, /// The path of the output document + /// + /// Use `-` to print output to the console's standard output. pub output: PathBuf, /// The format of the input (defaults to being inferred from the file extension or content type) @@ -1283,9 +1285,21 @@ pub mod cli { to, theme, } = self; + let document = Document::open(input, from).await?; - document.write_as(output, to, theme).await?; - display::nothing() + + let out = output.display().to_string(); + if out == "-" { + let format = match to { + None => "json".to_string(), + Some(format) => format, + }; + let content = document.dump(Some(format.clone())).await?; + display::content(&format, &content) + } else { + document.write_as(output, to, theme).await?; + display::nothing() + } } } #[derive(Debug, StructOpt)]