Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reading file from stdin with kopium -f - #50

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ async fn main() -> Result<()> {
Kopium::from_args().dispatch().await
}

fn get_stdin_data() -> Result<String> {
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() {
Expand All @@ -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 {
Expand Down