From a6d98b37feff250144623a12fe9f994cf8a12059 Mon Sep 17 00:00:00 2001 From: aaravm Date: Thu, 25 Jul 2024 17:30:55 +0530 Subject: [PATCH] added config --- cli/src/main.rs | 83 ++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index be6a730..2050612 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -9,13 +9,10 @@ use ga4gh_sdk::tes::models::TesTask; use ga4gh_sdk::test_utils::ensure_funnel_running; use std::fs; use std::path::Path; -// use std::os::linux::raw::stat; -// use ga4gh_sdk::configuration::BasicAuth; -// use std::fs::File; -// use serde_json::Value; -// use std::io::Read; -// use std::io::Write; -// use tempfile::tempdir; +use ga4gh_sdk::configuration::BasicAuth; +use std::fs::File; +use serde_json::Value; +use std::io::Read; /// # Examples @@ -228,10 +225,10 @@ async fn run_cli(cmd: Command<'_>) -> Result<(), Box> { }; } if let Some(("cancel", sub)) = sub.subcommand() { - let mut config = Configuration::default(); + // let mut config = Configuration::default(); let id = sub.value_of("id").unwrap().to_string(); - // let mut config = load_configuration(); + let mut config = load_configuration(); let funnel_url = ensure_funnel_running().await; config.set_base_path(&funnel_url); let transport = Transport::new(&config); @@ -252,42 +249,42 @@ async fn run_cli(cmd: Command<'_>) -> Result<(), Box> { } Ok(()) } -// fn read_configuration_from_file(file_path: &str) -> Result> { -// let mut file = File::open(file_path)?; -// let mut contents = String::new(); -// file.read_to_string(&mut contents)?; +fn read_configuration_from_file(file_path: &str) -> Result> { + let mut file = File::open(file_path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; -// let json_value: Value = serde_json::from_str(&contents)?; + let json_value: Value = serde_json::from_str(&contents)?; -// let base_path = json_value["base_path"].as_str().unwrap_or_default().to_string(); -// let user_agent = json_value["user_agent"].as_str().map(|s| s.to_string()); -// let basic_auth = json_value["basic_auth"].as_object().map(|auth| BasicAuth { -// username: auth["username"].as_str().unwrap_or_default().to_string(), -// password: Some(auth["password"].as_str().unwrap_or_default().to_string()), -// }); -// let oauth_access_token = json_value["oauth_access_token"].as_str().map(|s| s.to_string()); + let base_path = json_value["base_path"].as_str().unwrap_or_default().to_string(); + let user_agent = json_value["user_agent"].as_str().map(|s| s.to_string()); + let basic_auth = json_value["basic_auth"].as_object().map(|auth| BasicAuth { + username: auth["username"].as_str().unwrap_or_default().to_string(), + password: Some(auth["password"].as_str().unwrap_or_default().to_string()), + }); + let oauth_access_token = json_value["oauth_access_token"].as_str().map(|s| s.to_string()); -// let config = Configuration::new(base_path, user_agent, basic_auth, oauth_access_token); -// Ok(config) -// } + let config = Configuration::new(base_path, user_agent, basic_auth, oauth_access_token); + Ok(config) +} -// fn load_configuration() -> Configuration { -// let config_file_path = dirs::home_dir().map(|path| path.join(".config")); -// if let Some(path) = config_file_path { -// if path.exists() { -// if let Some(path_str) = path.to_str() { -// match read_configuration_from_file(path_str) { -// Ok(config) => {config}, -// Err(_) => {Configuration::default()}, -// } -// } else { -// Configuration::default() -// } +fn load_configuration() -> Configuration { + let config_file_path = dirs::home_dir().map(|path| path.join(".config")); + if let Some(path) = config_file_path { + if path.exists() { + if let Some(path_str) = path.to_str() { + match read_configuration_from_file(path_str) { + Ok(config) => {config}, + Err(_) => {Configuration::default()}, + } + } else { + Configuration::default() + } -// } else { -// Configuration::default() -// } -// } else { -// Configuration::default() -// } -// } + } else { + Configuration::default() + } + } else { + Configuration::default() + } +}