1
//! The SSH keys tab: fetching the list and settling the add and remove replies.
2

            
3
use crate::tabs::ssh_keys::SshKeysTab;
4
use cli_core::render::{WireValue, parse_wire};
5
use scripting::nomiscript::Value;
6

            
7
use crate::app::App;
8
use crate::modal::{ConfirmAction, Modal};
9
use crate::route::RouteCtx;
10
use crate::view::ViewId;
11
use crate::window::WindowId;
12

            
13
impl App {
14
55
    pub fn fetch_ssh_keys(&mut self, window: WindowId) {
15
55
        let Some(seq) = self
16
55
            .win_mut::<SshKeysTab>(window)
17
55
            .map(|tab| tab.seq.advance())
18
        else {
19
            return;
20
        };
21
55
        let ctx = RouteCtx::SshKeys { seq };
22
55
        if let Some(id) = self.dispatch_eval(window, ctx, "(list-ssh-keys)".to_string())
23
27
            && let Some(keys) = self.win_mut::<SshKeysTab>(window)
24
        {
25
27
            keys.set_loading(id);
26
28
        }
27
55
    }
28

            
29
    /// Asks before removing the key `fingerprint`.
30
4
    pub fn confirm_remove_ssh_key(&mut self, fingerprint: String) {
31
4
        if self.dialog_free("remove of the SSH key") {
32
4
            self.open_dialog_for(
33
4
                ViewId::SshKeys,
34
4
                Modal::confirm(
35
4
                    format!("Remove SSH key {fingerprint}?"),
36
4
                    ConfirmAction::RemoveSshKey(fingerprint),
37
4
                ),
38
4
            );
39
        }
40
4
    }
41

            
42
16
    pub(super) fn deliver_ssh_keys(&mut self, window: WindowId, seq: u64, wire: &str) {
43
16
        if let Some(keys) = self.win_mut::<SshKeysTab>(window)
44
16
            && keys.seq.awaits(seq)
45
        {
46
14
            keys.on_reply(wire);
47
2
        }
48
16
    }
49

            
50
2
    pub(super) fn deliver_ssh_key_added(&mut self, window: WindowId, wire: &str) {
51
2
        match parse_wire(wire) {
52
            Ok(WireValue::Value(Value::String(id))) => {
53
2
                self.status = "key added".to_string();
54
2
                if let Some(keys) = self.win_mut::<SshKeysTab>(window) {
55
2
                    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
2
    }
62

            
63
3
    pub(super) fn deliver_ssh_key_removed(&mut self, fingerprint: &str, wire: &str) {
64
3
        self.status = match parse_wire(wire) {
65
2
            Ok(_) => format!("removed {fingerprint}"),
66
1
            Err(e) => format!("remove failed: {e}"),
67
        };
68
3
    }
69
}