summaryrefslogtreecommitdiff
path: root/src/systemd/unit.rs
blob: 3be7fa56787621cc0e9f416f664a36dae5b3ecc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::collections::HashMap;
use std::process::Command;

use std::io;

#[derive(Debug, PartialEq, Eq)]
pub struct Unit {
    pub name: String,
    pub type_: String
}

pub fn parse_key_value(line: &str) -> Option<(String, String)> {
    let pos = line.find('=');
    pos.map(|pos| {
        let (k, v) = line.split_at(pos);
        (k.to_owned(), v[1..].to_owned())
    })
}

#[test]
fn test_parse_key_value() {
    assert_eq!(None, parse_key_value("ab"));
    assert_eq!(Some(("a".to_owned(), "b".to_owned())), parse_key_value("a=b"));
    assert_eq!(Some(("a".to_owned(), "b=c".to_owned())), parse_key_value("a=b=c"));
}

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());

    let mut unit_info = HashMap::new();
    for line in String::from_utf8_lossy(&status.stdout).split('\n') {
        if let Some((k, v)) = parse_key_value(line) {
            unit_info.insert(k, v);
        } else {
            make_unit(unit_info).map(|u| units.push(u));
            unit_info = HashMap::new();
        }
    }
    return Ok(units);
}