Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle/profile: Enable home detection on Windows
✗ CI failure Lorenz Leutgeb committed 10 months ago
commit beb5ee6d0f39d49b990720847a9805390d62306a
parent f90cc2981292244554945ed6a98af103d581f682
1 failed (1 total) View logs
1 file changed +52 -8
modified crates/radicle/src/profile.rs
@@ -450,15 +450,59 @@ impl AliasStore for Aliases {

/// Get the path to the radicle home folder.
pub fn home() -> Result<Home, io::Error> {
-
    if let Some(home) = env::var_os(env::RAD_HOME) {
-
        Ok(Home::new(PathBuf::from(home))?)
-
    } else if let Some(home) = env::var_os("HOME") {
-
        Ok(Home::new(PathBuf::from(home).join(".radicle"))?)
-
    } else {
-
        Err(io::Error::new(
+
    #[cfg(unix)]
+
    const ERROR_MESSAGE_UNSET: &str =
+
        "Environment variables `RAD_HOME` and `HOME` are both unset or not valid Unicode.";
+

+
    #[cfg(windows)]
+
    const ERROR_MESSAGE_UNSET: &str =
+
        "Environment variables `RAD_HOME`, `HOME`, and `USERPROFILE` are all unset or not valid Unicode.";
+

+
    struct DetectedHome {
+
        path: String,
+

+
        /// Depending on the detection method, we may need to join `.radicle` to the detected path.
+
        join_dot_radicle: bool,
+
    }
+

+
    let detected = {
+
        match env::var(env::RAD_HOME).ok() {
+
            Some(home) => Some(DetectedHome {
+
                path: home,
+
                join_dot_radicle: false,
+
            }),
+
            None => {
+
                let mut home = env::var("HOME").ok();
+

+
                if home.is_none() && cfg!(windows) {
+
                    home = env::var("USERPROFILE").ok();
+
                }
+

+
                home.map(|home| DetectedHome {
+
                    path: home,
+
                    join_dot_radicle: true,
+
                })
+
            }
+
        }
+
    };
+

+
    match detected {
+
        Some(DetectedHome {
+
            path: home,
+
            join_dot_radicle,
+
        }) => {
+
            let mut home = PathBuf::from(home);
+

+
            if join_dot_radicle {
+
                home = home.join(".radicle");
+
            }
+

+
            Ok(Home::new(home)?)
+
        }
+
        None => Err(io::Error::new(
            io::ErrorKind::NotFound,
-
            "Neither `RAD_HOME` nor `HOME` are set",
-
        ))
+
            ERROR_MESSAGE_UNSET.to_string(),
+
        )),
    }
}