// SPDX-License-Identifier: Apache-2.0 use super::{operators, packagers}; use crate::{cli_print, config}; use clap::{ArgMatches, 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::("escrow").unwrap(); let email: String = subcom_args.get_one::("email").unwrap().clone(); cli_print(operators::register(escrow, email).map_err(Into::into)); } Some(("inspect", inspect_args)) => { let wallet = match inspect_args.get_one::("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 app_id: String = subcom_args.get_one::("contract").unwrap().clone(); let reason: String = subcom_args.get_one::("reason").unwrap().clone(); cli_print(operators::kick(app_id, reason).map_err(Into::into)); } Some(("ban-user", subcom_args)) => { let user_wallet: String = subcom_args.get_one::("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, Box> { 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::("path").unwrap().clone(); config::Config::init_config().sign_file(&path); } Some(("ssh-pubkey-path", path_subcommand)) => { let path: String = path_subcommand.get_one::("path").unwrap().clone(); config::Config::set_ssh_pubkey_path(&path); } Some(("network", path_subcommand)) => { let name: String = path_subcommand.get_one::("name").unwrap().clone(); config::Config::set_network(&name); } _ => cli_print(Ok(config::Config::get_account_data())), } } const FISH_COMPLETION: &str = r#" complete -c detee-cli -n '__fish_detee_cli_using_subcommand vm; and __fish_seen_subcommand_from delete' -a '(cat ~/.detee/cli/vms/vm_id_list)' -f complete -c detee-cli -n '__fish_detee_cli_using_subcommand vm; and __fish_seen_subcommand_from update' -a '(cat ~/.detee/cli/vms/vm_id_list)' -f complete -c detee-cli -n '__fish_detee_cli_using_subcommand vm; and __fish_seen_subcommand_from inspect' -a '(cat ~/.detee/cli/vms/vm_id_list)' -f complete -c detee-cli -n '__fish_detee_cli_using_subcommand vm; and __fish_seen_subcommand_from ssh' -a '(cat ~/.detee/cli/vms/vm_id_list)' -f complete -c detee-cli -n '__fish_detee_cli_using_subcommand app; and __fish_seen_subcommand_from update' -a '(cat ~/.detee/cli/apps/app_id_list)' -f complete -c detee-cli -n '__fish_detee_cli_using_subcommand app; and __fish_seen_subcommand_from get' -a '(cat ~/.detee/cli/apps/app_id_list)' -f complete -c detee-cli -n '__fish_detee_cli_using_subcommand app; and __fish_seen_subcommand_from delete' -a '(cat ~/.detee/cli/apps/app_id_list)' -f complete -c detee-cli -n '__fish_detee_cli_using_subcommand app; and __fish_seen_subcommand_from inspect' -a '(cat ~/.detee/cli/apps/app_id_list)' -f "#; pub fn handle_completion(matches: &ArgMatches, cmd: Command) { let mut cmd = disable_help_for_all_subcommands(cmd); if let Some(shell) = matches.get_one::("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!("{FISH_COMPLETION}"); } } } 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 }