-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.rs
106 lines (90 loc) · 3.04 KB
/
main.rs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::{num::ParseIntError, str::FromStr};
use clap::Parser;
use log::error;
use notify_rust::{Notification, Timeout};
mod package;
mod kernel;
use kernel::KernelChecker;
mod checks;
use checks::{Check, CheckResult};
mod critical_packages_check;
use critical_packages_check::CriticalPackagesCheck;
mod session;
fn timeout_from_str(s: &str) -> Result<Timeout, ParseIntError> {
match s {
"default" => Ok(Timeout::Default),
"never" => Ok(Timeout::Never),
milliseconds => Ok(Timeout::Milliseconds(u32::from_str(milliseconds)?)),
}
}
#[derive(Debug, Parser)]
#[clap(
version,
about = "Check the currently installed kernel against the currently running one."
)]
struct Args {
/// Disable desktop notification
#[clap(long)]
disable_notification: bool,
/// Timeout for the desktop notification in milliseconds.
///
/// * "default" will leave the timeout to be set by the server.
///
/// * "never" or "0" will cause the notification never to expire.
///
/// * Any other number will be interpreted as the timeout in milliseconds.
#[clap(long, value_parser(timeout_from_str), default_value = "default")]
notification_timeout: Timeout,
/// Comma separated list of packages were we should reboot after an upgrade.
#[clap(
long,
use_value_delimiter = true,
default_value = "systemd,linux-firmware,amd-ucode,intel-ucode"
)]
reboot_packages: Vec<String>,
/// Comma separated list of packages were we should restart our session after an upgrade.
#[clap(
long,
use_value_delimiter = true,
default_value = "xorg-server,xorg-xwayland"
)]
session_restart_packages: Vec<String>,
}
fn main() {
env_logger::init();
let args = Args::parse();
// Initialize Pacman database
let alpm = alpm::Alpm::new("/", "/var/lib/pacman/")
.expect("Could not open pacman database at /var/lib/pacman");
let db = alpm.localdb();
let mut checkers: Vec<Box<dyn Check>> = vec![];
match KernelChecker::new(db) {
Ok(kernel_checker) => checkers.push(Box::new(kernel_checker)),
Err(err) => {
error!("Could not create kernel checker: {err:#}")
}
}
match CriticalPackagesCheck::new(args.reboot_packages, args.session_restart_packages, db) {
Ok(critical_packages_checker) => checkers.push(Box::new(critical_packages_checker)),
Err(err) => {
error!("Could not create critical package checker: {err:#}")
}
}
let result = checkers
.iter()
.map(|v| v.check())
.max()
.expect("No checkers could run");
if result > CheckResult::Nothing {
println!("{}", result.summary());
if !args.disable_notification {
Notification::new()
.summary(result.summary())
.body(result.body())
.timeout(args.notification_timeout)
.show()
.map_err(|e| error!("Couldn't send notification: {}", e))
.ok();
}
}
}