summaryrefslogtreecommitdiff
path: root/user-info
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2023-03-20 23:34:50 +0200
committerJustin Wernick <justin@worthe-it.co.za>2023-03-20 23:34:50 +0200
commita62fa84b0bb5abe3a2c52a047f47b6403ffcd54c (patch)
tree7efd7e60d5d046b1239c91beb3e9d9cc45e9839e /user-info
parent2b827a0ab06fb715290a0450a3fff56d3e6f4ee6 (diff)
Move user info gathering to its own workspace crate
Diffstat (limited to 'user-info')
-rw-r--r--user-info/Cargo.toml7
-rw-r--r--user-info/src/lib.rs20
2 files changed, 27 insertions, 0 deletions
diff --git a/user-info/Cargo.toml b/user-info/Cargo.toml
new file mode 100644
index 0000000..63a3f7c
--- /dev/null
+++ b/user-info/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "user-info"
+version = "0.1.0"
+edition = "2021"
+
+[dependencies]
+nix = { version = "0.26.2", default-features = false, features = ["user"] }
diff --git a/user-info/src/lib.rs b/user-info/src/lib.rs
new file mode 100644
index 0000000..bd5ff42
--- /dev/null
+++ b/user-info/src/lib.rs
@@ -0,0 +1,20 @@
+use nix::unistd::{getuid, User};
+
+pub fn get_username() -> Option<String> {
+ let uid = getuid();
+ User::from_uid(uid).ok().flatten().map(|user| user.name)
+}
+
+#[cfg(test)]
+mod test {
+ use super::*;
+
+ #[test]
+ fn it_returns_a_username() {
+ // We can't get too specific here because this is your actual username,
+ // but we at lease expect it to be some string, not None.
+ let username = get_username();
+ let username_len = username.unwrap().trim().len();
+ assert!(username_len > 0);
+ }
+}