From 1fae5201c7980b36b637998bc926adc78ce00749 Mon Sep 17 00:00:00 2001 From: Jonathan Grahl Date: Fri, 28 Jun 2024 23:27:26 +0200 Subject: [PATCH] feat: copy from other env when creating (#39) * feat: copy from other env when creating * fix: use arg over clap Co-authored-by: Sascha --------- Co-authored-by: Sascha --- src/api/mod.rs | 4 ++++ src/api/types.rs | 13 ++++++++++++- src/commands/environments.rs | 5 ++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 87e391a..fb31b4f 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -137,10 +137,14 @@ impl APIClient { token: &str, name: &str, org_name: &str, + copy_from: Option<&str>, ) -> anyhow::Result { let url = format!("{}/orgs/{}/envs", self.base_url, org_name); let mut body = HashMap::new(); body.insert("name", name); + if let Some(copy_from) = copy_from { + body.insert("copy_from", copy_from); + } let response = self.post(&url, token, &body)?; match response.status() { StatusCode::CREATED => Ok(serde_json::from_str(&response.text()?) diff --git a/src/api/types.rs b/src/api/types.rs index b415f69..82fc74e 100644 --- a/src/api/types.rs +++ b/src/api/types.rs @@ -17,7 +17,9 @@ pub struct ListOrganizationResponse { #[derive(Serialize, Deserialize, Debug, Tabled)] pub struct CreateEnvironmentResponse { - pub name: String + pub name: String, + #[serde(default, skip_serializing_if = "is_default")] + pub copy_from: DisplayOption } #[derive(Serialize, Deserialize, Debug)] @@ -76,3 +78,12 @@ impl Display for DisplayOption { Ok(()) } } + +impl Display for DisplayOption { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + match &self.0 { + Some(value) => write!(f, "{}", value), + None => Ok(()), + } + } +} diff --git a/src/commands/environments.rs b/src/commands/environments.rs index b84cf1b..f1dea8c 100644 --- a/src/commands/environments.rs +++ b/src/commands/environments.rs @@ -46,6 +46,9 @@ pub enum Commands { pub struct Create { #[arg(help = "Name of the environment to create")] name: String, + + #[arg(long, help = "Copy from an existing environment", num_args(0..=1), require_equals(true), value_name = "ENV_NAME",)] + copy_from: Option, } impl Create { @@ -58,7 +61,7 @@ impl Create { let response = base .api_client() - .create_environment(token, &self.name, &org_name)?; + .create_environment(token, &self.name, &org_name, self.copy_from.as_deref())?; let table = Table::new([response]).to_string(); println!("{}", table);