inital commit
authorLexie Malina <tech@colfax.ave.t-ch.net>
Thu, 3 Aug 2023 10:22:37 +0000 (10:22 +0000)
committerLexie Malina <tech@colfax.ave.t-ch.net>
Thu, 3 Aug 2023 10:22:37 +0000 (10:22 +0000)
.gitignore [new file with mode: 0644]
Cargo.lock [new file with mode: 0644]
Cargo.toml [new file with mode: 0644]
git-daemon-export-ok [new file with mode: 0644]
rust-analyzer.core [new file with mode: 0644]
src/main.rs [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..ea8c4bf
--- /dev/null
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644 (file)
index 0000000..bd0baf7
--- /dev/null
@@ -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 (file)
index 0000000..2c984e3
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
diff --git a/rust-analyzer.core b/rust-analyzer.core
new file mode 100644 (file)
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 (file)
index 0000000..37a1e87
--- /dev/null
@@ -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<String> = 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::<Vec<&str>>() {
+            u if u.len() < 7 => false,
+            u if u[2].parse::<u32>().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
+}