Skip to content

Commit

Permalink
Merge pull request #21 from molnett/delete-env
Browse files Browse the repository at this point in the history
feat: support deleting environments
  • Loading branch information
tmlye authored Mar 21, 2024
2 parents b107277 + fdd4b22 commit 479aa28
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl APIClient {
match response.status() {
StatusCode::CREATED => Ok(serde_json::from_str(&response.text()?).with_context(|| "Failed to deserialize org")?),
StatusCode::UNAUTHORIZED => Err(anyhow!("Unauthorized, please login first")),
StatusCode::CONFLICT => Err(anyhow!("Organization already exists")),
StatusCode::NOT_FOUND => Err(anyhow!("Org not found")),
StatusCode::BAD_REQUEST => Err(anyhow!("Bad request: {}", response.text()?)),
_ => Err(anyhow!("Failed to deploy service. API returned {} - {}", response.status(), response.text()?))
Expand Down Expand Up @@ -101,9 +102,25 @@ impl APIClient {
match response.status() {
StatusCode::CREATED => Ok(serde_json::from_str(&response.text()?).with_context(|| "Failed to deserialize env")?),
StatusCode::UNAUTHORIZED => Err(anyhow!("Unauthorized, please login first")),
StatusCode::CONFLICT => Err(anyhow!("Environment already exists")),
StatusCode::NOT_FOUND => Err(anyhow!("Org not found")),
StatusCode::BAD_REQUEST => Err(anyhow!("Bad request: {}", response.text()?)),
_ => Err(anyhow!("Failed to deploy service. API returned {} - {}", response.status(), response.text()?))
_ => Err(anyhow!("Failed to create environment. API returned {} - {}", response.status(), response.text()?))
}
}

pub fn delete_environment(
&self,
token: &str,
org_name: &str,
name: &str
) -> anyhow::Result<()> {
let url = format!("{}/orgs/{}/envs/{}", self.base_url, org_name, name);
let response = self.delete(&url, token)?;
match response.status() {
StatusCode::NO_CONTENT => Ok(()),
StatusCode::NOT_FOUND => Err(anyhow!("Environment does not exist")),
_ => Err(anyhow!("Failed to delete environment. API returned {} - {}", response.status(), response.text()?))
}
}

Expand Down
38 changes: 38 additions & 0 deletions src/commands/environments.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::CommandBase;
use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use dialoguer::FuzzySelect;
use tabled::Table;

#[derive(Debug, Parser)]
Expand All @@ -22,6 +23,7 @@ impl Environments {
match &self.command {
Some(Commands::Create(create)) => create.execute(base),
Some(Commands::List(list)) => list.execute(base),
Some(Commands::Delete(delete)) => delete.execute(base),
None => Ok(()),
}
}
Expand All @@ -35,6 +37,8 @@ pub enum Commands {
/// List environments
#[command()]
List(List),
/// Delete an environment
Delete(Delete),
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -80,3 +84,37 @@ impl List {
Ok(())
}
}

#[derive(Debug, Parser)]
pub struct Delete {
#[arg(help = "Name of the environment")]
name: String,
#[arg(long, help = "Skip confirmation", default_missing_value("true"), default_value("false"), num_args(0..=1), require_equals(true))]
no_confirm: Option<bool>,
}

impl Delete {
pub fn execute(&self, base: &CommandBase) -> Result<()> {
let org_name = base.get_org()?;
let token = base
.user_config()
.get_token()
.ok_or_else(|| anyhow!("No token found. Please login first."))?;

if let Some(false) = self.no_confirm {
let prompt = format!("Org: {}, Environment: {}. Are you sure you want to delete this environment and everything in it?", org_name, self.name);
FuzzySelect::with_theme(&dialoguer::theme::ColorfulTheme::default())
.with_prompt(prompt)
.items(&["no", "yes"])
.default(0)
.interact()
.unwrap();
}

base.api_client()
.delete_environment(token, &org_name, &self.name)?;

println!("Environment {} deleted", self.name);
Ok(())
}
}

0 comments on commit 479aa28

Please sign in to comment.