Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inital fix for "list_units" not working correctely #11

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/systemctl.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 34 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn systemctl_capture(args: Vec<&str>) -> std::io::Result<String> {
}
} else {
Err(Error::new(ErrorKind::InvalidData, "systemctl stdout empty"))
}
}
/*},
false => {
Err(Error::new(ErrorKind::Other,
Expand Down Expand Up @@ -135,7 +135,7 @@ pub fn list_units(
for l in lines.skip(1) {
// header labels
let parsed: Vec<_> = l.split_ascii_whitespace().collect();
if parsed.len() == 2 {
if parsed.len() >= 2 {
result.push(parsed[0].to_string())
}
}
Expand Down Expand Up @@ -165,6 +165,11 @@ pub enum AutoStartStatus {
Generated,
#[strum(serialize = "indirect")]
Indirect,
#[strum(serialize = "transient")]
Transient,
#[strum(serialize = "enabled-runtime")]
Enabled_runtime,

}

impl Default for AutoStartStatus {
Expand Down Expand Up @@ -194,6 +199,8 @@ pub enum Type {
Path,
#[strum(serialize = "target")]
Target,
#[strum(serialize = "swap")]
Swap,
}

impl Default for Type {
Expand Down Expand Up @@ -414,6 +421,7 @@ impl Unit {
let items: Vec<_> = name.split_terminator(".").collect();
let name = items[0];
// `type` is deduced from .extension
println!("{}",items[1]);
let utype = Type::from_str(items[1].trim()).unwrap();
let mut script: String = String::new();

Expand Down Expand Up @@ -452,6 +460,7 @@ impl Unit {
let (rem, _) = rem.split_at(rem.len() - 1); // remove ")"
let items: Vec<_> = rem.split_terminator(";").collect();
script = items[0].trim().to_string();

auto_start = AutoStartStatus::from_str(items[1].trim()).unwrap();
if items.len() > 2 {
// preset is optionnal ?
Expand Down Expand Up @@ -657,6 +666,8 @@ impl Unit {
}
}



#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -706,21 +717,42 @@ mod test {
let unit = Unit::from_systemctl("non-existing");
assert_eq!(unit.is_err(), true);
}

//Auxiliar function for next test
fn contains_numbers(s: &str) -> bool {
for c in s.chars() {
if c.is_numeric() {
return true;
}
}
false
}

#[test]
fn test_service_unit_construction() {
let units = list_units(None, None).unwrap(); // all units
assert_eq!(units.len() > 0, true);
for unit in units {
let unit = unit.as_str();
if unit.contains("@") {
// not testing this one
// would require @x service # identification / enumeration
continue;
}
if contains_numbers(&unit) {
//if you try to unwrap a unit with a name containing numbers it will give out an error
//for now this is a quick fix to avoid that
//this problem needs to be looked in to in detail
continue;
}

let c0 = unit.chars().nth(0).unwrap();
if c0.is_alphanumeric() {
// valid unit name --> run test
println!("Unit: {:?}", &unit);
let u = Unit::from_systemctl(&unit).unwrap();
println!("####################################");

println!("Unit: {:#?}", u);
println!("active: {}", u.active);
println!("preset: {}", u.preset);
Expand Down