1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
56use aws_types::os_shim_internal;
78/// An operating system, like Windows or Linux
9#[derive(Debug, Copy, Clone, Eq, PartialEq)]
10#[non_exhaustive]
11pub enum Os {
12/// A Windows-based operating system
13Windows,
14/// Any Unix-based operating system
15Unix,
16}
1718impl Os {
19/// Returns the current operating system
20pub fn real() -> Self {
21match std::env::consts::OS {
22"windows" => Os::Windows,
23_ => Os::Unix,
24 }
25 }
26}
2728/// Resolve a home directory given a set of environment variables
29pub fn home_dir(env_var: &os_shim_internal::Env, os: Os) -> Option<String> {
30if let Ok(home) = env_var.get("HOME") {
31tracing::debug!(src = "HOME", "loaded home directory");
32return Some(home);
33 }
3435if os == Os::Windows {
36if let Ok(home) = env_var.get("USERPROFILE") {
37tracing::debug!(src = "USERPROFILE", "loaded home directory");
38return Some(home);
39 }
4041let home_drive = env_var.get("HOMEDRIVE");
42let home_path = env_var.get("HOMEPATH");
43tracing::debug!(src = "HOMEDRIVE/HOMEPATH", "loaded home directory");
44if let (Ok(mut drive), Ok(path)) = (home_drive, home_path) {
45 drive.push_str(&path);
46return Some(drive);
47 }
48 }
49None
50}
5152#[cfg(test)]
53mod test {
54use super::{home_dir, Os};
55use aws_types::os_shim_internal::Env;
5657#[test]
58fn homedir_profile_only_windows() {
59// windows specific variables should only be considered when the platform is windows
60let env = Env::from_slice(&[("USERPROFILE", "C:\\Users\\name")]);
61assert_eq!(
62 home_dir(&env, Os::Windows),
63Some("C:\\Users\\name".to_string())
64 );
65assert_eq!(home_dir(&env, Os::Unix), None);
66 }
67}