summaryrefslogtreecommitdiff
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
parent2b827a0ab06fb715290a0450a3fff56d3e6f4ee6 (diff)
Move user info gathering to its own workspace crate
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.toml3
-rw-r--r--src/git.rs2
-rw-r--r--src/lib.rs1
-rw-r--r--src/user.rs7
-rw-r--r--tests/cli.rs2
-rw-r--r--user-info/Cargo.toml7
-rw-r--r--user-info/src/lib.rs20
8 files changed, 40 insertions, 10 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 8d62299..b98bff4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -590,6 +590,7 @@ dependencies = [
"shlex",
"tempfile",
"thiserror",
+ "user-info",
]
[[package]]
@@ -735,6 +736,13 @@ dependencies = [
]
[[package]]
+name = "user-info"
+version = "0.1.0"
+dependencies = [
+ "nix 0.26.2",
+]
+
+[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index b273c83..1e98b29 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,6 +14,7 @@ rexpect = "0.5.0"
rustyline = { version = "11.0.0", default-features = false }
shlex = "1.1.0"
thiserror = "1.0.38"
+user-info = { path = "user-info" }
[dev-dependencies]
anyhow = "1.0.69"
@@ -21,3 +22,5 @@ assert_cmd = "2.0.8"
get-port = "4.0.0"
once_cell = "1.17.1"
tempfile = "3.4.0"
+
+[workspace]
diff --git a/src/git.rs b/src/git.rs
index 753f53a..24b376d 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -1,10 +1,10 @@
use crate::{
parser::{GitReceivePackArgs, GitUploadPackArgs},
- user::get_username,
ShackleError,
};
use git2::{Repository, RepositoryInitOptions};
use std::{path::PathBuf, process::Command};
+use user_info::get_username;
pub struct GitInitResult {
pub path: PathBuf,
diff --git a/src/lib.rs b/src/lib.rs
index b645918..99d8877 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,6 +1,5 @@
pub mod git;
pub mod parser;
-pub mod user;
use parser::*;
use rustyline::error::ReadlineError;
diff --git a/src/user.rs b/src/user.rs
deleted file mode 100644
index 72aea8d..0000000
--- a/src/user.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-pub fn get_username() -> Option<String> {
- let uid = nix::unistd::getuid();
- nix::unistd::User::from_uid(uid)
- .ok()
- .flatten()
- .map(|user| user.name)
-}
diff --git a/tests/cli.rs b/tests/cli.rs
index a370154..1daa850 100644
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@ -1,8 +1,8 @@
use anyhow::Result;
use assert_cmd::{cargo::cargo_bin, Command};
use rexpect::session::{spawn_command, PtySession};
-use shackle::user::get_username;
use tempfile::TempDir;
+use user_info::get_username;
struct TestContext {
p: PtySession,
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);
+ }
+}