Compare commits

...

2 Commits

5 changed files with 59 additions and 57 deletions

@ -1,11 +1,10 @@
use clap::{builder::PossibleValue, Arg, ArgMatches, Command}; use clap::{builder::PossibleValue, Arg, Command};
use clap_complete::{generate, Shell}; use detee_cli::general::cli_handler::{
use detee_cli::general::cli_handler::handle_operators; handle_account, handle_completion, handle_operators, handle_packagers,
};
use detee_cli::sgx::cli_handler::handle_app; use detee_cli::sgx::cli_handler::handle_app;
use detee_cli::snp::cli_handler::{handle_vm, handle_vm_nodes}; use detee_cli::snp::cli_handler::{handle_vm, handle_vm_nodes};
use detee_cli::*; use detee_cli::*;
use std::error::Error;
use std::io;
const ABOUT: &str = r#"The DeTEE CLI allows you to manage and deploy applications and virtual machines. 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. All software runs within Trusted Execution Environments on a distributed network.
@ -573,54 +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())),
}
}
fn handle_packagers(
_matches: &ArgMatches,
) -> Result<Vec<crate::packagers::Packager>, Box<dyn Error>> {
Ok(crate::packagers::get_packagers())
}

@ -1,6 +1,11 @@
use super::operators; use super::operators;
use super::packagers;
use crate::{cli_print, config}; use crate::{cli_print, config};
use clap::ArgMatches; use clap::ArgMatches;
use clap::Command;
use clap_complete::{generate, Shell};
use std::error::Error;
use std::io;
pub fn handle_operators(matches: &ArgMatches) { pub fn handle_operators(matches: &ArgMatches) {
match matches.subcommand() { match matches.subcommand() {
@ -34,3 +39,52 @@ pub fn handle_operators(matches: &ArgMatches) {
_ => println!("To get more information about operators, use: detee-cli operator --help"), _ => 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(("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
}

@ -1,3 +1,4 @@
pub mod cli_handler; pub mod cli_handler;
pub mod operators; pub mod operators;
pub mod packagers;
// pub mod grpc; // pub mod grpc;

@ -2,7 +2,6 @@ pub mod config;
pub mod constants; pub mod constants;
pub mod general; pub mod general;
pub mod name_generator; pub mod name_generator;
pub mod packagers;
pub mod sgx; pub mod sgx;
pub mod snp; pub mod snp;
pub mod utils; pub mod utils;