Skip to content

Commit

Permalink
feat: copy from other env when creating
Browse files Browse the repository at this point in the history
  • Loading branch information
bittermandel committed Jun 23, 2024
1 parent 17790f0 commit d7eafb6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ impl APIClient {
token: &str,
name: &str,
org_name: &str,
copy_from: Option<&str>,
) -> anyhow::Result<CreateEnvironmentResponse> {
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()?)
Expand Down
13 changes: 12 additions & 1 deletion src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -76,3 +78,12 @@ impl Display for DisplayOption<DisplayHashMap> {
Ok(())
}
}

impl<T: Display> Display for DisplayOption<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match &self.0 {
Some(value) => write!(f, "{}", value),
None => Ok(()),
}
}
}
5 changes: 4 additions & 1 deletion src/commands/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub enum Commands {
pub struct Create {
#[arg(help = "Name of the environment to create")]
name: String,

#[clap(long, help = "Copy from an existing environment", num_args(0..=1), require_equals(true), value_name = "ENV_NAME",)]
copy_from: Option<String>,
}

impl Create {
Expand All @@ -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);
Expand Down

0 comments on commit d7eafb6

Please sign in to comment.