Skip to content

Commit

Permalink
Merge pull request #4 from schultyy/add-debian-support
Browse files Browse the repository at this point in the history
Add debian support
  • Loading branch information
Jan Schulte committed Oct 24, 2015
2 parents 5f513b1 + 00ea514 commit 6ed7a11
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub enum OSType {
Unknown,
Redhat,
OSX,
Ubuntu
Ubuntu,
Debian
}

fn file_exists<P: AsRef<Path>>(path: P) -> bool {
Expand All @@ -31,16 +32,19 @@ fn is_os_x() -> bool {
}

fn lsb_release() -> OSType {
match lsb_release::from_file("/etc/lsb-release") {
Ok(release) => {
match lsb_release::retrieve() {
Some(release) => {
if release.distro == Some("Ubuntu".to_string()) {
OSType::Ubuntu
}
else if release.distro == Some("Debian".to_string()) {
OSType::Debian
}
else {
OSType::Unknown
}
},
Err(_) => OSType::Unknown
None => OSType::Unknown
}

}
Expand All @@ -57,7 +61,7 @@ pub fn current_platform() -> OSType {
if is_os_x() {
OSType::OSX
}
else if file_exists("/etc/lsb-release") {
else if lsb_release::is_available() {
lsb_release()
}
else if file_exists("/etc/redhat-release") || file_exists("/etc/centos-release") {
Expand Down
35 changes: 14 additions & 21 deletions src/lsb_release.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
extern crate regex;
use self::regex::Regex;
use std::io::prelude::*;
use std::fs::File;
use std::convert::AsRef;
use std::path::Path;
use std::io::Error;
use std::process::Command;

pub struct LsbRelease {
pub distro: Option<String>,
pub version: Option<String>
}

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<LsbRelease, Error> {
let mut handle = match File::open(path) {
Ok(h) => h,
Err(err) => return Err(err)
pub fn retrieve() -> Option<LsbRelease> {
let output = match Command::new("lsb_release").arg("-a").output() {
Ok(o) => o,
Err(_) =>return None
};
let stdout = String::from_utf8_lossy(&output.stdout);
Some(parse(stdout.to_string()))
}

let mut file_content = String::new();

match handle.read_to_string(&mut file_content) {
Ok(_) => {
let release = parse(file_content);
Ok(release)
},
Err(err) => {
Err(err)
}
pub fn is_available() -> bool {
match Command::new("lsb_release").output() {
Ok(_) => true,
Err(_) => false
}
}

pub fn parse(file: String) -> LsbRelease {
let distrib_regex = Regex::new(r"DISTRIB_ID=(\w+)").unwrap();
let distrib_release_regex = Regex::new(r"DISTRIB_RELEASE=([\w\.]+)").unwrap();
let distrib_regex = Regex::new(r"Distributor ID:\s(\w+)").unwrap();
let distrib_release_regex = Regex::new(r"Release:\s([\w\.]+)").unwrap();

let distro = match distrib_regex.captures_iter(&file).next() {
Some(m) => {
Expand Down
14 changes: 7 additions & 7 deletions tests/lsb_release_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ mod lsb_release;

fn file() -> String {
"
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION=\"Ubuntu 14.04.2 LTS\"
".to_string()
Distributor ID: Debian
Description: Debian GNU/Linux 7.8 (wheezy)
Release: 7.8
Codename: wheezy
".to_string()
}

#[test]
pub fn test_parses_lsb_distro() {
let parse_results = lsb_release::parse(file());
assert_eq!(parse_results.distro, Some("Ubuntu".to_string()));
assert_eq!(parse_results.distro, Some("Debian".to_string()));
}

#[test]
pub fn test_parses_lsb_version() {
let parse_results = lsb_release::parse(file());
assert_eq!(parse_results.version, Some("14.04".to_string()));
assert_eq!(parse_results.version, Some("7.8".to_string()));
}

0 comments on commit 6ed7a11

Please sign in to comment.