Skip to content

Commit

Permalink
Merge pull request #5 from schultyy/add-windows-support
Browse files Browse the repository at this point in the history
Add windows support
  • Loading branch information
Jan Schulte committed Nov 29, 2015
2 parents 5722fe4 + 3db5af5 commit f98eec4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "os_type"
version = "0.3.0"
version = "0.4.0"
authors = ["Jan Schulte <[email protected]>"]
license = "MIT"
description = "Detect the operating system type"
Expand Down
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs;
use std::convert::AsRef;
use std::path::Path;
mod lsb_release;
mod windows_ver;

///A list of supported operating system types
#[derive(Debug)]
Expand All @@ -12,7 +13,8 @@ pub enum OSType {
Redhat,
OSX,
Ubuntu,
Debian
Debian,
Windows
}

fn file_exists<P: AsRef<Path>>(path: P) -> bool {
Expand All @@ -24,6 +26,14 @@ fn file_exists<P: AsRef<Path>>(path: P) -> bool {
}
}

fn is_windows() -> bool {
if cfg!(target_os="windows") {
return true;
} else {
return false;
}
}

fn is_os_x() -> bool {
match Command::new("sw_vers").output() {
Ok(output) => output.status.success(),
Expand Down Expand Up @@ -61,6 +71,9 @@ pub fn current_platform() -> OSType {
if is_os_x() {
OSType::OSX
}
else if is_windows() {
OSType::Windows
}
else if lsb_release::is_available() {
lsb_release()
}
Expand Down
32 changes: 32 additions & 0 deletions src/windows_ver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
extern crate regex;

use self::regex::Regex;
use std::process::Command;

pub struct WindowsVer {
pub version: Option<String>
}

pub fn retrieve() -> Option<WindowsVer> {
let output = match Command::new("ver").output() {
Ok(o) => o,
Err(_) => return None
};
let stdout = String::from_utf8_lossy(&output.stdout);
Some(parse(stdout.to_string()))
}

pub fn parse(output: String) -> WindowsVer {
let version_regex = Regex::new(r"^Microsoft Windows \[Version\s(\d+\.\d+\.\d+)\]$").unwrap();

let version = match version_regex.captures_iter(&output).next() {
Some(m) => {
match m.at(1) {
Some(version) => Some(version.to_string()),
None => None
}
},
None => None
};
WindowsVer { version: version }
}
12 changes: 12 additions & 0 deletions tests/windows_ver_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[path="../src/windows_ver.rs"]
mod windows_ver;

fn output() -> String {
"Microsoft Windows [Version 6.1.7601]".into()
}

#[test]
pub fn test_parses_version() {
let parse_results = windows_ver::parse(output());
assert_eq!(parse_results.version, Some("6.1.7601".into()));
}

0 comments on commit f98eec4

Please sign in to comment.