File tree Expand file tree Collapse file tree 4 files changed +59
-2
lines changed
Expand file tree Collapse file tree 4 files changed +59
-2
lines changed Original file line number Diff line number Diff line change 11[package ]
22name = " os_type"
3- version = " 0.3 .0"
3+ version = " 0.4 .0"
44authors = [" Jan Schulte <hello@unexpected-co.de>" ]
55license = " MIT"
66description = " Detect the operating system type"
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ use std::fs;
33use std:: convert:: AsRef ;
44use std:: path:: Path ;
55mod 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
1820fn 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+
2737fn 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments