From: Lexie Malina Date: Thu, 3 Aug 2023 10:22:37 +0000 (+0000) Subject: inital commit X-Git-Url: https://git.t-ch.net/?a=commitdiff_plain;h=85ead2ffbfa772ddab4fee0933066d255caa1354;p=colfax-finger.git inital commit --- 85ead2ffbfa772ddab4fee0933066d255caa1354 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bd0baf7 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "colfax-finger" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2c984e3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "colfax-finger" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/git-daemon-export-ok b/git-daemon-export-ok new file mode 100644 index 0000000..e69de29 diff --git a/rust-analyzer.core b/rust-analyzer.core new file mode 100644 index 0000000..da1aa33 Binary files /dev/null and b/rust-analyzer.core differ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..37a1e87 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,110 @@ +// #![feature(vec_push_within_capacity)] +use std::env; +use std::fs; + +fn main() { + // Get args from command line + let args: Vec = env::args().collect(); + dbg!(&args); + + // Ensure user is supplied with command + if args.len() < 2 { + println!(""); + std::process::exit(1); + } + + // read passwd file and make list of users + let passwd = match fs::read_to_string("/etc/passwd") { + Ok(file) => file, + Err(_) => { + eprintln!("Unable to read passwd file."); + std::process::exit(1); + } + }; + + let users: Vec<&str> = passwd.split('\n').collect(); + + dbg!(&users); + + // finds a matching line for a user + let user = users.iter().find(|l| -> bool { + match l.split(':').collect::>() { + u if u.len() < 7 => false, + u if u[2].parse::().unwrap() < 1000 => false, + u if u[0] != &args[1] => false, + _ => true, + } + }); + + dbg!(&user); + + match user { + Some(u) => { + format_user(u); + } + None => println!("User not found"), + } +} + +fn format_user(user: &str) -> () { + let u: Vec<&str> = user.split(':').collect(); + let mut gecos: Vec<&str> = Vec::with_capacity(5); + for g in u[4].split(',') { + gecos.push(g); + } + + for _ in gecos.len()..5 { + gecos.push(""); + } + + // read passwd file and make list of users + let plan = match fs::read_to_string(format!("{}/.plan", u[5])) { + Ok(file) => file, + Err(_) => { + format!("{} has no plan", u[5]) + } + }; + + dbg!(&gecos); + + { + let mut line = format!("User: {}", u[0]); + line = fill_till_len(line.as_str(), 40); + + if !gecos[0].is_empty() { + line.push_str(format!("Name: {}", gecos[0]).as_str()); + } + + println!("{}", line); + } + + { + println!("Homepage: https://t-ch.net/~/{}", u[0]); + } + + { + let mut line = String::new(); + if !gecos[1].is_empty() { + line.push_str(format!("Office Location: {}", gecos[1]).as_str()); + line = fill_till_len(line.as_str(), 40); + } + + if !gecos[2].is_empty() { + line.push_str(format!("#: {}", gecos[2]).as_str()); + } + + if !line.is_empty() { + println!("{}", line); + } + + println!("{}", plan) + } +} + +fn fill_till_len(s: &str, len: usize) -> String { + let mut line = String::from(s); + for _ in line.len()..len { + line.push(' '); + } + line +}