From a54276e972c8610068fe1c41d5ba9379761a2a63 Mon Sep 17 00:00:00 2001 From: Vaibhav Rabber Date: Thu, 26 Dec 2024 22:51:51 +0530 Subject: [PATCH] feat: `s2 ls` command to list basins or streams (#102) Resolves: #101 Signed-off-by: Vaibhav Rabber --- src/main.rs | 179 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 117 insertions(+), 62 deletions(-) diff --git a/src/main.rs b/src/main.rs index 593a99d..a06b688 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,6 +75,28 @@ enum Commands { action: ConfigActions, }, + /// List basins or streams in a basin. + /// + /// List basins if basin name is not provided otherwise lists streams in + /// the basin. + Ls { + /// Name of the basin to manage or S2 URI with basin and prefix. + #[arg(value_name = "BASIN|S2_URI")] + uri: Option, + + /// Filter to names that begin with this prefix. + #[arg(short = 'p', long)] + prefix: Option, + + /// Filter to names that lexicographically start after this name. + #[arg(short = 's', long)] + start_after: Option, + + /// Number of results, upto a maximum of 1000. + #[arg(short = 'n', long)] + limit: Option, + }, + /// List basins. ListBasins { /// Filter to basin names that begin with this prefix. @@ -122,7 +144,6 @@ enum Commands { }, /// List streams. - #[command(alias = "ls")] ListStreams { /// Name of the basin to manage or S2 URI with basin and prefix. #[arg(value_name = "BASIN|S2_URI")] @@ -152,7 +173,6 @@ enum Commands { }, /// Delete a stream. - #[command(alias = "rm")] DeleteStream { #[arg(value_name = "S2_URI")] uri: S2BasinAndStreamUri, @@ -426,6 +446,84 @@ async fn run() -> Result<(), S2CliError> { .with(tracing_subscriber::EnvFilter::from_default_env()) .init(); + async fn list_basins( + client_config: ClientConfig, + prefix: Option, + start_after: Option, + limit: Option, + ) -> Result<(), S2CliError> { + let account_service = AccountService::new(Client::new(client_config)); + let response = account_service + .list_basins( + prefix.unwrap_or_default(), + start_after.unwrap_or_default(), + limit.unwrap_or_default(), + ) + .await?; + for basin_info in response.basins { + let BasinInfo { name, state, .. } = basin_info; + + let state = match state { + s2::types::BasinState::Active => state.to_string().green(), + s2::types::BasinState::Deleting => state.to_string().red(), + _ => state.to_string().yellow(), + }; + println!("{} {}", name, state); + } + Ok(()) + } + + async fn list_streams( + client_config: ClientConfig, + uri: S2BasinAndMaybeStreamUri, + prefix: Option, + start_after: Option, + limit: Option, + ) -> Result<(), S2CliError> { + let S2BasinAndMaybeStreamUri { + basin, + stream: maybe_prefix, + } = uri; + let prefix = match (maybe_prefix, prefix) { + (Some(_), Some(_)) => { + return Err(S2CliError::InvalidArgs(miette::miette!( + help = "Make sure to provide the prefix once either using '--prefix' opt or in URI like 's2://basin-name/prefix'", + "Multiple prefixes provided" + ))); + } + (Some(s), None) | (None, Some(s)) => Some(s), + (None, None) => None, + }; + let basin_client = BasinClient::new(client_config, basin); + let streams = BasinService::new(basin_client) + .list_streams( + prefix.unwrap_or_default(), + start_after.unwrap_or_default(), + limit.unwrap_or_default(), + ) + .await?; + for StreamInfo { + name, + created_at, + deleted_at, + } in streams + { + let date_time = |time: u32| { + humantime::format_rfc3339_seconds(UNIX_EPOCH + Duration::from_secs(time as u64)) + }; + + println!( + "{} {} {}", + name, + date_time(created_at).to_string().green(), + deleted_at + .map(|d| date_time(d).to_string().red()) + .unwrap_or_default() + ); + } + Ok(()) + } + match commands.command { Commands::Config { action } => match action { ConfigActions::Set { auth_token } => { @@ -438,34 +536,31 @@ async fn run() -> Result<(), S2CliError> { } }, - Commands::ListBasins { + Commands::Ls { + uri, prefix, start_after, limit, } => { let cfg = config::load_config(&config_path)?; let client_config = client_config(cfg.auth_token)?; - let account_service = AccountService::new(Client::new(client_config)); - let response = account_service - .list_basins( - prefix.unwrap_or_default(), - start_after.unwrap_or_default(), - limit.unwrap_or_default(), - ) - .await?; - - for basin_info in response.basins { - let BasinInfo { name, state, .. } = basin_info; - - let state = match state { - s2::types::BasinState::Active => state.to_string().green(), - s2::types::BasinState::Deleting => state.to_string().red(), - _ => state.to_string().yellow(), - }; - println!("{} {}", name, state); + if let Some(uri) = uri { + list_streams(client_config, uri, prefix, start_after, limit).await?; + } else { + list_basins(client_config, prefix, start_after, limit).await?; } } + Commands::ListBasins { + prefix, + start_after, + limit, + } => { + let cfg = config::load_config(&config_path)?; + let client_config = client_config(cfg.auth_token)?; + list_basins(client_config, prefix, start_after, limit).await?; + } + Commands::CreateBasin { basin, config } => { let cfg = config::load_config(&config_path)?; let client_config = client_config(cfg.auth_token)?; @@ -534,49 +629,9 @@ async fn run() -> Result<(), S2CliError> { start_after, limit, } => { - let S2BasinAndMaybeStreamUri { - basin, - stream: maybe_prefix, - } = uri; - let prefix = match (maybe_prefix, prefix) { - (Some(_), Some(_)) => { - return Err(S2CliError::InvalidArgs(miette::miette!( - help = "Make sure to provide the prefix once either using '--prefix' opt or in URI like 's2://basin-name/prefix'", - "Multiple prefixes provided" - ))); - } - (Some(s), None) | (None, Some(s)) => Some(s), - (None, None) => None, - }; let cfg = config::load_config(&config_path)?; let client_config = client_config(cfg.auth_token)?; - let basin_client = BasinClient::new(client_config, basin); - let streams = BasinService::new(basin_client) - .list_streams( - prefix.unwrap_or_default(), - start_after.unwrap_or_default(), - limit.unwrap_or_default(), - ) - .await?; - for StreamInfo { - name, - created_at, - deleted_at, - } in streams - { - let date_time = |time: u32| { - humantime::format_rfc3339_seconds(UNIX_EPOCH + Duration::from_secs(time as u64)) - }; - - println!( - "{} {} {}", - name, - date_time(created_at).to_string().green(), - deleted_at - .map(|d| date_time(d).to_string().red()) - .unwrap_or_default() - ); - } + list_streams(client_config, uri, prefix, start_after, limit).await?; } Commands::CreateStream { uri, config } => {