feat(cli): add "current"
allow viewing of current git user todo: clean up the awful code in main
This commit is contained in:
@@ -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
@@ -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
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user