Skip to content

Commit

Permalink
feat(CLI): Add option for convert to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Aug 31, 2021
1 parent b196d42 commit eae08fc
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions rust/src/documents.rs
Expand Up @@ -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
)]
Expand All @@ -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)
Expand All @@ -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)]
Expand Down

0 comments on commit eae08fc

Please sign in to comment.