Skip to content

Commit

Permalink
feat: allow stdin for validate
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Apr 10, 2024
1 parent 1e49823 commit a7ee3e4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
4 changes: 3 additions & 1 deletion stac-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ tokio = { version = "1.23", features = ["macros", "rt-multi-thread"] }
tokio-stream = "0.1"
url = "2"

[dev-dependencies]
assert_cmd = "2"

[[bin]]
name = "stac"
path = "src/main.rs"
doc = false
test = false
1 change: 1 addition & 0 deletions stac-cli/data
6 changes: 4 additions & 2 deletions stac-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ pub enum Command {
/// or Item, that object will be validated. If its a collections
/// endpoint from a STAC API, all collections will be validated.
/// Additional behavior TBD.
href: String,
///
/// If this is not provided, will read from standard input.
href: Option<String>,
},
}

Expand Down Expand Up @@ -135,7 +137,7 @@ impl Command {
crate::commands::search(&href, search, max_items, stream, !(compact | stream)).await
}
Sort { href, compact } => crate::commands::sort(&href, compact).await,
Validate { href } => crate::commands::validate(&href).await,
Validate { href } => crate::commands::validate(href.as_deref()).await,
}
}
}
9 changes: 7 additions & 2 deletions stac-cli/src/commands/validate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use crate::{Error, Result};
use serde_json::Value;
use stac_validate::{Validate, Validator};

pub async fn validate(href: &str) -> Result<()> {
let value: serde_json::Value = stac_async::read_json(href).await?;
pub async fn validate(href: Option<&str>) -> Result<()> {
let value: Value = if let Some(href) = href {
stac_async::read_json(href).await?
} else {
serde_json::from_reader(std::io::stdin())?
};
if let Some(map) = value.as_object() {
if map.contains_key("type") {
let value = value.clone();
Expand Down
33 changes: 33 additions & 0 deletions stac-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,36 @@ async fn main() {
}
}
}

#[cfg(test)]
mod tests {
use assert_cmd::Command;
use std::{fs::File, io::Read};

#[test]
fn help() {
let mut command = Command::cargo_bin("stac").unwrap();
command.arg("help").assert().success();
}

#[test]
fn validate() {
let mut command = Command::cargo_bin("stac").unwrap();
command
.arg("validate")
.arg("data/simple-item.json")
.assert()
.success();
}

#[test]
fn validate_stdin() {
let mut command = Command::cargo_bin("stac").unwrap();
let mut item = String::new();
File::open("data/simple-item.json")
.unwrap()
.read_to_string(&mut item)
.unwrap();
command.arg("validate").write_stdin(item).assert().success();
}
}

0 comments on commit a7ee3e4

Please sign in to comment.