detee-cli/src/general/cli_handler.rs

96 lines
4.5 KiB
Rust

use super::operators;
use super::packagers;
use crate::{cli_print, config};
use clap::ArgMatches;
use clap::Command;
use clap_complete::{generate, Shell};
use std::error::Error;
use std::io;
pub fn handle_operators(matches: &ArgMatches) {
match matches.subcommand() {
Some(("list", _)) => {
cli_print(operators::print_operators().map_err(Into::into));
}
Some(("register", subcom_args)) => {
let escrow: u64 = *subcom_args.get_one::<u64>("escrow").unwrap();
let email: String = subcom_args.get_one::<String>("email").unwrap().clone();
cli_print(operators::register(escrow, email).map_err(Into::into));
}
Some(("inspect", inspect_args)) => {
let wallet = match inspect_args.get_one::<String>("wallet") {
Some(wallet) => wallet.to_string(),
None => config::Config::get_detee_wallet().unwrap_or("".to_string()),
};
cli_print(operators::inspect_operator(wallet).map_err(Into::into));
}
Some(("kick", subcom_args)) => {
let uuid: String = subcom_args.get_one::<String>("contract").unwrap().clone();
let reason: String = subcom_args.get_one::<String>("reason").unwrap().clone();
cli_print(operators::kick(uuid, reason).map_err(Into::into));
}
Some(("ban-user", subcom_args)) => {
let user_wallet: String = subcom_args.get_one::<String>("wallet").unwrap().clone();
cli_print(operators::ban(user_wallet).map_err(Into::into));
}
Some(("decom", _)) => {
todo!("Currently decomissioning is not supported. Will be ");
}
_ => println!("To get more information about operators, use: detee-cli operator --help"),
}
}
pub fn handle_packagers(_matches: &ArgMatches) -> Result<Vec<packagers::Packager>, Box<dyn Error>> {
Ok(packagers::get_packagers())
}
pub fn handle_account(matches: &ArgMatches) {
match matches.subcommand() {
Some(("show", _)) => cli_print(Ok(config::Config::get_account_data())),
Some(("sign", path_subcommand)) => {
let path: String = path_subcommand.get_one::<String>("path").unwrap().clone();
config::Config::init_config().sign_file(&path);
}
Some(("ssh-pubkey-path", path_subcommand)) => {
let path: String = path_subcommand.get_one::<String>("path").unwrap().clone();
config::Config::set_ssh_pubkey_path(&path);
}
Some(("network", path_subcommand)) => {
let name: String = path_subcommand.get_one::<String>("name").unwrap().clone();
config::Config::set_network(&name);
}
_ => cli_print(Ok(config::Config::get_account_data())),
}
}
pub fn handle_completion(matches: &ArgMatches, cmd: Command) {
let mut cmd = disable_help_for_all_subcommands(cmd);
if let Some(shell) = matches.get_one::<String>("shell") {
let shell: Shell = shell.parse().expect("Invalid shell type");
generate(shell, &mut cmd, "detee-cli", &mut io::stdout());
if shell.to_string() == "fish" {
println!("complete -c detee-cli -n '__fish_seen_subcommand_from vm delete' -a '(cat ~/.detee/cli/vms/uuid_list)' -f");
println!("complete -c detee-cli -n '__fish_seen_subcommand_from vm update' -a '(cat ~/.detee/cli/vms/uuid_list)' -f");
println!("complete -c detee-cli -n '__fish_seen_subcommand_from vm inspect' -a '(cat ~/.detee/cli/vms/uuid_list)' -f");
println!("complete -c detee-cli -n '__fish_seen_subcommand_from vm ssh' -a '(cat ~/.detee/cli/vms/uuid_list)' -f");
println!("complete -c detee-cli -n '__fish_seen_subcommand_from app config update' -a '(cat ~/.detee/cli/apps/uuid_list)' -f");
println!("complete -c detee-cli -n '__fish_seen_subcommand_from app config get' -a '(cat ~/.detee/cli/apps/uuid_list)' -f");
println!("complete -c detee-cli -n '__fish_seen_subcommand_from app delete' -a '(cat ~/.detee/cli/apps/uuid_list)' -f");
println!("complete -c detee-cli -n '__fish_seen_subcommand_from app inspect' -a '(cat ~/.detee/cli/apps/uuid_list)' -f");
}
}
}
fn disable_help_for_all_subcommands(mut cmd: Command) -> Command {
cmd = cmd.disable_help_subcommand(true);
let subcommands: Vec<_> = cmd
.get_subcommands_mut()
.map(|sub| disable_help_for_all_subcommands(sub.clone()))
.collect();
for sub in subcommands {
cmd = cmd.subcommand(sub);
}
cmd
}