gt/src/config.rs

140 lines
4.2 KiB
Rust

use std::env;
use std::ops::Deref;
use std::process;
use config::File;
use serde::{Deserialize, Serialize};
use crate::generate;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Configuration {
pub api_token: Option<String>,
pub base_api: String,
pub base_url: String,
pub username: Option<String>,
pub password: Option<String>,
}
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();
match env::consts::OS {
// this case is currently untested
"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));
}
_ => {
println!(
"Unsupported operating system! {:?} might cause some instabilities!",
env::consts::OS
);
location.push(String::from("config.json"));
}
}
for i in location {
settings.merge(File::with_name(&i).required(false)).unwrap();
}
let config: Result<Configuration, config::ConfigError> =
settings.try_into::<Configuration>();
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(|_| 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.is_empty() {
self.username = Some(username_env);
} else {
println!("cannot find username env var");
}
if !password_env.is_empty() {
self.password = Some(password_env);
} else {
println!("cannot find password env var");
}
if !api_token_env.is_empty() {
self.api_token = Some(api_token_env);
} else {
println!("cannot find api token env var");
}
if !base_url_env.is_empty() {
self.base_url = base_url_env;
}
println!("{:?}", &self);
}
}
impl Default for Configuration {
fn default() -> Self {
Self::new()
}
}