From 11ccdb54c7b75ac25e7ee27fe276b013ba9e7be2 Mon Sep 17 00:00:00 2001 From: Noor Date: Thu, 20 Mar 2025 22:02:05 +0530 Subject: [PATCH] refactor: move account handling and completion functions to general cli handler --- src/bin/detee-cli.rs | 53 +++----------------------------------- src/general/cli_handler.rs | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 49 deletions(-) diff --git a/src/bin/detee-cli.rs b/src/bin/detee-cli.rs index d5d41c2..0552269 100644 --- a/src/bin/detee-cli.rs +++ b/src/bin/detee-cli.rs @@ -1,10 +1,10 @@ -use clap::{builder::PossibleValue, Arg, ArgMatches, Command}; -use clap_complete::{generate, Shell}; -use detee_cli::general::cli_handler::{handle_operators, handle_packagers}; +use clap::{builder::PossibleValue, Arg, Command}; +use detee_cli::general::cli_handler::{ + handle_account, handle_completion, handle_operators, handle_packagers, +}; use detee_cli::sgx::cli_handler::handle_app; use detee_cli::snp::cli_handler::{handle_vm, handle_vm_nodes}; use detee_cli::*; -use std::io; const ABOUT: &str = r#"The DeTEE CLI allows you to manage and deploy applications and virtual machines. All software runs within Trusted Execution Environments on a distributed network. @@ -572,48 +572,3 @@ fn main() { } } } - -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 -} - -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!("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"); - } - } -} - -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(("brain-url", path_subcommand)) => { - let url: String = path_subcommand.get_one::("url").unwrap().clone(); - config::Config::set_brain_url(&url); - } - _ => cli_print(Ok(config::Config::get_account_data())), - } -} diff --git a/src/general/cli_handler.rs b/src/general/cli_handler.rs index 79decbf..223f909 100644 --- a/src/general/cli_handler.rs +++ b/src/general/cli_handler.rs @@ -2,7 +2,10 @@ 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() { @@ -40,3 +43,48 @@ pub fn handle_operators(matches: &ArgMatches) { 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(("brain-url", path_subcommand)) => { + let url: String = path_subcommand.get_one::("url").unwrap().clone(); + config::Config::set_brain_url(&url); + } + _ => 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::("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"); + } + } +} + +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 +}