95347aebb6
allow viewing of current git user todo: clean up the awful code in main
100 lines
3.3 KiB
Rust
100 lines
3.3 KiB
Rust
use clap::Parser;
|
|
|
|
use crate::cli::Commands;
|
|
use crate::config::Profile;
|
|
|
|
mod cli;
|
|
mod config;
|
|
mod utils;
|
|
|
|
fn main() {
|
|
let args = cli::Cli::parse();
|
|
|
|
match args.command {
|
|
Commands::List => {
|
|
let profiles = config::get_profile_list().expect("unable to get list of profiles");
|
|
if profiles.is_empty() {
|
|
println!("no profiles found");
|
|
return;
|
|
}
|
|
|
|
println!("{:<15} {:<20} {:<25}", "profile", "name", "email");
|
|
for profile in profiles {
|
|
println!(
|
|
"{:<15} {:<20} {:<25}",
|
|
profile.name, profile.username, profile.email
|
|
);
|
|
}
|
|
}
|
|
Commands::Use(args) => {
|
|
let profiles = config::get_profile_list().expect("unable to get list of profiles");
|
|
let profile = profiles.iter().find(|p| p.name == args.profile_name);
|
|
if profile.is_none() {
|
|
println!("profile not found");
|
|
return;
|
|
}
|
|
|
|
let profile = profile.unwrap();
|
|
|
|
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");
|
|
let existing_profile = profiles.iter().position(|p| p.name == args.profile_name);
|
|
if existing_profile.is_some() {
|
|
println!("profile already exists");
|
|
return;
|
|
}
|
|
profiles.push(Profile {
|
|
name: args.profile_name,
|
|
username: args.name,
|
|
email: args.email,
|
|
});
|
|
config::write_profile_list(profiles).expect("unable to write profile file");
|
|
println!("profile added")
|
|
}
|
|
Commands::Remove(args) => {
|
|
let mut profiles = config::get_profile_list().expect("unable to get list of profiles");
|
|
let index = profiles
|
|
.iter()
|
|
.position(|p| p.name == args.profile_name)
|
|
.expect("unable to find profile");
|
|
profiles.remove(index);
|
|
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
|
|
}
|