adding aws credentials

This commit is contained in:
jfmonty2 committed 2026-11-19 20:55:32 -05:00
1 parent 31edd55b0a
commit 39b3f74068
5 files changed
+335 -53

No files matched your search

+189 -39
View File
@@ -1,13 +1,14 @@
use gpui_kit::base::input::{InputEvent, InputState};
use gpui_kit::{
Context, FontWeight, InteractiveElement, IntoElement, ParentElement, Render, SharedString,
Styled, TestSupportExt, Window, div, rems,
AppContext, Context, Div, Entity, FontWeight, InteractiveElement, IntoElement, ParentElement,
Render, SharedString, Styled, Subscription, TestSupportExt, Window, div, rems,
};
use gpui_omarchy::{
ActiveTheme, ButtonVariant, ChoiceItem, IconName, empty_state, focus_scope, icon_button,
tab_list,
ActiveTheme, ButtonVariant, ChoiceItem, IconName, Status, badge, button, empty_state,
focus_scope, icon_button, input, switch, tab_list,
};
use crate::components::error_alert;
use crate::components::{PassphraseInput, PassphraseInputEvent, error_alert};
use crate::credentials::AwsCredential;
use crate::session::AppSession;
use super::Screen;
@@ -16,32 +17,206 @@ use super::Screen;
pub struct CredentialsScreen {
tab: usize,
aws: Vec<(String, AwsCredential)>,
aws_form: Option<AwsForm>,
error: Option<SharedString>,
}
struct AwsForm {
name: Entity<InputState>,
access_key_id: Entity<InputState>,
secret_access_key: Entity<PassphraseInput>,
is_default: bool,
_subscriptions: Vec<Subscription>,
}
impl CredentialsScreen {
pub fn new(cx: &mut Context<Self>) -> Self {
let mut this = Self { tab: 0, aws: Vec::new(), aws_form: None, error: None };
this.load_aws(cx);
this
}
fn load_aws(&mut self, cx: &mut Context<Self>) {
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
unreachable!("credentials screen is only shown while unlocked");
};
let (aws, error) = match db.list_aws_credentials() {
match db.list_aws_credentials() {
Ok(creds) => {
let mut creds: Vec<_> = creds.into_iter().collect();
creds.sort_by(|a, b| a.0.cmp(&b.0));
(creds, None)
self.aws = creds.into_iter().collect();
self.aws.sort_by(|a, b| a.0.cmp(&b.0));
self.error = None;
},
Err(e) => (Vec::new(), Some(format!("Failed to load credentials: {e}").into())),
Err(e) => self.error = Some(format!("Failed to load credentials: {e}").into()),
}
}
fn add_aws(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let name = cx.new(|cx| InputState::new(window, cx));
let access_key_id = cx.new(|cx| InputState::new(window, cx));
let show = cx.new(|_| false);
let secret_access_key = cx.new(|cx| PassphraseInput::new("aws-secret", show, window, cx));
name.update(cx, |name, cx| name.focus(window, cx));
let mut _subscriptions = Vec::new();
for input in [&name, &access_key_id] {
_subscriptions.push(cx.subscribe(input, |this, _, event: &InputEvent, cx| {
if let InputEvent::PressEnter { .. } = event {
this.save_aws(cx);
}
}));
}
_subscriptions.push(cx.subscribe(&secret_access_key, |this, _, event, cx| {
if let PassphraseInputEvent::Submit = event {
this.save_aws(cx);
}
}));
self.aws_form = Some(AwsForm {
name,
access_key_id,
secret_access_key,
is_default: false,
_subscriptions,
});
self.error = None;
cx.notify();
}
fn save_aws(&mut self, cx: &mut Context<Self>) {
let Some(form) = &self.aws_form else { return };
let name = form.name.read(cx).value().trim().to_string();
let cred = AwsCredential {
access_key_id: form.access_key_id.read(cx).value().trim().to_string(),
secret_access_key: form.secret_access_key.read(cx).value(cx).trim().to_string(),
is_default: form.is_default,
};
Self { tab: 0, aws, error }
if name.is_empty() || cred.access_key_id.is_empty() || cred.secret_access_key.is_empty() {
self.error = Some("All fields are required.".into());
cx.notify();
return;
}
// saving is an upsert, so without this a new credential would silently replace the old one
if self.aws.iter().any(|(existing, _)| *existing == name) {
self.error = Some(format!("An AWS credential named \"{name}\" already exists.").into());
cx.notify();
return;
}
let AppSession::Unlocked(db) = cx.global::<AppSession>() else {
unreachable!("credentials screen is only shown while unlocked");
};
match db.save_aws_credential(&name, &cred) {
Ok(()) => {
self.aws_form = None;
self.load_aws(cx);
},
Err(e) => self.error = Some(format!("Failed to save credential: {e}").into()),
}
cx.notify();
}
fn render_aws_tab(&self, window: &mut Window, cx: &mut Context<Self>) -> Div {
let t = cx.omarchy().clone();
let list = if self.aws.is_empty() {
empty_state("AWS access keys", "You have no saved AWS credentials.", cx)
} else {
div()
.flex()
.flex_col()
.gap(rems(0.5))
.children(self.aws.iter().map(|(name, cred)| {
div()
.id(SharedString::from(name))
.test_support()
.flex()
.items_center()
.gap(rems(0.75))
.p(rems(0.625))
.border_1()
.border_color(if cred.is_default { t.accent } else { t.border })
.child(div().font_weight(FontWeight::BOLD).child(name.clone()))
.children(cred.is_default.then(|| badge("Default", Status::Success, cx)))
.child(
div()
.ml_auto()
.font_family(t.mono_font.clone())
.text_color(t.secondary)
.child(cred.access_key_id.clone()),
)
}))
};
let Some(form) = &self.aws_form else {
return div().flex().flex_col().gap(rems(1.)).child(list).child(
button("aws-add", "Add", ButtonVariant::Primary, cx)
.self_center()
.on_click(cx.listener(|this, _, window, cx| this.add_aws(window, cx))),
);
};
let field = |label: &'static str, input: gpui_kit::AnyElement| {
div()
.flex()
.flex_col()
.gap(rems(0.25))
.child(div().text_size(rems(0.875)).child(label))
.child(input)
};
let this = cx.entity();
div().flex().flex_col().gap(rems(1.)).child(list).child(
div()
.flex()
.flex_col()
.gap(rems(1.))
.p(rems(1.))
.border_1()
.border_color(t.border)
.child(field("Name", input("aws-name", &form.name, window, cx).into_any_element()))
.child(field(
"Key ID",
input("aws-key-id", &form.access_key_id, window, cx)
.font_family(t.mono_font.clone())
.into_any_element(),
))
.child(field("Secret key", form.secret_access_key.clone().into_any_element()))
.child(
div()
.flex()
.items_center()
.gap(rems(0.5))
.child(
switch("aws-default", "Default AWS access key", form.is_default, cx)
.on_change(move |value, _, _, cx| this.update(cx, |this, cx| {
if let Some(form) = &mut this.aws_form {
form.is_default = value;
}
cx.notify();
})),
)
.child(
button("aws-cancel", "Cancel", ButtonVariant::Outline, cx)
.ml_auto()
.on_click(cx.listener(|this, _, _, cx| {
this.aws_form = None;
this.error = None;
cx.notify();
})),
)
.child(
button("aws-save", "Save", ButtonVariant::Primary, cx)
.on_click(cx.listener(|this, _, _, cx| this.save_aws(cx))),
),
),
)
}
}
impl Render for CredentialsScreen {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let t = cx.omarchy().clone();
let this = cx.entity();
let tabs = tab_list(
"credential-tabs",
@@ -61,32 +236,7 @@ impl Render for CredentialsScreen {
);
let panel = match self.tab {
0 if self.aws.is_empty() => empty_state(
"AWS access keys",
"You have no saved AWS credentials.",
cx,
),
0 => div()
.flex()
.flex_col()
.gap(rems(0.5))
.children(self.aws.iter().map(|(name, cred)| {
div()
.id(SharedString::from(name))
.test_support()
.flex()
.justify_between()
.p(rems(0.625))
.border_1()
.border_color(t.border)
.child(div().font_weight(FontWeight::BOLD).child(name.clone()))
.child(
div()
.font_family(t.mono_font.clone())
.text_color(t.secondary)
.child(cred.access_key_id.clone()),
)
})),
0 => self.render_aws_tab(window, cx),
1 => empty_state("SSH keys", "Not implemented yet.", cx),
_ => empty_state("Docker credentials", "Not implemented yet.", cx),
};