feat(cli): add remove
added "remove" and added check to prevent duplicate profiles
This commit is contained in:
@@ -12,6 +12,7 @@ pub enum Commands {
|
|||||||
List,
|
List,
|
||||||
Use(UseArgs),
|
Use(UseArgs),
|
||||||
Add(AddArgs),
|
Add(AddArgs),
|
||||||
|
Remove(RemoveArgs),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Args)]
|
#[derive(Debug, Args)]
|
||||||
@@ -29,3 +30,8 @@ pub struct AddArgs {
|
|||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub email: String,
|
pub email: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Args)]
|
||||||
|
pub struct RemoveArgs {
|
||||||
|
pub profile_name: String,
|
||||||
|
}
|
||||||
|
|||||||
+15
@@ -41,6 +41,11 @@ fn main() {
|
|||||||
}
|
}
|
||||||
Commands::Add(args) => {
|
Commands::Add(args) => {
|
||||||
let mut profiles = config::get_profile_list().expect("unable to get list of profiles");
|
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 {
|
profiles.push(Profile {
|
||||||
name: args.profile_name,
|
name: args.profile_name,
|
||||||
username: args.name,
|
username: args.name,
|
||||||
@@ -49,5 +54,15 @@ fn main() {
|
|||||||
config::write_profile_list(profiles).expect("unable to write profile file");
|
config::write_profile_list(profiles).expect("unable to write profile file");
|
||||||
println!("profile added")
|
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")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user