From f875ae5814129351e75783616205f3f2f7b8e830 Mon Sep 17 00:00:00 2001 From: Ondrej Zoder Date: Fri, 5 Jul 2024 14:22:00 +0100 Subject: [PATCH] feat(commands): create config from URL --- CHANGELOG.md | 9 ++++-- cli/src/commands/config.rs | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ba2b23..9e3f1b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# Unreleased +- Add `config parse-from-url` command for parsing configuration from a URL + # v0.28.0 - Add general fields to `create datasets` @@ -14,10 +17,10 @@ # v0.25.0 - Fixes issue when getting streams that have multiple filters on single user property -- Fixes issue where upper case file names would not be matched in `parse` +- Fixes issue where upper case file names would not be matched in `parse` - Reduce batch size when deleting comment batches - Support attachment type filters -- support getting stats for `get buckets` +- support getting stats for `get buckets` - Show usage on `get quotas` # v0.24.0 @@ -29,7 +32,7 @@ - Add `get emails` - Added support for `--auto-increase-up-to` when creating quotas. -- Support spans format for entities +- Support spans format for entities # v0.22.2 diff --git a/cli/src/commands/config.rs b/cli/src/commands/config.rs index 768e33d..02fc23a 100644 --- a/cli/src/commands/config.rs +++ b/cli/src/commands/config.rs @@ -76,6 +76,17 @@ pub enum ConfigArgs { #[structopt(name = "is-required", parse(try_from_str))] is_required: bool, }, + + #[structopt(name = "parse-from-url")] + /// Parse config from a URL + ParseFromUrl { + /// The URL to be parsed + #[structopt(long = "url", short = "u")] + url: Option, + /// The reinfer API token that will be used for this context + #[structopt(long = "token", short = "t")] + token: Option, + }, } pub fn run( @@ -206,10 +217,60 @@ pub fn run( } } } + ConfigArgs::ParseFromUrl { url, token } => { + parse_context_from_url(url, token, config.clone(), config_path)?; + } } Ok(config) } +fn parse_context_from_url( + url: &Option, + token: &Option, + config: ReinferConfig, + config_path: impl AsRef, +) -> Result<()> { + let mut url: Url = match url { + None => loop { + match Url::parse(&utils::read_from_stdin("URL", None)?) { + Ok(url) => break url, + Err(error) => { + error!("Invalid URL: {}", error); + } + } + }, + Some(url) => url.clone(), + }; + let path_segments: Vec<&str> = match url.path_segments() { + None => { + return Err(anyhow!( + "Invalid URL path, needs to contain //reinfer_/" + )) + } + Some(segments) => segments.collect(), + }; + if path_segments.len() < 3 || path_segments[2] != "reinfer_" { + return { + Err(anyhow!( + "Invalid URL path, needs to contain //reinfer_/" + )) + }; + } + let name: String = format!("{}/{}", path_segments[0], path_segments[1]); + // Remove the path to use the rest as the endpoint + url.set_path(""); + + add_or_edit_context( + &Some(name), + token, + &Some(url), + false, + &None, + config, + config_path, + ) +} + fn add_or_edit_context( name: &Option, token: &Option,