added configuration system
This commit is contained in:
parent
9c6381dcfe
commit
1ff6efdd9d
34
src/config.rs
Normal file
34
src/config.rs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
use std::env;
|
||||||
|
|
||||||
|
use config::File;
|
||||||
|
use serde::{Deserialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Configuration {
|
||||||
|
pub api_token: String,
|
||||||
|
pub base_api: String,
|
||||||
|
pub base_url: String,
|
||||||
|
pub username: Option<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Configuration {
|
||||||
|
pub fn new() -> Configuration {
|
||||||
|
let home_dir_env = env::var("HOME").unwrap();
|
||||||
|
let mut settings = config::Config::default();
|
||||||
|
let mut location: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
location.push("config.json".to_string());
|
||||||
|
location.push("/etc/gt/config.json".to_string());
|
||||||
|
location.push(format!("{}/.config/gt/config.json", home_dir_env));
|
||||||
|
|
||||||
|
for i in location {
|
||||||
|
settings.merge(File::with_name(&i)
|
||||||
|
.required(false))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
let config = settings.try_into::<Configuration>().expect("Couldn't load config into gt!");
|
||||||
|
|
||||||
|
config
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user