From 95347aebb6309fc9f952134010c53b1742b7b855 Mon Sep 17 00:00:00 2001 From: ozzy Date: Tue, 1 Sep 2026 17:43:19 -0500 Subject: [PATCH] feat(cli): add "current" allow viewing of current git user todo: clean up the awful code in main --- src/cli.rs | 7 +++++++ src/main.rs | 37 ++++++++++++++++++++++++++++++++++--- src/utils.rs | 14 ++++++++++---- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 27477a9..b451f7a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -13,6 +13,7 @@ pub enum Commands { Use(UseArgs), Add(AddArgs), Remove(RemoveArgs), + Current(CurrentArgs), } #[derive(Debug, Args)] @@ -35,3 +36,9 @@ pub struct AddArgs { pub struct RemoveArgs { pub profile_name: String, } + +#[derive(Debug, Args)] +pub struct CurrentArgs { + #[arg(short, long)] + pub global: bool, +} diff --git a/src/main.rs b/src/main.rs index 6fcee4d..9c2314e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,9 +35,17 @@ fn main() { } let profile = profile.unwrap(); - let global = if args.global { "--global" } else { "" }; - utils::run_command("git", &["config", "user.name", &profile.username, global]); - utils::run_command("git", &["config", "user.email", &profile.email, global]); + + utils::run_command( + "git", + &add_global_if(&["config", "user.name", &profile.username], args.global), + ) + .expect("command failed"); + utils::run_command( + "git", + &add_global_if(&["config", "user.email", &profile.email], args.global), + ) + .expect("command failed"); } Commands::Add(args) => { let mut profiles = config::get_profile_list().expect("unable to get list of profiles"); @@ -64,5 +72,28 @@ fn main() { config::write_profile_list(profiles).expect("unable to write profile file"); println!("profile removed") } + Commands::Current(args) => { + let mut name = + utils::run_command("git", &add_global_if(&["config", "user.name"], args.global)) + .expect("command failed"); + let mut email = utils::run_command( + "git", + &add_global_if(&["config", "user.email"], args.global), + ) + .expect("command failed"); + + name.pop(); + email.pop(); + + println!("current user: {} ({})", name, email); + } } } + +fn add_global_if<'a>(args: &[&'a str], global: bool) -> Vec<&'a str> { + let mut result = args.to_vec(); + if global { + result.insert(1, "--global"); + } + result +} diff --git a/src/utils.rs b/src/utils.rs index 1c5f392..2742826 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,16 @@ use std::process::Command; -pub fn run_command(command: &str, args: &[&str]) { - let mut child = Command::new(command) +pub fn run_command(command: &str, args: &[&str]) -> Result { + let child = Command::new(command) .args(args) - .spawn() + .output() .expect("failed to execute process"); - let _ = child.wait().expect("failed to wait on child"); + if child.status.success() { + let stdout = String::from_utf8(child.stdout).unwrap(); + Ok(stdout.to_owned()) + } else { + let stderr = String::from_utf8(child.stderr).unwrap(); + Err(stderr) + } }