From a942c3c01632cfc54473c155e40c8ee282f6c98d Mon Sep 17 00:00:00 2001 From: Philip Date: Sat, 12 Mar 2022 07:44:26 +0100 Subject: [PATCH] Write a config file if it is missing --- src/defaultconfig.toml | 13 ++++++------- src/main.rs | 1 + src/settings.rs | 44 ++++++++++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/defaultconfig.toml b/src/defaultconfig.toml index c4d0812..4c73c14 100644 --- a/src/defaultconfig.toml +++ b/src/defaultconfig.toml @@ -1,6 +1,9 @@ debug= false [steamgrid_db] +auth_key = "Write your authentication key between these quotes" +enabled = true +prefer_animated = false [origin] enabled = true @@ -19,15 +22,11 @@ enabled = true [legendary] enabled = true -[steam] - [itch] enabled = true create_symlinks = true -[steamgrid_db] -enabled = true -prefer_animated = false - [lutris] -enabled = true \ No newline at end of file +enabled = true + +[steam] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 01aa197..4503fc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,6 +25,7 @@ async fn main() -> Result<(), Box> { #[cfg(not(feature = "ui"))] { let settings = settings::Settings::new()?; + settings::Settings::write_config_if_missing(); sync::run_sync(&settings).await } } diff --git a/src/settings.rs b/src/settings.rs index f84863d..6c61ad9 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,13 +1,7 @@ use crate::{ - egs::EpicGamesLauncherSettings, - gog::GogSettings, - itch::ItchSettings, - legendary::LegendarySettings, - origin::OriginSettings, - steam::SteamSettings, - steamgriddb::SteamGridDbSettings, - uplay::UplaySettings, - lutris::settings::LutrisSettings, + egs::EpicGamesLauncherSettings, gog::GogSettings, itch::ItchSettings, + legendary::LegendarySettings, lutris::settings::LutrisSettings, origin::OriginSettings, + steam::SteamSettings, steamgriddb::SteamGridDbSettings, uplay::UplaySettings, }; use config::{Config, ConfigError, Environment, File}; @@ -52,6 +46,36 @@ impl Settings { // Eg.. `STEAMSYNC_DEBUG=1 ./target/app` would set the `debug` key s.merge(Environment::with_prefix("steamsync").separator("-"))?; - s.try_into() + let mut result: Result = s.try_into(); + + sanitize_auth_key(&mut result); + + + + result + } + + pub fn write_config_if_missing() { + let config_path = std::path::Path::new("config.toml"); + if !config_path.exists() { + let worked = std::fs::write(config_path, include_str!("defaultconfig.toml")); + match worked { + Ok(_) => println!("Create configuration file at {:?}", &config_path), + Err(err) => println!( + "Could not create configuration file at {:?}, reason: {:?}", + &config_path, err + ), + } + } + } +} + +fn sanitize_auth_key(result: &mut Result) { + if let Ok(result) = result.as_mut() { + if let Some(auth_key) = result.steamgrid_db.auth_key.as_ref() { + if auth_key == "Write your authentication key between these quotes" { + result.steamgrid_db.auth_key = None; + } + } } }