Rewrite upcheck in Rust.

This commit is contained in:
Eduard Tolosa 2019-03-26 01:39:14 -05:00
parent 3ffdef0169
commit aa4fcbfc27
3 changed files with 64 additions and 0 deletions

8
Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "upcheck"
version = "0.1.0"
authors = ["Eduard Toloza <tolosaeduard@gmail.com>"]
edition = "2018"
[dependencies]
notify-rust = "^3"

56
src/main.rs Normal file
View file

@ -0,0 +1,56 @@
// Crate for notifications
extern crate notify_rust;
use notify_rust::Notification;
use std::path::Path;
use std::process::Command;
fn main() {
upcheck();
}
fn upcheck() {
// Declare variables
let checkupdates = "/usr/bin/checkupdates";
// check if pacman-contrib and libnotify are installed
if Path::new(&checkupdates).exists() {
// Get output of checkupdates command
let available_updates = Command::new(&checkupdates)
.output()
.expect("Failed to execute the checkupdates command.");
// if the previous command exit with sucess, continue.
if available_updates.status.success() {
// Convert the output of the command into a string.
let available_updates = String::from_utf8(available_updates.stdout)
.expect("Output of checkupdates could not be parsed as valid UTF-8.");
// Send the notification if updates are or not available
if &available_updates.len() > &0 {
Notification::new()
.summary("The following updates are available:")
.body(&available_updates)
.icon("dialog-information")
.show()
.unwrap();
} else {
Notification::new()
.summary("No updates are available.")
.icon("dialog-information")
.show()
.unwrap();
}
} else {
eprintln!("Process exited with: {}", available_updates.status);
eprintln!(
"\nMore information about the error:\n\n {:#?}",
available_updates
);
}
} else {
eprintln!(
"File {} doesn't exist, please install the pacman-contrib package.",
&checkupdates
);
}
}

BIN
upcheck

Binary file not shown.