Skip to main content

tui/app/
ssh_keys.rs

1//! The SSH keys tab: fetching the list and settling the add and remove replies.
2
3use crate::tabs::ssh_keys::SshKeysTab;
4use cli_core::render::{WireValue, parse_wire};
5use scripting::nomiscript::Value;
6
7use crate::app::App;
8use crate::modal::{ConfirmAction, Modal};
9use crate::route::RouteCtx;
10use crate::view::ViewId;
11use crate::window::WindowId;
12
13impl App {
14    pub fn fetch_ssh_keys(&mut self, window: WindowId) {
15        let Some(seq) = self
16            .win_mut::<SshKeysTab>(window)
17            .map(|tab| tab.seq.advance())
18        else {
19            return;
20        };
21        let ctx = RouteCtx::SshKeys { seq };
22        if let Some(id) = self.dispatch_eval(window, ctx, "(list-ssh-keys)".to_string())
23            && let Some(keys) = self.win_mut::<SshKeysTab>(window)
24        {
25            keys.set_loading(id);
26        }
27    }
28
29    /// Asks before removing the key `fingerprint`.
30    pub fn confirm_remove_ssh_key(&mut self, fingerprint: String) {
31        if self.dialog_free("remove of the SSH key") {
32            self.open_dialog_for(
33                ViewId::SshKeys,
34                Modal::confirm(
35                    format!("Remove SSH key {fingerprint}?"),
36                    ConfirmAction::RemoveSshKey(fingerprint),
37                ),
38            );
39        }
40    }
41
42    pub(super) fn deliver_ssh_keys(&mut self, window: WindowId, seq: u64, wire: &str) {
43        if let Some(keys) = self.win_mut::<SshKeysTab>(window)
44            && keys.seq.awaits(seq)
45        {
46            keys.on_reply(wire);
47        }
48    }
49
50    pub(super) fn deliver_ssh_key_added(&mut self, window: WindowId, wire: &str) {
51        match parse_wire(wire) {
52            Ok(WireValue::Value(Value::String(id))) => {
53                self.status = "key added".to_string();
54                if let Some(keys) = self.win_mut::<SshKeysTab>(window) {
55                    keys.follow = Some(id);
56                }
57            }
58            Ok(_) => self.status = "key added".to_string(),
59            Err(e) => self.status = format!("add failed: {e}"),
60        }
61    }
62
63    pub(super) fn deliver_ssh_key_removed(&mut self, fingerprint: &str, wire: &str) {
64        self.status = match parse_wire(wire) {
65            Ok(_) => format!("removed {fingerprint}"),
66            Err(e) => format!("remove failed: {e}"),
67        };
68    }
69}