refactor(cli): Simplify namespace update-limit command code

pull/24376/head
Fraser Savage 2023-04-05 15:25:37 +01:00
parent 9fcaee9ac9
commit 44478348c7
No known key found for this signature in database
GPG Key ID: DE47C33CE8C5C446
2 changed files with 13 additions and 12 deletions

View File

@ -16,9 +16,6 @@ pub enum Error {
#[error("Client error: {0}")]
ClientError(#[from] influxdb_iox_client::error::Error),
#[error("No valid limit was provided")]
InvalidLimit,
}
pub type Result<T, E = Error> = std::result::Result<T, E>;

View File

@ -1,7 +1,7 @@
use influxdb_iox_client::connection::Connection;
use influxdb_iox_client::namespace::generated_types::LimitUpdate;
use crate::commands::namespace::{Error, Result};
use crate::commands::namespace::Result;
#[derive(Debug, clap::Parser)]
pub struct Config {
@ -15,6 +15,12 @@ pub struct Config {
#[derive(Debug, clap::Args)]
#[clap(group(
// This arg group "limit" links the members of the below struct
// named "max_tables" and "max_columns_per_table" together as
// mutually exclusive flags. As we specify all flags & commands
// using clap-derive rather than the imperative builder, v3 only
// properly supports this kind of behaviour in a macro code block.
// NOTE: It takes the variable names and not the flag long names.
clap::ArgGroup::new("limit")
.required(true)
.args(&["max_tables", "max_columns_per_table"])
@ -29,22 +35,20 @@ struct Args {
max_columns_per_table: Option<i32>,
}
impl TryFrom<Args> for LimitUpdate {
type Error = Error;
fn try_from(args: Args) -> Result<Self> {
impl From<Args> for LimitUpdate {
fn from(args: Args) -> Self {
let Args {
max_tables,
max_columns_per_table,
} = args;
if let Some(n) = max_tables {
return Ok(Self::MaxTables(n));
return Self::MaxTables(n);
}
if let Some(n) = max_columns_per_table {
return Ok(Self::MaxColumnsPerTable(n));
return Self::MaxColumnsPerTable(n);
}
Err(Error::InvalidLimit)
unreachable!();
}
}
@ -54,7 +58,7 @@ pub async fn command(connection: Connection, config: Config) -> Result<()> {
let namespace = client
.update_namespace_service_protection_limit(
&config.namespace,
LimitUpdate::try_from(config.args)?,
LimitUpdate::from(config.args),
)
.await?;
println!("{}", serde_json::to_string_pretty(&namespace)?);