feat: app UUID cli completion

This commit is contained in:
Noor 2025-03-24 16:42:40 +05:30
parent 9b3bdba70d
commit 7d7200bca4
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
6 changed files with 51 additions and 10 deletions

@ -452,3 +452,13 @@ impl Config {
Ok(Self::path_dir()? + ("/app_signing_key.pem")) Ok(Self::path_dir()? + ("/app_signing_key.pem"))
} }
} }
impl Config {
pub fn app_uuid_list_path() -> Result<String, Error> {
let dir = Self::home_dir() + ("/.detee/cli/apps");
if !Path::new(&dir).exists() {
std::fs::create_dir_all(dir.clone())?;
}
Ok(dir + "/uuid_list")
}
}

@ -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 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 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 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");
} }
} }
} }

@ -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::utils::{fetch_config_and_mr_enclave, override_envs_and_args_launch_config};
use crate::sgx::AppDeleteResponse; use crate::sgx::AppDeleteResponse;
use crate::sgx::{ 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::sgx::{AppContract, AppDeployResponse};
use crate::utils::block_on; use crate::utils::block_on;
@ -127,8 +128,14 @@ fn handle_deploy(
override_envs_and_args_launch_config(&mut launch_config, envs, args); 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)) { 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))), Ok(new_app_res) => Err(Box::new(std::io::Error::other(new_app_res.error))),
Err(e) => Err(Box::new(e)), Err(e) => Err(Box::new(e)),
} }
@ -169,6 +176,8 @@ fn handle_list() -> Result<Vec<AppContract>, Box<dyn std::error::Error>> {
let contracts: Vec<AppContract> = let contracts: Vec<AppContract> =
block_on(list_contracts(req))?.into_iter().map(|n| n.into()).collect(); block_on(list_contracts(req))?.into_iter().map(|n| n.into()).collect();
write_uuid_list(&contracts)?;
Ok(contracts) Ok(contracts)
} }

@ -27,6 +27,8 @@ pub enum Error {
AppContractNotFound(String), AppContractNotFound(String),
#[error("Brain returned the following error: {0}")] #[error("Brain returned the following error: {0}")]
Brain(#[from] grpc_brain::Error), Brain(#[from] grpc_brain::Error),
#[error("Could not read file from disk: {0}")]
FileNotFound(#[from] std::io::Error),
} }
#[derive(Tabled, Debug, Serialize, Deserialize)] #[derive(Tabled, Debug, Serialize, Deserialize)]
@ -243,3 +245,24 @@ pub async fn get_app_node_by_contract(uuid: &str) -> Result<AppNodeListResp, Err
Ok(get_one_app_node(AppNodeFilters { node_pubkey: contract.node_pubkey, ..Default::default() }) Ok(get_one_app_node(AppNodeFilters { node_pubkey: contract.node_pubkey, ..Default::default() })
.await?) .await?)
} }
fn write_uuid_list(app_contracts: &[AppContract]) -> 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::<Vec<String>>()
.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(())
}

@ -219,10 +219,3 @@ pub async fn get_contract_by_uuid(uuid: &str) -> Result<VmContract, Error> {
} }
Ok(contracts[0].clone()) Ok(contracts[0].clone())
} }
// pub fn block_on<F>(future: F) -> F::Output
// where
// F: std::future::Future,
// {
// tokio::runtime::Runtime::new().unwrap().block_on(future)
// }

@ -341,7 +341,8 @@ fn write_uuid_list(contracts: &[VmContract]) -> Result<(), Error> {
pub fn append_uuid_list(uuid: &str, hostname: &str) -> Result<(), Error> { pub fn append_uuid_list(uuid: &str, hostname: &str) -> Result<(), Error> {
use std::{fs::OpenOptions, io::prelude::*}; 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}")?; writeln!(file, "{uuid}\t{hostname}")?;
Ok(()) Ok(())
} }