diff options
Diffstat (limited to 'src/systemd')
| -rw-r--r-- | src/systemd/mod.rs | 2 | ||||
| -rw-r--r-- | src/systemd/unit.rs (renamed from src/systemd/job.rs) | 33 |
2 files changed, 29 insertions, 6 deletions
diff --git a/src/systemd/mod.rs b/src/systemd/mod.rs index 80daa3e..a5acbce 100644 --- a/src/systemd/mod.rs +++ b/src/systemd/mod.rs @@ -1 +1 @@ -pub mod job; +pub mod unit; diff --git a/src/systemd/job.rs b/src/systemd/unit.rs index 967bb42..3be7fa5 100644 --- a/src/systemd/job.rs +++ b/src/systemd/unit.rs @@ -3,7 +3,7 @@ use std::process::Command; use std::io; -#[derive(Debug)] +#[derive(Debug, PartialEq, Eq)] pub struct Unit { pub name: String, pub type_: String @@ -24,7 +24,32 @@ fn test_parse_key_value() { assert_eq!(Some(("a".to_owned(), "b=c".to_owned())), parse_key_value("a=b=c")); } -pub fn get_jobs() -> io::Result<Vec<Unit>> { +fn make_unit(info: HashMap<String, String>) -> Option<Unit> { + fn from_id(id: &str) -> Option<String> { + id.rsplit('.').next().map(|t| t.to_owned()) + }; + let id = info.get("Id").map(|id| id.clone()); + let type_ = id.as_ref().and_then(|id| from_id(&id)); + + if let (Some(id), Some(type_)) = (id, type_) { + Some(Unit { name: id, type_: type_ }) + } else { + None + } +} + +#[test] +fn test_make_unit() { + let s = |s: &str| -> String { s.to_owned() }; + let info = [(s("Id"), s("Test.service"))] + .iter().cloned().collect::<HashMap<String, String>>(); + assert_eq!(Some(Unit { name: s("Test.service"), type_: s("service") }), make_unit(info)); + + let info = HashMap::new(); + assert_eq!(None, make_unit(info)); +} + +pub fn get_units() -> io::Result<Vec<Unit>> { let mut units = Vec::new(); let status = try!(Command::new("systemctl") .args(&["show", "*"]).output()); @@ -34,9 +59,7 @@ pub fn get_jobs() -> io::Result<Vec<Unit>> { if let Some((k, v)) = parse_key_value(line) { unit_info.insert(k, v); } else { - if let Some(id) = unit_info.remove("Id") { - units.push(Unit { name: id, type_: String::new() }); - } + make_unit(unit_info).map(|u| units.push(u)); unit_info = HashMap::new(); } } |
