diff --git a/src/config.rs b/src/config.rs index dbc30d7..3a554d9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -452,3 +452,13 @@ impl Config { Ok(Self::path_dir()? + ("/app_signing_key.pem")) } } + +impl Config { + pub fn app_uuid_list_path() -> Result { + let dir = Self::home_dir() + ("/.detee/cli/apps"); + if !Path::new(&dir).exists() { + std::fs::create_dir_all(dir.clone())?; + } + Ok(dir + "/uuid_list") + } +} diff --git a/src/general/cli_handler.rs b/src/general/cli_handler.rs index 223f909..dfe1f46 100644 --- a/src/general/cli_handler.rs +++ b/src/general/cli_handler.rs @@ -73,6 +73,11 @@ pub fn handle_completion(matches: &ArgMatches, cmd: Command) { 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"); } } } diff --git a/src/sgx/cli_handler.rs b/src/sgx/cli_handler.rs index ce62845..babbf48 100644 --- a/src/sgx/cli_handler.rs +++ b/src/sgx/cli_handler.rs @@ -9,7 +9,8 @@ use crate::sgx::packaging::package_enclave; use crate::sgx::utils::{fetch_config_and_mr_enclave, override_envs_and_args_launch_config}; use crate::sgx::AppDeleteResponse; use crate::sgx::{ - get_app_node, get_app_node_by_contract, get_one_contract, inspect_node, print_nodes, + append_uuid_list, get_app_node, get_app_node_by_contract, get_one_contract, inspect_node, + print_nodes, write_uuid_list, }; use crate::sgx::{AppContract, AppDeployResponse}; use crate::utils::block_on; @@ -127,8 +128,14 @@ fn handle_deploy( override_envs_and_args_launch_config(&mut launch_config, envs, args); + let app_name = app_deploy_config.app_name.clone(); + match block_on(deploy_new_app_and_update_config(app_deploy_config, launch_config)) { - Ok(new_app_res) if new_app_res.error.is_empty() => Ok(new_app_res.into()), + Ok(new_app_res) if new_app_res.error.is_empty() => { + append_uuid_list(&new_app_res.uuid, &app_name)?; + Ok(new_app_res.into()) + } + Ok(new_app_res) => Err(Box::new(std::io::Error::other(new_app_res.error))), Err(e) => Err(Box::new(e)), } @@ -169,6 +176,8 @@ fn handle_list() -> Result, Box> { let contracts: Vec = block_on(list_contracts(req))?.into_iter().map(|n| n.into()).collect(); + write_uuid_list(&contracts)?; + Ok(contracts) } diff --git a/src/sgx/mod.rs b/src/sgx/mod.rs index 22dbd3d..72327a8 100644 --- a/src/sgx/mod.rs +++ b/src/sgx/mod.rs @@ -27,6 +27,8 @@ pub enum Error { AppContractNotFound(String), #[error("Brain returned the following error: {0}")] Brain(#[from] grpc_brain::Error), + #[error("Could not read file from disk: {0}")] + FileNotFound(#[from] std::io::Error), } #[derive(Tabled, Debug, Serialize, Deserialize)] @@ -243,3 +245,24 @@ pub async fn get_app_node_by_contract(uuid: &str) -> Result Result<(), Error> { + let app_uuid_list_path = Config::app_uuid_list_path()?; + let mut file = std::fs::File::create(app_uuid_list_path)?; + let output: String = app_contracts + .iter() + .map(|app| format!("{}\t{}", app.uuid, app.name).to_string()) + .collect::>() + .join("\n"); + let output = output + "\n"; + std::io::Write::write_all(&mut file, output.as_bytes())?; + Ok(()) +} + +pub fn append_uuid_list(uuid: &str, app_name: &str) -> Result<(), Error> { + use std::{fs::OpenOptions, io::prelude::*}; + let mut file = + OpenOptions::new().create(true).append(true).open(Config::app_uuid_list_path()?).unwrap(); + writeln!(file, "{uuid}\t{app_name}")?; + Ok(()) +} diff --git a/src/snp/grpc.rs b/src/snp/grpc.rs index 06f9ac0..55b7484 100644 --- a/src/snp/grpc.rs +++ b/src/snp/grpc.rs @@ -219,10 +219,3 @@ pub async fn get_contract_by_uuid(uuid: &str) -> Result { } Ok(contracts[0].clone()) } - -// pub fn block_on(future: F) -> F::Output -// where -// F: std::future::Future, -// { -// tokio::runtime::Runtime::new().unwrap().block_on(future) -// } diff --git a/src/snp/mod.rs b/src/snp/mod.rs index a68fa7c..483772f 100644 --- a/src/snp/mod.rs +++ b/src/snp/mod.rs @@ -341,7 +341,8 @@ fn write_uuid_list(contracts: &[VmContract]) -> Result<(), Error> { pub fn append_uuid_list(uuid: &str, hostname: &str) -> Result<(), Error> { use std::{fs::OpenOptions, io::prelude::*}; - let mut file = OpenOptions::new().append(true).open(Config::vm_uuid_list_path()?)?; + let mut file = + OpenOptions::new().create(true).append(true).open(Config::vm_uuid_list_path()?)?; writeln!(file, "{uuid}\t{hostname}")?; Ok(()) }