feat: generate config, load shell env vars

This commit is contained in:
2023-01-29 19:19:50 -05:00
parent 58b4b00311
commit 7c5c0c13fb
8 changed files with 275 additions and 56 deletions
+72 -27
View File
@@ -1,10 +1,13 @@
use std::env;
use std::ops::Deref;
use std::process;
use config::File;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize)]
use crate::generate;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Configuration {
pub api_token: Option<String>,
pub base_api: String,
@@ -15,6 +18,16 @@ pub struct Configuration {
impl Configuration {
pub fn new() -> Configuration {
Configuration {
api_token: None,
base_api: String::new(),
base_url: String::new(),
username: None,
password: None,
}
}
pub fn load_config_file(&mut self) {
let home_dir_env = env::var("HOME").unwrap();
let mut settings = config::Config::default();
let mut location: Vec<String> = Vec::new();
@@ -24,18 +37,21 @@ impl Configuration {
"windows" => {
location.push(String::from("config.json"));
location.push(format!("{:?}/AppData/Roaming/gt/config.json", home_dir_env))
},
}
// this case is currently untested
"macos" => {
location.push(String::from("config.json"));
},
}
"linux" => {
location.push(String::from("config.json"));
location.push(String::from("/etc/gt/config.json"));
location.push(format!("{:?}/.config/gt/config.json", home_dir_env));
},
location.push(format!("{:?}/.config/gt/config.json", home_dir_env));
}
_ => {
println!("Unsupported operating system! {:?} might cause some instabilities!", env::consts::OS);
println!(
"Unsupported operating system! {:?} might cause some instabilities!",
env::consts::OS
);
location.push(String::from("config.json"));
}
}
@@ -44,46 +60,75 @@ impl Configuration {
settings.merge(File::with_name(&i).required(false)).unwrap();
}
let config = settings
.try_into::<Configuration>()
.expect("Couldn't load config into gt!");
let config: Result<Configuration, config::ConfigError> =
settings.try_into::<Configuration>();
config
match config {
Ok(_) => {
let c = config.unwrap();
if (self.api_token.is_none()) {
self.api_token = c.api_token;
}
if (self.base_url.is_empty()) {
self.base_url = c.base_url;
}
if (self.base_api.is_empty()) {
self.base_api = c.base_api;
}
if (self.username.is_none()) {
self.username = c.username;
}
if (self.password.is_none()) {
self.password = c.password;
}
println!("{:?}", self);
}
Err(e) => {
println!("No config found! Trying to generate config: {}", e);
generate::generate_config();
print!("Go fill out your config.json file in your present working directory. If you need assistance, please reference the readme.");
process::exit(4);
}
}
}
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();
let username_env = env::var("GT_USERNAME").unwrap_or_else(|_| String::new());
let password_env = env::var("GT_PASSWORD").unwrap_or_else(|_| String::new());
let api_token_env = env::var("GT_API_TOKEN").unwrap_or_else(|_| String::new());
let base_url_env = env::var("GT_URL").unwrap_or_else(|_| String::new());
// 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() {
if !username_env.is_empty() {
self.username = Some(username_env);
} else {
println!("cannot find username env var");
}
if password_env != "".to_string() {
password_config = &password_env.deref().to_string();
if !password_env.is_empty() {
self.password = Some(password_env);
} else {
println!("cannot find password env var");
}
if api_token_env != "".to_string() {
api_token_config = &api_token_env.deref().to_string();
if !api_token_env.is_empty() {
self.api_token = Some(api_token_env);
} else {
println!("cannot find api token env var");
}
println!("{:?}", &self);
if !base_url_env.is_empty() {
self.base_url = base_url_env;
}
println!("{:?}", &self);
}
}