Skip to content

Commit

Permalink
added config
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravm committed Jul 25, 2024
1 parent 6be3e10 commit a6d98b3
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -228,10 +225,10 @@ async fn run_cli(cmd: Command<'_>) -> Result<(), Box<dyn Error>> {
};
}
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);
Expand All @@ -252,42 +249,42 @@ async fn run_cli(cmd: Command<'_>) -> Result<(), Box<dyn Error>> {
}
Ok(())
}
// fn read_configuration_from_file(file_path: &str) -> Result<Configuration, Box<dyn Error>> {
// 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<Configuration, Box<dyn Error>> {
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()
}
}

0 comments on commit a6d98b3

Please sign in to comment.