Skip to content

Commit

Permalink
feat: allow stdin for stac-cli sort
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Apr 11, 2024
1 parent 0c9b3f4 commit 917bb51
Show file tree
Hide file tree
Showing 4 changed files with 26 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 sort` can take stdin ([#241](https://github.com/stac-utils/stac-rs/pull/241))

## [0.0.7] - 2024-04-11

### Added
Expand Down
6 changes: 4 additions & 2 deletions stac-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ pub enum Command {
/// Sorts the fields of STAC object.
Sort {
/// The href of the STAC object.
href: String,
///
/// If this is not provided, will read from standard input.
href: Option<String>,

/// If true, don't pretty-print the output
#[arg(short, long)]
Expand Down Expand Up @@ -199,7 +201,7 @@ impl Command {
let search = get_search.try_into()?;
crate::commands::search(&href, search, max_items, stream, !(compact | stream)).await
}
Sort { href, compact } => crate::commands::sort(&href, compact).await,
Sort { href, compact } => crate::commands::sort(href.as_deref(), compact).await,
Validate { href } => crate::commands::validate(href.as_deref()).await,
}
}
Expand Down
9 changes: 7 additions & 2 deletions stac-cli/src/commands/sort.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use crate::Result;
use stac::Value;

pub async fn sort(href: &str, compact: bool) -> Result<()> {
let value: stac::Value = stac_async::read_json(href).await?;
pub async fn sort(href: Option<&str>, compact: bool) -> Result<()> {
let value: Value = if let Some(href) = href {
stac_async::read_json(href).await?
} else {
serde_json::from_reader(std::io::stdin())?
};
let output = if compact {
serde_json::to_string(&value).unwrap()
} else {
Expand Down
11 changes: 11 additions & 0 deletions stac-cli/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ fn validate_stdin() {
.unwrap();
command.arg("validate").write_stdin(item).assert().success();
}

#[test]
fn sort_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("sort").write_stdin(item).assert().success();
}

0 comments on commit 917bb51

Please sign in to comment.