Skip to content

Commit

Permalink
feat(cli): read from input stdin in validate cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
aciba90 committed Sep 27, 2024
1 parent 2e82f4d commit 1412857
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
11 changes: 9 additions & 2 deletions ccv-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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) {
Expand Down
39 changes: 38 additions & 1 deletion ccv-cli/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
Expand Down Expand Up @@ -61,3 +61,40 @@ network:

Ok(())
}

#[test]
fn valid_network_config_stdin() -> Result<(), Box<dyn std::error::Error>> {
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(())
}

0 comments on commit 1412857

Please sign in to comment.