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 stdin for validate #236

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
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
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();
}