From 141285742626e2977f5eb3ec47c360c53854e8e1 Mon Sep 17 00:00:00 2001 From: Alberto Contreras Date: Fri, 27 Sep 2024 16:14:28 +0200 Subject: [PATCH] feat(cli): read from input stdin in validate cmd --- ccv-cli/src/main.rs | 11 +++++++++-- ccv-cli/tests/cli.rs | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/ccv-cli/src/main.rs b/ccv-cli/src/main.rs index 0b92416..279e5af 100644 --- a/ccv-cli/src/main.rs +++ b/ccv-cli/src/main.rs @@ -1,7 +1,12 @@ use ccv_core::{schema::ConfigKind, validator::Validator}; use clap::builder::TypedValueParser as _; use clap::Parser; -use std::{fs, path::Path, path::PathBuf, process}; +use std::{ + fs, + io::{self, Read}, + path::{Path, PathBuf}, + process, +}; #[derive(Parser)] #[command(name = "ccv", author, version, about, long_about = None)] @@ -28,7 +33,9 @@ async fn main() -> process::ExitCode { let CCVCli::Validate(args) = CCVCli::parse(); let payload = if Path::new("-") == args.file { - todo!("read stdin"); + let mut buffer = Vec::new(); + io::stdin().read_to_end(&mut buffer).unwrap(); + String::from_utf8(buffer).unwrap() } else { let f = args.file; match fs::read_to_string(&f) { diff --git a/ccv-cli/tests/cli.rs b/ccv-cli/tests/cli.rs index 39b2a16..77e58ed 100644 --- a/ccv-cli/tests/cli.rs +++ b/ccv-cli/tests/cli.rs @@ -1,7 +1,7 @@ use assert_cmd::prelude::*; +use assert_cmd::Command; use assert_fs::prelude::*; use predicates::prelude::*; -use std::process::Command; #[test] fn file_doesnt_exist() -> Result<(), Box> { @@ -61,3 +61,40 @@ network: Ok(()) } + +#[test] +fn valid_network_config_stdin() -> Result<(), Box> { + let content = r#" +network: + version: 1 + config: + - type: bond + name: a + mac_address: aa:bb + mtu: 1 + subnets: + - type: dhcp6 + control: manual + netmask: 255.255.255.0 + gateway: 10.0.0.1 + dns_nameservers: + - 8.8.8.8 + dns_search: + - find.me + routes: + - type: route + destination: 10.20.0.0/8 + gateway: a.b.c.d + metric: 200"#; + + let mut cmd = Command::cargo_bin("ccv-cli")?; + cmd.arg("validate") + .args(["--kind", "networkconfig"]) + .args(["-"]); + cmd.write_stdin(content); + cmd.assert().success().stdout(predicate::str::contains( + r#"{"annotations":[],"errors":[],"is_valid":true}"#, + )); + + Ok(()) +}