~deadjakk/RiceStats

dfa8419961b50934b829f4f3f6830205a342dfc5 — deadjakk 2 years ago 137809c
added pacman support
4 files changed, 58 insertions(+), 18 deletions(-)

M README.md
M src/lib.rs
M src/main.rs
M src/updates_utils.rs
M README.md => README.md +7 -6
@@ 25,11 25,13 @@ USAGE:
    rice_stats [FLAGS] [OPTIONS]

FLAGS:
    -a, --apt           This flag will cause the program to run `apt update -y && apt upgrade` updating the apt
                        repository but NOT the packages and then output the parsed number of available updates REQUIRES
                        SUID bit to be set on this binary via: sudo chown <this binary> && sudo chmod u+s <this binary>
                        or alternatively this binary can be run as an elevated user
    -h, --help          Prints help information
    -p, --packages      When this flag is used, the existence of pacman or apt will be determnined by running the
                        'which' program. if apt is present, it will run 'apt update -y'. if pacman is present, it will
                        run 'pacman -Syu'. No updates will actually take place, it will only parse the output to
                        determine the number of available packages and then output the number. REQUIRES SUID bit to be
                        set on this binary via: sudo chown <this binary> && sudo chmod u+s <this binary> or
                        alternatively this binary can be run as an elevated user
    -s, --sys-uptime    Return the system uptime in days
    -u, --usage         Return the current network usage for the primary interface
    -V, --version       Prints version information


@@ 38,11 40,10 @@ OPTIONS:
    -i, --interface <interface>    Override the interface that is used for several commands. If this is not provided an
                                   attempt will be made to find the primary interface based on the presence of an ip and
                                   then traffic rate

``` 

## Usage  
```
~$ rice_stats -a -u
~$ rice_stats -p -u
rx-8869 tx-9323 0 Updates Available
```

M src/lib.rs => src/lib.rs +10 -6
@@ 1,4 1,4 @@
pub use std::{fmt,error::Error,process::Command};
pub use std::{fmt,error::Error,process::Command,process::Output};

#[derive(Debug)]
pub enum RiceError {


@@ 26,12 26,16 @@ impl fmt::Display for RiceError{
    }
}

pub fn raw_command(command: &str ) -> Output {
    Command::new("sh")
        .arg("-c")
        .arg(&command)
        .output()
        .expect("failed to execute process")
}

pub fn run_command(command : &str, output_line: &mut String) -> Result<(),RiceError>{
    let output = Command::new("sh")
                .arg("-c")
                .arg(&command)
                .output()
                .expect("failed to execute process");
    let output = raw_command(&command);

    match output.status.success() {
        true => {

M src/main.rs => src/main.rs +8 -5
@@ 18,13 18,16 @@ struct UserOptions {
    interface: Option<String>,

    #[structopt(short,long)]
    /// This flag will cause the program to run `apt update -y && apt upgrade` 
    /// updating the apt repository but NOT the packages
    /// and then output the parsed number of available updates
    /// When this flag is used, the existence of pacman or apt will
    /// be determnined by running the 'which' program.
    /// if apt is present, it will run 'apt update -y'. 
    /// if pacman is present, it will run 'pacman -Syu'.
    /// No updates will actually take place, it will only parse the output
    /// to determine the number of available packages and then output the number.
    /// REQUIRES SUID bit to be set on this binary via:
    /// sudo chown <this binary> && sudo chmod u+s <this binary>
    /// or alternatively this binary can be run as an elevated user
    apt: bool,
    packages: bool,

    #[structopt(short,long)]
    /// Return the current network usage for the primary interface


@@ 45,7 48,7 @@ fn main() {
        }
    }

    if args.apt == true {
    if args.packages == true {
        if let Err(e) = get_updates(&mut output_line){
            eprintln!("Error received attempting show_updates: {}",e);
        }

M src/updates_utils.rs => src/updates_utils.rs +33 -1
@@ 1,5 1,7 @@
use rice_stats::{RiceError,run_command};
use rice_stats::{RiceError,run_command,raw_command};
use sudo::{check,RunningAs,escalate_if_needed};
use std::str::from_utf8;
use std::error::Error;
use regex::Regex;

pub fn get_updates(output_line: &mut String) -> Result<(),RiceError> {


@@ 24,9 26,39 @@ pub fn get_updates(output_line: &mut String) -> Result<(),RiceError> {
        }
        Err(_) => (),
    }
    match run_command("which pacman",&mut "".to_string()) {
        Ok(_) => {
            if let Err(_) = pacman_poll(output_line){
            }
            return Ok(());
        }
        Err(_) => (),
    }
    Ok(())
}

fn pacman_poll (output_line: &mut String) -> Result<(),Box<dyn Error>> {
    let output = raw_command("pacman -Syu");
    let output = from_utf8(&output.stdout)?;

    let pattern = Regex::new("\\(\\d+?").unwrap();
    let p_match = pattern.captures(&output);
    match p_match { 
        Some(v) => {
            let mut num_packages = v[0].to_string();
            num_packages.remove(0);
            output_line.push_str(&num_packages);
            output_line.push_str(" Updates Available ");
            return Ok(());
        },
        None => {
            // Updates were not found
            output_line.push_str("0 Updates Available ");
            return Ok(());
        }
    }
}

fn apt_poll (output_line: &mut String) -> Result<(),RiceError> {
    let mut command_to_parse = String::new();
    if let Err(_) = run_command("apt update -y", &mut command_to_parse){