Show warning if config version is outdated or invalid

This commit is contained in:
timvisee 2021-11-15 16:32:13 +01:00
parent 6b38dce5ab
commit 17ec663e15
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
3 changed files with 46 additions and 1 deletions

7
Cargo.lock generated
View File

@ -634,6 +634,7 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"toml", "toml",
"version-compare",
"winapi", "winapi",
] ]
@ -1264,6 +1265,12 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "version-compare"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.3" version = "0.9.3"

View File

@ -39,6 +39,7 @@ shlex = "1.1"
thiserror = "1.0" thiserror = "1.0"
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "io-util", "net", "macros", "time", "process", "signal", "sync"] } tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "io-util", "net", "macros", "time", "process", "signal", "sync"] }
toml = "0.5" toml = "0.5"
version-compare = "0.1"
# Feature: rcon # Feature: rcon
rust_rcon = { package = "rcon", version = "0.5", optional = true } rust_rcon = { package = "rcon", version = "0.5", optional = true }

View File

@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
use clap::ArgMatches; use clap::ArgMatches;
use serde::Deserialize; use serde::Deserialize;
use version_compare::Cmp;
use crate::proto; use crate::proto;
use crate::util::error::{quit_error, quit_error_msg, ErrorHintsBuilder}; use crate::util::error::{quit_error, quit_error_msg, ErrorHintsBuilder};
@ -12,6 +13,9 @@ use crate::util::error::{quit_error, quit_error_msg, ErrorHintsBuilder};
/// Default configuration file location. /// Default configuration file location.
pub const CONFIG_FILE: &str = "lazymc.toml"; pub const CONFIG_FILE: &str = "lazymc.toml";
/// Configuration version user should be using, or warning will be shown.
const CONFIG_VERSION: &str = "0.2.0";
/// Load config from file, based on CLI arguments. /// Load config from file, based on CLI arguments.
/// ///
/// Quits with an error message on failure. /// Quits with an error message on failure.
@ -96,13 +100,32 @@ pub struct Config {
/// Advanced configuration. /// Advanced configuration.
#[serde(default)] #[serde(default)]
pub advanced: Advanced, pub advanced: Advanced,
/// Config configuration.
#[serde(default)]
pub config: ConfigConfig,
} }
impl Config { impl Config {
/// Load configuration from file. /// Load configuration from file.
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> { pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
let data = fs::read(path)?; let data = fs::read(path)?;
let config = toml::from_slice(&data)?; let config: Config = toml::from_slice(&data)?;
// Show warning if config version is problematic
match &config.config.version {
None => warn!(target: "lazymc::config", "Config version unknown, it may be outdated"),
Some(version) => match version_compare::compare_to(version, CONFIG_VERSION, Cmp::Ge) {
Ok(false) => {
warn!(target: "lazymc::config", "Config is for older lazymc version, you may need to update it")
}
Err(_) => {
warn!(target: "lazymc::config", "Config version is invalid, you may need to update it")
}
Ok(true) => {}
},
}
Ok(config) Ok(config)
} }
} }
@ -341,6 +364,20 @@ impl Default for Advanced {
} }
} }
/// Config configuration.
#[derive(Debug, Deserialize)]
#[serde(default)]
pub struct ConfigConfig {
/// Configuration for lazymc version.
pub version: Option<String>,
}
impl Default for ConfigConfig {
fn default() -> Self {
Self { version: None }
}
}
fn option_pathbuf_dot() -> Option<PathBuf> { fn option_pathbuf_dot() -> Option<PathBuf> {
Some(".".into()) Some(".".into())
} }