Skip to content

Commit

Permalink
fix(cli): href handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Oct 9, 2024
1 parent d7dd5ed commit 0e15595
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions cli/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::Read;

use crate::{options::Options, Error, Result};
use stac::{Format, Value};
use std::io::Read;
use url::Url;

/// The input to a CLI run.
#[derive(Debug, Default)]
Expand Down Expand Up @@ -59,20 +59,24 @@ impl Input {
/// Gets a serde_json value from the input.
pub(crate) async fn get_json(&self) -> Result<serde_json::Value> {
if let Some(href) = self.href.as_deref() {
let (object_store, path) =
object_store::parse_url_opts(&href.parse()?, self.options.iter())?;
let get_result = object_store.get(&path).await?;
let bytes = get_result.bytes().await?;
let format = self
.format
.or_else(|| Format::infer_from_href(href))
.unwrap_or_default();
match format {
Format::Json(..) => serde_json::from_slice(&bytes).map_err(Error::from),
_ => {
let value: Value = format.from_bytes(bytes)?;
serde_json::to_value(value).map_err(Error::from)
if let Ok(url) = Url::parse(href) {
let (object_store, path) = object_store::parse_url_opts(&url, self.options.iter())?;
let get_result = object_store.get(&path).await?;
let bytes = get_result.bytes().await?;
match format {
Format::Json(..) => serde_json::from_slice(&bytes).map_err(Error::from),
_ => {
let value: Value = format.from_bytes(bytes)?;
serde_json::to_value(value).map_err(Error::from)
}
}
} else {
let value: Value = format.from_path(href)?;
serde_json::to_value(value).map_err(Error::from)
}
} else {
let mut buf = Vec::new();
Expand Down

0 comments on commit 0e15595

Please sign in to comment.