feat(cli): add "current"

allow viewing of current git user
todo: clean up the awful code in main
This commit is contained in:
2026-09-01 17:43:19 -05:00
parent 7ed681532c
commit 95347aebb6
3 changed files with 51 additions and 7 deletions
+7
View File
@@ -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,
}
+34 -3
View File
@@ -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
}
+10 -4
View File
@@ -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<String, String> {
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)
}
}