added functionality to load environment variables

This commit is contained in:
Wyatt J. Miller 2022-07-17 20:22:53 -04:00
parent 4951770a9d
commit a8c70a2869
2 changed files with 41 additions and 4 deletions

View File

@ -1,4 +1,5 @@
use std::env;
use std::ops::Deref;
use config::File;
use serde::Deserialize;
@ -22,7 +23,7 @@ impl Configuration {
// this case is currently untested
"windows" => {
location.push(String::from("config.json"));
location.push(String::from("{:?}/AppData/Roaming/gt/config.json"))
location.push(format!("{:?}/AppData/Roaming/gt/config.json", home_dir_env))
},
// this case is currently untested
"macos" => {
@ -49,6 +50,41 @@ impl Configuration {
config
}
pub fn load_envs(&mut self) {
// get environment variables
let username_env = env::var("GT_USERNAME").unwrap_or_else(|_| "".to_string());
let password_env = env::var("GT_PASSWORD").unwrap_or_else(|_| "".to_string());
let api_token_env = env::var("GT_API_TOKEN").unwrap_or_else(|_| "".to_string());
// get struct fields
let mut user_config = self.username.as_ref().unwrap();
let mut password_config = self.password.as_ref().unwrap();
let mut api_token_config = self.api_token.as_ref().unwrap();
// check and see if the env vars are empty
// if they are not, put the env vars in place of the config property
if username_env != "".to_string() {
self.username = Some(username_env);
} else {
println!("cannot find username env var");
}
if password_env != "".to_string() {
password_config = &password_env.deref().to_string();
} else {
println!("cannot find password env var");
}
if api_token_env != "".to_string() {
api_token_config = &api_token_env.deref().to_string();
} else {
println!("cannot find api token env var");
}
println!("{:?}", &self);
}
}
impl Default for Configuration {

View File

@ -16,7 +16,8 @@ use clap::ArgMatches;
fn main() {
let matches: ArgMatches = arg::get_args();
let config = crate::config::Configuration::new();
let mut config = crate::config::Configuration::new();
config.load_envs();
let auth = request::Authentication::new(&config);
let request = auth.request_chooser(config.clone(), matches);
@ -47,11 +48,11 @@ fn main() {
}
if repo_matches.is_present("push") {
repo.push_to_remote(&request)
repo.push_to_remote(&request, &config)
}
if repo_matches.is_present("pull") {
repo.pull_from_remote(&request)
repo.pull_from_remote(&request, &config)
}
if repo_matches.is_present("clone") {