1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
extern crate regex;

use std::process::Command;
mod lsb_release;
mod windows_ver;
mod rhel_release;
mod sw_vers;
mod utils;

///A list of supported operating system types
#[derive(Debug)]
#[derive(PartialEq)]
#[derive(Clone)]
pub enum OSType {
    Unknown,
    Redhat,
    OSX,
    Ubuntu,
    Debian,
    Arch,
    CentOS
}

/// Holds information about Operating System type and its version
/// If the version could not be fetched it defaults to `0.0.0`
#[derive(Debug)]
#[derive(Clone)]
#[derive(PartialEq)]
pub struct OSInformation {
    pub os_type: self::OSType,
    pub version: String
}

fn default_version() -> String {
    "0.0.0".into()
}

fn unknown_os() -> OSInformation {
    OSInformation {
        os_type: OSType::Unknown,
        version: default_version()
    }
}

fn is_os_x() -> bool {
    match Command::new("sw_vers").output() {
        Ok(output) => output.status.success(),
        Err(_) => false
    }
}

fn get_sw_vers() -> OSInformation {
    if let Some(osx_info) = sw_vers::retrieve() {
        OSInformation {
            os_type: OSType::OSX,
            version: osx_info.product_version.unwrap_or(default_version())
        }
    } else {
        unknown_os()
    }
}

fn lsb_release() -> OSInformation {
    match lsb_release::retrieve() {
        Some(release) => {
            if release.distro == Some("Ubuntu".to_string()) {
                OSInformation {
                    os_type: OSType::Ubuntu,
                    version: release.version.unwrap_or(default_version())
                }
            }
            else if release.distro == Some("Debian".to_string()) {
                OSInformation {
                    os_type: OSType::Debian,
                    version: release.version.unwrap_or(default_version())
                }
            } else if release.distro == Some("Arch".to_string()) {
                OSInformation {
                    os_type: OSType::Arch,
                    version: release.version.unwrap_or(default_version())
                }
            }
            else if release.distro == Some("CentOS".to_string()){
                OSInformation {
                    os_type: OSType::CentOS,
                    version: release.version.unwrap_or(default_version())
                }
            }
            else {
                unknown_os()
            }
        },
        None => unknown_os()
    }
}

fn rhel_release() -> OSInformation {
    match rhel_release::retrieve() {
        Some(release) => {
            if release.distro == Some("CentOS".to_string()) {
                OSInformation {
                    os_type: OSType::CentOS,
                    version: release.version.unwrap_or(default_version())
                }
            } else {
                OSInformation {
                    os_type: OSType::Redhat,
                    version: release.version.unwrap_or(default_version())
                }
            }
        },
        None => unknown_os()
    }
}

///Returns the current operating system type
///
///#Example
///
///```
///use os_type;
///let os = os_type::current_platform();
///println!("Type: {:?}", os.os_type);
///println!("Version: {}", os.version);
///```
pub fn current_platform() -> OSInformation {
    if is_os_x() {
        get_sw_vers()
    }
    else if lsb_release::is_available() {
        lsb_release()
    }
    else if utils::file_exists("/etc/redhat-release") || utils::file_exists("/etc/centos-release") {
        rhel_release()
    }
    else {
        unknown_os()
    }
}