Skip to main content

cossh/ssh_config/
model.rs

1//! SSH config domain models.
2
3use crate::inventory::{ConnectionProtocol, SshOptionMap, TreeFolder};
4use std::path::PathBuf;
5
6/// Represents a single SSH host configuration.
7#[derive(Debug, Clone)]
8pub struct SshHost {
9    /// The host name/alias from the config.
10    pub name: String,
11    /// Connection protocol for this host.
12    pub protocol: ConnectionProtocol,
13    /// Hostname (or IP address).
14    pub hostname: Option<String>,
15    /// Username.
16    pub user: Option<String>,
17    /// Port number.
18    pub port: Option<u16>,
19    /// Identity file path.
20    pub identity_files: Vec<String>,
21    /// Whether only explicit identities should be used.
22    pub identities_only: Option<bool>,
23    /// Proxy jump host.
24    pub proxy_jump: Option<String>,
25    /// Proxy command setting.
26    pub proxy_command: Option<String>,
27    /// ForwardAgent value.
28    pub forward_agent: Option<String>,
29    /// Description from `#_Desc` comment.
30    pub description: Option<String>,
31    /// Profile from `#_Profile` comment.
32    pub profile: Option<String>,
33    /// Password key name (from `#_pass` comment).
34    pub pass_key: Option<String>,
35    /// Optional RDP domain (from `#_RdpDomain` comment).
36    pub rdp_domain: Option<String>,
37    /// Additional FreeRDP client arguments (from `#_RdpArgs` comments).
38    pub rdp_args: Vec<String>,
39    /// Whether to hide this host from the interactive host view (from `#_hidden` comment).
40    pub hidden: bool,
41    /// Local forward settings.
42    pub local_forward: Vec<String>,
43    /// Remote forward settings.
44    pub remote_forward: Vec<String>,
45    /// Additional custom options.
46    pub other_options: SshOptionMap,
47}
48
49impl SshHost {
50    /// Create a new `SshHost` with just a name.
51    pub fn new(name: String) -> Self {
52        Self {
53            name,
54            protocol: ConnectionProtocol::Ssh,
55            hostname: None,
56            user: None,
57            port: None,
58            identity_files: Vec::new(),
59            identities_only: None,
60            proxy_jump: None,
61            proxy_command: None,
62            forward_agent: None,
63            description: None,
64            profile: None,
65            pass_key: None,
66            rdp_domain: None,
67            rdp_args: Vec::new(),
68            hidden: false,
69            local_forward: Vec::new(),
70            remote_forward: Vec::new(),
71            other_options: SshOptionMap::new(),
72        }
73    }
74}
75
76/// Parsed SSH host data and include graph as a folder tree.
77#[derive(Debug, Clone)]
78pub struct SshHostTreeModel {
79    /// Root folder (`~/.ssh/config`).
80    pub root: TreeFolder,
81    /// Flattened host list in discovery order.
82    pub hosts: Vec<SshHost>,
83}
84
85impl SshHostTreeModel {
86    #[allow(dead_code)]
87    pub(super) fn empty(root_path: PathBuf) -> Self {
88        let root_name = root_path.file_name().and_then(|segment| segment.to_str()).unwrap_or("config").to_string();
89        Self {
90            root: TreeFolder {
91                id: 0,
92                name: root_name,
93                path: root_path,
94                children: Vec::new(),
95                host_indices: Vec::new(),
96            },
97            hosts: Vec::new(),
98        }
99    }
100}