diff options
-rw-r--r-- | Cargo.lock | 8 | ||||
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/git.rs | 2 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/user.rs | 7 | ||||
-rw-r--r-- | tests/cli.rs | 2 | ||||
-rw-r--r-- | user-info/Cargo.toml | 7 | ||||
-rw-r--r-- | user-info/src/lib.rs | 20 |
8 files changed, 40 insertions, 10 deletions
@@ -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" @@ -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] @@ -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, @@ -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); + } +} |