diff --git a/src/config.rs b/src/config.rs index c1b6f69..f902f5a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,6 +7,7 @@ pub struct ConfigLogin { pub username: String, pub password: String, pub device_id: String, + pub home_server_url: String } impl Default for ConfigLogin { @@ -15,6 +16,7 @@ impl Default for ConfigLogin { username: "".to_string(), password: "".to_string(), device_id: "".to_string(), + home_server_url: "".to_string() } } } diff --git a/src/main.rs b/src/main.rs index dc57805..6601eb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use config::{ConfigImages, ConfigLogin}; mod settings; use settings::Settings; -const HOME_SERVER: &str = "https://e1m1.xyz"; +// TODO 16.06.2021: Use token for login in the future. #[tokio::main] async fn main() { @@ -33,14 +33,7 @@ async fn run() -> Result<(), Box> { let dir_login = &dir_exe.join("login.json"); let dir_images = &dir_exe.join("images.json"); - let settings_login: Settings = - Settings::::load(&dir_login).unwrap(); - - let mut settings_images: Settings = - Settings::::load(&dir_images).unwrap(); - - let conf_login = &settings_login.loaded; - let conf_images = &mut settings_images.loaded; + // --- DEBUG STUFF / LAUNCH ARGS PARSER --- let mut flag_debug: bool = false; let mut flag_generate: bool = false; @@ -59,6 +52,12 @@ async fn run() -> Result<(), Box> { } } + if !dir_login.exists() || !dir_images.exists() { + flag_generate = true; + } + + // --- END DEBUG --- + if flag_generate { println!("Generating config files.."); Settings::::new(ConfigLogin::default()) @@ -69,16 +68,19 @@ async fn run() -> Result<(), Box> { .unwrap() .save(&dir_images.with_extension("json.template").clone()) .unwrap(); - return Ok(()); } - if let Ok(client) = login( - conf_login.username.as_str(), - conf_login.password.as_str(), - conf_login.device_id.as_str(), - ) - .await + let settings_login: Settings = + Settings::::load(&dir_login).unwrap(); + + let mut settings_images: Settings = + Settings::::load(&dir_images).unwrap(); + + let conf_login = &settings_login.loaded; + let conf_images = &mut settings_images.loaded; + + if let Ok(client) = login(&conf_login).await { let selection = { if flag_debug { @@ -130,15 +132,12 @@ async fn run() -> Result<(), Box> { Ok(()) } -async fn login( - username: &str, - password: &str, - device_id: &str, +async fn login(config: &ConfigLogin ) -> Result { - let client = Client::new(Url::parse(HOME_SERVER).unwrap()).unwrap(); + let client = Client::new(Url::parse(config.home_server_url.as_str()).unwrap()).unwrap(); match client - .login(username, password, Some(device_id), Some("NEO bot")) + .login(config.username.as_str(), config.password.as_str(), Some(config.device_id.as_str()), Some("NEO bot")) .await { Ok(_) => return Ok(client),