Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support deleting environments #21

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}
}
Loading