Command line args

This commit is contained in:
Thomas Forgione 2019-12-12 14:26:47 +01:00
parent 36a6ec1e12
commit 7d7d9a60b0
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
3 changed files with 57 additions and 5 deletions

View File

@ -14,6 +14,15 @@ pub enum Os {
} }
impl Os { impl Os {
pub fn from_name(name: &str) -> Option<Os> {
match name.to_lowercase().as_ref() {
"archlinux" | "arch linux" => Some(Os::ArchLinux),
"ubuntu" => Some(Os::Ubuntu),
_ => None,
}
}
pub fn detect() -> Option<Os> { pub fn detect() -> Option<Os> {
let lsb_release = lsb_release().unwrap(); let lsb_release = lsb_release().unwrap();
@ -27,7 +36,6 @@ impl Os {
_ => None, _ => None,
} }
} }
pub fn ascii_logo(self) -> &'static str { pub fn ascii_logo(self) -> &'static str {

View File

@ -1,5 +1,4 @@
pub const ASCII_LOGO: &'static str = " \ pub const ASCII_LOGO: &'static str = " \x1B[31;1m./+o+-\x1B[0m \
\n \x1B[31;1m./+o+-\x1B[0m \
\n \x1B[1myyyyy-\x1B[0m \x1B[31;1m-yyyyyy+\x1B[0m \ \n \x1B[1myyyyy-\x1B[0m \x1B[31;1m-yyyyyy+\x1B[0m \
\n \x1B[1m://+//////\x1B[31;1m-yyyyyyo\x1B[0m \ \n \x1B[1m://+//////\x1B[31;1m-yyyyyyo\x1B[0m \
\n \x1B[33;1m.++ \x1B[0;1m.:/++++++/-\x1B[31;1m.+sss/`\x1B[0m \ \n \x1B[33;1m.++ \x1B[0;1m.:/++++++/-\x1B[31;1m.+sss/`\x1B[0m \

View File

@ -1,4 +1,5 @@
use std::env::{args, var}; use std::env::{args, var};
use std::process::exit;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
@ -10,7 +11,35 @@ pub fn main() {
let mut hostname_file = File::open("/etc/hostname").unwrap(); let mut hostname_file = File::open("/etc/hostname").unwrap();
hostname_file.read_to_string(&mut hostname).unwrap(); hostname_file.read_to_string(&mut hostname).unwrap();
let os = Os::detect().unwrap(); let mut os = Os::detect().unwrap();
let mut iterator = args().skip(1);
loop {
let arg = match iterator.next() {
Some(arg) => arg,
None => break,
};
if arg == "-d" {
os = match iterator.next().map(|x| Os::from_name(&x)) {
Some(Some(os)) => os,
Some(None) => {
eprintln!("\x1B[31;1merror:\x1B[0m distribution not recognized");
exit(1);
},
None => {
eprintln!("\x1B[31;1merror:\x1B[0m argument -d expect distribution as parameter");
exit(1);
},
};
continue;
};
}
let style = os.style(); let style = os.style();
let unstyle = os.unstyle(); let unstyle = os.unstyle();
@ -28,13 +57,29 @@ pub fn main() {
uptime, uptime,
]; ];
for arg in args().skip(1) {
let mut iterator = args().skip(1);
loop {
let arg = match iterator.next() {
Some(arg) => arg,
None => break,
};
if arg == "-d" {
iterator.next();
continue;
};
let split = arg.split(":").collect::<Vec<_>>(); let split = arg.split(":").collect::<Vec<_>>();
if split.len() != 2 { if split.len() != 2 {
eprintln!("\x1B[33;1mwarning:\x1B[0m argument must be formatted like \"key:value\""); eprintln!("\x1B[33;1mwarning:\x1B[0m argument must be formatted like \"key:value\"");
continue; continue;
} }
info.push(format!("{style}{}:{unstyle} {}\n", split[0], split[1], style = style, unstyle = unstyle)); info.push(format!("{style}{}:{unstyle} {}\n", split[0], split[1], style = style, unstyle = unstyle));
} }
let mut logo_iterator = os.ascii_logo().lines(); let mut logo_iterator = os.ascii_logo().lines();