Skip to content

Commit

Permalink
feat: add sort to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Oct 9, 2023
1 parent 0e3f979 commit ab8b67c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 33 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

- Sort ([#197](https://github.com/stac-utils/stac-rs/pull/197))

### Removed

- Downloading (use [stac-asset](https://github.com/stac-utils/stac-asset) instead) ([#194](https://github.com/stac-utils/stac-rs/pull/194))
Expand Down
8 changes: 8 additions & 0 deletions stac-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ Validate a STAC item:
```shell
stac validate https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/simple-item.json
```

### Sort

Sort the fields of a STAC item into the order they're specified in the spec:

```shell
stac sort https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/simple-item.json
```
22 changes: 22 additions & 0 deletions stac-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ use stac_validate::Validate;

#[derive(Debug, Subcommand)]
pub enum Command {
/// Sorts the fields of STAC object.
Sort {
/// The href of the STAC object.
href: String,

/// If true, don't pretty-print the output
#[arg(short, long)]
compact: bool,
},

/// Validates a STAC object using json-schema validation.
Validate {
/// The href of the STAC object.
Expand All @@ -15,6 +25,7 @@ impl Command {
pub async fn execute(self) -> Result<()> {
use Command::*;
match self {
Sort { href, compact } => sort(&href, compact).await,
Validate { href } => validate(&href).await,
}
}
Expand Down Expand Up @@ -43,3 +54,14 @@ async fn validate(href: &str) -> Result<()> {
}
}
}

async fn sort(href: &str, compact: bool) -> Result<()> {
let value: stac::Value = stac_async::read_json(href).await?;
let output = if compact {
serde_json::to_string(&value).unwrap()
} else {
serde_json::to_string_pretty(&value).unwrap()
};
println!("{}", output);
Ok(())
}
22 changes: 11 additions & 11 deletions stac/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ pub const CATALOG_TYPE: &str = "Catalog";
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Catalog {
/// Set to `"Catalog"` if this Catalog only implements the `Catalog` spec.
#[serde(
deserialize_with = "deserialize_type",
serialize_with = "serialize_type"
)]
r#type: String,

/// The STAC version the `Catalog` implements.
#[serde(rename = "stac_version")]
version: String,

/// A list of extension identifiers the `Catalog` implements.
#[serde(rename = "stac_extensions")]
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -44,17 +55,6 @@ pub struct Catalog {
#[serde(flatten)]
pub additional_fields: Map<String, Value>,

/// Set to `"Catalog"` if this Catalog only implements the `Catalog` spec.
#[serde(
deserialize_with = "deserialize_type",
serialize_with = "serialize_type"
)]
r#type: String,

/// The STAC version the `Catalog` implements.
#[serde(rename = "stac_version")]
version: String,

#[serde(skip)]
href: Option<String>,
}
Expand Down
22 changes: 11 additions & 11 deletions stac/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ const DEFAULT_LICENSE: &str = "proprietary";
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Collection {
/// Must be set to `"Collection"` to be a valid `Collection`.
#[serde(
deserialize_with = "deserialize_type",
serialize_with = "serialize_type"
)]
r#type: String,

/// The STAC version the `Collection` implements.
#[serde(rename = "stac_version")]
version: String,

/// A list of extension identifiers the `Collection` implements.
#[serde(rename = "stac_extensions")]
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -76,17 +87,6 @@ pub struct Collection {
#[serde(flatten)]
pub additional_fields: Map<String, Value>,

/// Must be set to `"Collection"` to be a valid `Collection`.
#[serde(
deserialize_with = "deserialize_type",
serialize_with = "serialize_type"
)]
r#type: String,

/// The STAC version the `Collection` implements.
#[serde(rename = "stac_version")]
version: String,

#[serde(skip)]
href: Option<String>,
}
Expand Down
22 changes: 11 additions & 11 deletions stac/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ pub const ITEM_TYPE: &str = "Feature";
/// (e.g., satellite imagery, derived data, DEMs).
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Item {
/// Type of the GeoJSON Object. MUST be set to `"Feature"`.
#[serde(
deserialize_with = "deserialize_type",
serialize_with = "serialize_type"
)]
r#type: String,

/// The STAC version the `Item` implements.
#[serde(rename = "stac_version")]
version: String,

/// A list of extensions the `Item` implements.
#[serde(rename = "stac_extensions")]
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -66,17 +77,6 @@ pub struct Item {
#[serde(flatten)]
pub additional_fields: Map<String, Value>,

/// Type of the GeoJSON Object. MUST be set to `"Feature"`.
#[serde(
deserialize_with = "deserialize_type",
serialize_with = "serialize_type"
)]
r#type: String,

/// The STAC version the `Item` implements.
#[serde(rename = "stac_version")]
version: String,

#[serde(skip)]
href: Option<String>,
}
Expand Down

0 comments on commit ab8b67c

Please sign in to comment.