Skip to content

Commit f98eec4

Browse files
author
Jan Schulte
committed
Merge pull request #5 from schultyy/add-windows-support
Add windows support
2 parents 5722fe4 + 3db5af5 commit f98eec4

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "os_type"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
authors = ["Jan Schulte <hello@unexpected-co.de>"]
55
license = "MIT"
66
description = "Detect the operating system type"

src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs;
33
use std::convert::AsRef;
44
use std::path::Path;
55
mod lsb_release;
6+
mod windows_ver;
67

78
///A list of supported operating system types
89
#[derive(Debug)]
@@ -12,7 +13,8 @@ pub enum OSType {
1213
Redhat,
1314
OSX,
1415
Ubuntu,
15-
Debian
16+
Debian,
17+
Windows
1618
}
1719

1820
fn file_exists<P: AsRef<Path>>(path: P) -> bool {
@@ -24,6 +26,14 @@ fn file_exists<P: AsRef<Path>>(path: P) -> bool {
2426
}
2527
}
2628

29+
fn is_windows() -> bool {
30+
if cfg!(target_os="windows") {
31+
return true;
32+
} else {
33+
return false;
34+
}
35+
}
36+
2737
fn is_os_x() -> bool {
2838
match Command::new("sw_vers").output() {
2939
Ok(output) => output.status.success(),
@@ -61,6 +71,9 @@ pub fn current_platform() -> OSType {
6171
if is_os_x() {
6272
OSType::OSX
6373
}
74+
else if is_windows() {
75+
OSType::Windows
76+
}
6477
else if lsb_release::is_available() {
6578
lsb_release()
6679
}

src/windows_ver.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
extern crate regex;
2+
3+
use self::regex::Regex;
4+
use std::process::Command;
5+
6+
pub struct WindowsVer {
7+
pub version: Option<String>
8+
}
9+
10+
pub fn retrieve() -> Option<WindowsVer> {
11+
let output = match Command::new("ver").output() {
12+
Ok(o) => o,
13+
Err(_) => return None
14+
};
15+
let stdout = String::from_utf8_lossy(&output.stdout);
16+
Some(parse(stdout.to_string()))
17+
}
18+
19+
pub fn parse(output: String) -> WindowsVer {
20+
let version_regex = Regex::new(r"^Microsoft Windows \[Version\s(\d+\.\d+\.\d+)\]$").unwrap();
21+
22+
let version = match version_regex.captures_iter(&output).next() {
23+
Some(m) => {
24+
match m.at(1) {
25+
Some(version) => Some(version.to_string()),
26+
None => None
27+
}
28+
},
29+
None => None
30+
};
31+
WindowsVer { version: version }
32+
}

tests/windows_ver_test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[path="../src/windows_ver.rs"]
2+
mod windows_ver;
3+
4+
fn output() -> String {
5+
"Microsoft Windows [Version 6.1.7601]".into()
6+
}
7+
8+
#[test]
9+
pub fn test_parses_version() {
10+
let parse_results = windows_ver::parse(output());
11+
assert_eq!(parse_results.version, Some("6.1.7601".into()));
12+
}

0 commit comments

Comments
 (0)