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 a5d2a5c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions stac-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Added

- `stac validate` can take from stdin ([#236](https://github.com/stac-utils/stac-rs/pull/236))

## [0.0.6] - 2023-10-18

### Added
Expand Down
3 changes: 3 additions & 0 deletions stac-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ 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"
Expand Down
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
29 changes: 29 additions & 0 deletions stac-cli/tests/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 a5d2a5c

Please sign in to comment.