Skip to main content

mur_common/config/
ssh_setup.rs

1//! SSH setup journey switches (`ssh_setup:`).
2
3use super::*;
4
5/// Dedicated-key generation and public-key install are offered.
6pub const SSH_SETUP_DEFAULT_GENERATE_KEY: bool = true;
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct SshSetupConfig {
10    /// Off: SSH setup offers no dedicated key and no public-key install.
11    /// A key that already exists is still loaded at start and still removed
12    /// with its agent, so turning this off strands no host.
13    #[serde(default = "ssh_setup_default_generate_key")]
14    pub generate_key: bool,
15}
16
17impl Default for SshSetupConfig {
18    fn default() -> Self {
19        Self {
20            generate_key: ssh_setup_default_generate_key(),
21        }
22    }
23}
24
25fn ssh_setup_default_generate_key() -> bool {
26    SSH_SETUP_DEFAULT_GENERATE_KEY
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn generate_key_is_on_when_the_block_is_absent() {
35        let c: Config = serde_yaml::from_str("{}").unwrap();
36        assert_eq!(c.ssh_setup.generate_key, SSH_SETUP_DEFAULT_GENERATE_KEY);
37        assert!(c.ssh_setup.generate_key);
38    }
39
40    #[test]
41    fn generate_key_is_on_when_the_block_is_empty() {
42        let c: Config = serde_yaml::from_str("ssh_setup: {}\n").unwrap();
43        assert!(c.ssh_setup.generate_key);
44    }
45
46    #[test]
47    fn generate_key_can_be_turned_off() {
48        let c: Config = serde_yaml::from_str("ssh_setup:\n  generate_key: false\n").unwrap();
49        assert!(!c.ssh_setup.generate_key);
50    }
51
52    #[test]
53    fn the_switch_survives_a_round_trip() {
54        let c: Config = serde_yaml::from_str("ssh_setup:\n  generate_key: false\n").unwrap();
55        let back: Config = serde_yaml::from_str(&serde_yaml::to_string(&c).unwrap()).unwrap();
56        assert_eq!(back.ssh_setup, c.ssh_setup);
57        // An unrelated config keeps the default through a round trip.
58        let plain: Config = serde_yaml::from_str("llm: {}\n").unwrap();
59        let back: Config = serde_yaml::from_str(&serde_yaml::to_string(&plain).unwrap()).unwrap();
60        assert_eq!(back.ssh_setup, SshSetupConfig::default());
61    }
62}