From 0c1dfff96905b4296ae5e840ddb48b6005b0057e Mon Sep 17 00:00:00 2001 From: clux Date: Thu, 21 Apr 2022 09:46:54 +0100 Subject: [PATCH] Allow reading file from stdin with kopium -f - fixes #48 Signed-off-by: clux --- src/main.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1df6be0..1f760fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,6 +123,14 @@ async fn main() -> Result<()> { Kopium::from_args().dispatch().await } +fn get_stdin_data() -> Result { + use std::io::{stdin, Read}; + let mut buf = Vec::new(); + stdin().read_to_end(&mut buf)?; + let input = String::from_utf8(buf)?; + Ok(input) +} + impl Kopium { async fn dispatch(&self) -> Result<()> { if let Some(name) = self.crd.as_deref() { @@ -133,8 +141,12 @@ impl Kopium { self.generate(crd).await } else if let Some(f) = self.file.as_deref() { // no cluster access needed in this case - let data = - std::fs::read_to_string(&f).with_context(|| format!("Failed to read {}", f.display()))?; + let data = if f.to_string_lossy() == "-" { + get_stdin_data().with_context(|| format!("Failed to read from stdin"))? + } else { + std::fs::read_to_string(&f).with_context(|| format!("Failed to read {}", f.display()))? + }; + let crd: CustomResourceDefinition = serde_yaml::from_str(&data)?; self.generate(crd).await } else {