aws credential deletion
This commit is contained in:
1 parent
21efa29fdd
commit
ce8727ee9a
4 files changed
+114
-3
No files matched your search
+1
-1
@@ -5,7 +5,7 @@ use gpui_kit::{AssetSource, Result, SharedString};
|
||||
|
||||
|
||||
// gpui-kit only embeds the icons its own components use, so anything else has to be listed here
|
||||
icon_assets!(CreddyIcons, [Key, LogOut, Pencil, ShieldCheck]);
|
||||
icon_assets!(CreddyIcons, [Key, LogOut, Pencil, ShieldCheck, Trash]);
|
||||
|
||||
/// Our extra icons, falling back to gpui-kit's defaults.
|
||||
pub struct CreddyAssets;
|
||||
|
||||
+25
-1
@@ -1,4 +1,5 @@
|
||||
use duckdb::{Error, params};
|
||||
use uuid::Uuid;
|
||||
use crate::credentials::AwsCredential;
|
||||
use super::Db;
|
||||
|
||||
@@ -57,6 +58,12 @@ impl Db {
|
||||
tx.commit()
|
||||
}
|
||||
|
||||
pub fn delete_aws_credential(&self, id: Uuid) -> Result<(), Error> {
|
||||
let conn = self.connection.lock().unwrap();
|
||||
conn.execute("DELETE FROM aws_credentials WHERE id = ?", params![id])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn list_aws_credentials(&self) -> Result<Vec<AwsCredential>, Error> {
|
||||
let conn = self.connection.lock().unwrap();
|
||||
let mut stmt = conn.prepare(
|
||||
@@ -84,7 +91,6 @@ impl Db {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use testresult::TestResult;
|
||||
use uuid::Uuid;
|
||||
use crate::test_data::TestEnv;
|
||||
use super::*;
|
||||
|
||||
@@ -180,6 +186,24 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_delete() -> TestResult {
|
||||
let env = TestEnv::new()?;
|
||||
let db = env.db()?;
|
||||
|
||||
let cred1 = test_cred("test", "KEY1");
|
||||
db.save_aws_credential(&cred1)?;
|
||||
let cred2 = test_cred("test2", "KEY2");
|
||||
db.save_aws_credential(&cred2)?;
|
||||
|
||||
db.delete_aws_credential(cred1.id)?;
|
||||
|
||||
let creds = db.list_aws_credentials()?;
|
||||
assert_eq!(creds.len(), 1);
|
||||
assert_eq!(creds[0].id, cred2.id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_change_default() -> TestResult {
|
||||
let env = TestEnv::new()?;
|
||||
|
||||
+32
@@ -401,4 +401,36 @@ mod tests {
|
||||
assert_eq!(creds[0].secret_access_key, "EXAMPLESECRET");
|
||||
assert!(creds[0].is_default);
|
||||
}
|
||||
|
||||
#[gpui_kit::test]
|
||||
fn test_delete_aws_credential(cx: &mut TestAppContext) {
|
||||
let env = TestEnv::new().unwrap();
|
||||
let cred = AwsCredential {
|
||||
id: Uuid::new_v4(),
|
||||
name: "work".into(),
|
||||
access_key_id: "AKIAEXAMPLE".into(),
|
||||
secret_access_key: "EXAMPLESECRET".into(),
|
||||
is_default: false,
|
||||
};
|
||||
env.db().unwrap().save_aws_credential(&cred).unwrap();
|
||||
let window = open_app(&env, cx);
|
||||
fill(window, "passphrase", TEST_PASSPHRASE, cx);
|
||||
click(window, "submit", cx);
|
||||
click(window, "credentials", cx);
|
||||
|
||||
click(window, "delete-work", cx);
|
||||
click(window, "aws-delete-cancel", cx);
|
||||
assert!(is_visible(window, "work", cx));
|
||||
|
||||
click(window, "delete-work", cx);
|
||||
click(window, "aws-delete-confirm", cx);
|
||||
assert_eq!(error_message(window, cx), None);
|
||||
assert!(!is_visible(window, "work", cx));
|
||||
|
||||
let creds = cx.read(|cx| {
|
||||
let AppSession::Unlocked(db) = cx.global::<AppSession>() else { panic!("locked") };
|
||||
db.list_aws_credentials().unwrap()
|
||||
});
|
||||
assert!(creds.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ pub struct CredentialsScreen {
|
||||
tab: usize,
|
||||
aws: Vec<AwsCredential>,
|
||||
aws_form: Option<AwsForm>,
|
||||
/// The id of the credential awaiting delete confirmation.
|
||||
aws_deleting: Option<Uuid>,
|
||||
error: Option<SharedString>,
|
||||
}
|
||||
|
||||
@@ -34,7 +36,7 @@ struct AwsForm {
|
||||
|
||||
impl CredentialsScreen {
|
||||
pub fn new(cx: &mut Context<Self>) -> Self {
|
||||
let mut this = Self { tab: 0, aws: Vec::new(), aws_form: None, error: None };
|
||||
let mut this = Self { tab: 0, aws: Vec::new(), aws_form: None, aws_deleting: None, error: None };
|
||||
this.load_aws(cx);
|
||||
this
|
||||
}
|
||||
@@ -128,6 +130,20 @@ impl CredentialsScreen {
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn delete_aws(&mut self, id: Uuid, cx: &mut Context<Self>) {
|
||||
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
|
||||
unreachable!("credentials screen is only shown while unlocked");
|
||||
};
|
||||
match db.delete_aws_credential(id) {
|
||||
Ok(()) => {
|
||||
self.aws_deleting = None;
|
||||
self.load_aws(cx);
|
||||
},
|
||||
Err(e) => self.error = Some(format!("Failed to delete credential: {e}").into()),
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn render_aws_tab(&self, window: &mut Window, cx: &mut Context<Self>) -> Div {
|
||||
let t = cx.omarchy().clone();
|
||||
|
||||
@@ -144,6 +160,32 @@ impl CredentialsScreen {
|
||||
}
|
||||
|
||||
let id = cred.id;
|
||||
if self.aws_deleting == Some(id) {
|
||||
panel = panel.child(
|
||||
div()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap(rems(0.5))
|
||||
.p(rems(0.625))
|
||||
.border_1()
|
||||
.border_color(t.danger)
|
||||
.child(format!("Delete AWS credential \"{}\"?", cred.name))
|
||||
.child(
|
||||
button("aws-delete-cancel", "Cancel", ButtonVariant::Outline, cx)
|
||||
.ml_auto()
|
||||
.on_click(cx.listener(|this, _, _, cx| {
|
||||
this.aws_deleting = None;
|
||||
cx.notify();
|
||||
})),
|
||||
)
|
||||
.child(
|
||||
button("aws-delete-confirm", "Delete", ButtonVariant::Danger, cx)
|
||||
.on_click(cx.listener(move |this, _, _, cx| this.delete_aws(id, cx))),
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
panel = panel.child(
|
||||
div()
|
||||
.id(SharedString::from(&cred.name))
|
||||
@@ -174,6 +216,19 @@ impl CredentialsScreen {
|
||||
.on_click(cx.listener(move |this, _, window, cx| {
|
||||
this.open_aws_form(Some(id), window, cx)
|
||||
})),
|
||||
)
|
||||
.child(
|
||||
icon_button(
|
||||
SharedString::from(format!("delete-{}", cred.name)),
|
||||
IconName::Trash,
|
||||
"Delete",
|
||||
ButtonVariant::Danger,
|
||||
cx,
|
||||
)
|
||||
.on_click(cx.listener(move |this, _, _, cx| {
|
||||
this.aws_deleting = Some(id);
|
||||
cx.notify();
|
||||
})),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user