refactor: move account handling and completion functions to general cli handler
This commit is contained in:
		
							parent
							
								
									8936d19c39
								
							
						
					
					
						commit
						11ccdb54c7
					
				| @ -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::<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"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 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(("brain-url", path_subcommand)) => { | ||||
|             let url: String = path_subcommand.get_one::<String>("url").unwrap().clone(); | ||||
|             config::Config::set_brain_url(&url); | ||||
|         } | ||||
|         _ => cli_print(Ok(config::Config::get_account_data())), | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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<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(("brain-url", path_subcommand)) => { | ||||
|             let url: String = path_subcommand.get_one::<String>("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::<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"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 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 | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user