printnanny_cli/
os.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
use anyhow::{anyhow, Result};
use clap::ArgMatches;
use log::error;
use std::fs;

use printnanny_services::metadata;
use printnanny_settings::printnanny::PrintNannySettings;
use printnanny_settings::SettingsFormat;
pub struct OsCommand;

const MTOD_HEADER: &str = r"

_____      _       _   _   _                         
|  __ \    (_)     | | | \ | |                        
| |__) | __ _ _ __ | |_|  \| | __ _ _ __  _ __  _   _ 
|  ___/ '__| | '_ \| __| . ` |/ _` | '_ \| '_ \| | | |
| |   | |  | | | | | |_| |\  | (_| | | | | | | | |_| |
|_|   |_|  |_|_| |_|\__|_| \_|\__,_|_| |_|_| |_|\__, |
                                                 __/ |
                                                |___/ 
";

async fn handle_issue() -> Result<()> {
    let config = PrintNannySettings::new().await?;
    let result = fs::read_to_string(&config.paths.issue_txt);
    let output = match result {
        Ok(content) => content,
        Err(e) => {
            let msg = format!(
                "Error reading file={:?} error={:?}",
                &config.paths.issue_txt, e
            );
            error!(
                "Error reading file={:?} error={:?}",
                &config.paths.issue_txt, e
            );
            msg
        }
    };
    print!("{}", output);
    Ok(())
}

async fn handle_motd() -> Result<()> {
    print!("{}", &MTOD_HEADER);
    handle_issue().await
}

fn handle_system_info(args: &ArgMatches) -> Result<()> {
    let system_info = metadata::system_info()?;
    let format = args.value_of_t::<SettingsFormat>("format")?;
    let output = match format {
        SettingsFormat::Json => serde_json::to_string(&system_info)?,
        SettingsFormat::Toml => toml::ser::to_string(&system_info)?,
        SettingsFormat::Ini | SettingsFormat::Yaml => todo!(),
    };
    print!("{}", &output);
    Ok(())
}

impl OsCommand {
    pub async fn handle(sub_m: &clap::ArgMatches) -> Result<()> {
        match sub_m.subcommand() {
            Some(("issue", _args)) => handle_issue().await,
            Some(("motd", _args)) => handle_motd().await,
            Some(("system-info", args)) => handle_system_info(args),

            _ => Err(anyhow!("Unhandled subcommand")),
        }
    }
}