Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
REVIEW: Also attempt to fallback to xyz when dev credential is None
Fintan Halpenny committed 1 day ago
commit 4c567a7c2a80e09fe4752dc71917ebc40c4f49ff
parent 76cbb469779f280e80b84bf7d3f6eb466b49b579
1 file changed +54 -23
modified crates/radicle-node/src/main.rs
@@ -223,29 +223,9 @@ enum ExecutionError {
/// should be migrated away from. If it is used, a warning is logged.
#[cfg(all(feature = "systemd", target_os = "linux"))]
fn load_credential(id_suffix: &str) -> Option<PathBuf> {
-
    const INFIX_NODE: &str = ".radicle.node.";
-
    const PREFIX_DEV: &str = "dev";
-
    const PREFIX_XYZ: &str = "xyz";
-

-
    let id_dev = format!("{}{}{}", PREFIX_DEV, INFIX_NODE, id_suffix);
-

-
    match radicle_systemd::credential::path(&id_dev) {
-
        Ok(option) => option,
-
        Err(err) => {
-
            log::warn!(target: "node", "Failed to obtain of the systemd credential with ID '{id_dev}': {err}");
-

-
            let id_xyz = format!("{}{}{}", PREFIX_XYZ, INFIX_NODE, id_suffix);
-
            match radicle_systemd::credential::path(&id_xyz) {
-
                Ok(option) => {
-
                    log::warn!(target: "node", "Obtained path of the systemd credential with ID '{id_xyz}'. Using this credential ID is discouraged. Please change the ID to '{id_dev}'.");
-
                    option
-
                }
-
                Err(err) => {
-
                    log::warn!(target: "node", "Failed to obtain path of the systemd credential with ID '{id_xyz}': {err}");
-
                    None
-
                }
-
            }
-
        }
+
    match credential::SystemdCredential::new(id_suffix)? {
+
        credential::SystemdCredential::DotDev(path) => Some(path),
+
        credential::SystemdCredential::DotXyz(path) => Some(path),
    }
}

@@ -455,3 +435,54 @@ fn main() {
        exit(1);
    }
}
+

+
#[cfg(all(feature = "systemd", target_os = "linux"))]
+
mod credential {
+
    use std::path::PathBuf;
+

+
    pub(super) enum SystemdCredential {
+
        DotDev(PathBuf),
+
        DotXyz(PathBuf),
+
    }
+

+
    impl SystemdCredential {
+
        const INFIX_NODE: &str = ".radicle.node.";
+
        const PREFIX_DEV: &str = "dev";
+
        const PREFIX_XYZ: &str = "xyz";
+

+
        pub fn new(id_suffix: &str) -> Option<Self> {
+
            let id_dev = Self::id_dev(id_suffix);
+
            match radicle_systemd::credential::path(&id_dev) {
+
                Ok(Some(path)) => Some(Self::DotDev(path)),
+
                Ok(None) => Self::xyz_path(id_suffix, &id_dev),
+
                Err(err) => {
+
                    log::warn!(target: "node", "Failed to obtain of the systemd credential with ID '{id_dev}': {err}");
+
                    Self::xyz_path(id_suffix, &id_dev)
+
                }
+
            }
+
        }
+

+
        fn xyz_path(id_suffix: &str, id_dev: &str) -> Option<Self> {
+
            let id_xyz = Self::id_xyz(id_suffix);
+
            match radicle_systemd::credential::path(&id_xyz) {
+
                Ok(Some(path)) => {
+
                    log::warn!(target: "node", "Obtained path of the systemd credential with ID '{id_xyz}'. Using this credential ID is discouraged. Please change the ID to '{id_dev}'.");
+
                    Some(Self::DotXyz(path))
+
                }
+
                Ok(None) => None,
+
                Err(err) => {
+
                    log::warn!(target: "node", "Failed to obtain path of the systemd credential with ID '{id_xyz}': {err}");
+
                    None
+
                }
+
            }
+
        }
+

+
        fn id_dev(id_suffix: &str) -> String {
+
            format!("{}{}{}", Self::PREFIX_DEV, Self::INFIX_NODE, id_suffix)
+
        }
+

+
        fn id_xyz(id_suffix: &str) -> String {
+
            format!("{}{}{}", Self::PREFIX_XYZ, Self::INFIX_NODE, id_suffix)
+
        }
+
    }
+
}