Skip to content

Commit 110b9b2

Browse files
committed
feat: [torrust#937] add version to configration file following semver.
I will fail if the provided version is not supported. ```toml version = "2" ``` It only supports the exect match '2'.
1 parent ddd73ca commit 110b9b2

7 files changed

+89
-3
lines changed

packages/configuration/src/lib.rs

+61-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::sync::Arc;
1212
use std::time::Duration;
1313

1414
use camino::Utf8PathBuf;
15-
use derive_more::Constructor;
15+
use derive_more::{Constructor, Display};
1616
use serde::{Deserialize, Serialize};
1717
use serde_with::serde_as;
1818
use thiserror::Error;
@@ -45,6 +45,63 @@ pub type Threshold = v2::logging::Threshold;
4545

4646
pub type AccessTokens = HashMap<String, String>;
4747

48+
pub const LATEST_VERSION: &str = "2";
49+
50+
/// Info about the configuration specification.
51+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display)]
52+
pub struct Metadata {
53+
#[serde(default = "Metadata::default_version")]
54+
#[serde(flatten)]
55+
version: Version,
56+
}
57+
58+
impl Default for Metadata {
59+
fn default() -> Self {
60+
Self {
61+
version: Self::default_version(),
62+
}
63+
}
64+
}
65+
66+
impl Metadata {
67+
fn default_version() -> Version {
68+
Version::latest()
69+
}
70+
}
71+
72+
/// Info about the configuration specification.
73+
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Display)]
74+
pub struct Version {
75+
#[serde(default = "Version::default_semver")]
76+
version: String,
77+
}
78+
79+
impl Default for Version {
80+
fn default() -> Self {
81+
Self {
82+
version: Self::default_semver(),
83+
}
84+
}
85+
}
86+
87+
impl Version {
88+
fn new(semver: &str) -> Self {
89+
Self {
90+
version: semver.to_owned(),
91+
}
92+
}
93+
94+
fn latest() -> Self {
95+
Self {
96+
version: LATEST_VERSION.to_string(),
97+
}
98+
}
99+
100+
fn default_semver() -> String {
101+
LATEST_VERSION.to_string()
102+
}
103+
}
104+
48105
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Constructor)]
49106
pub struct TrackerPolicy {
50107
// Cleanup job configuration
@@ -208,6 +265,9 @@ pub enum Error {
208265

209266
#[error("The error for errors that can never happen.")]
210267
Infallible,
268+
269+
#[error("Unsupported configuration version: {version}")]
270+
UnsupportedVersion { version: Metadata },
211271
}
212272

213273
impl From<figment::Error> for Error {

packages/configuration/src/v2/mod.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,24 @@ use self::health_check_api::HealthCheckApi;
251251
use self::http_tracker::HttpTracker;
252252
use self::tracker_api::HttpApi;
253253
use self::udp_tracker::UdpTracker;
254-
use crate::{Error, Info};
254+
use crate::{Error, Info, Metadata, Version};
255+
256+
/// This configuration version
257+
const VERSION_2: &str = "2";
255258

256259
/// Prefix for env vars that overwrite configuration options.
257260
const CONFIG_OVERRIDE_PREFIX: &str = "TORRUST_TRACKER_CONFIG_OVERRIDE_";
261+
258262
/// Path separator in env var names for nested values in configuration.
259263
const CONFIG_OVERRIDE_SEPARATOR: &str = "__";
260264

261265
/// Core configuration for the tracker.
262266
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default)]
263267
pub struct Configuration {
268+
/// Configuration metadata.
269+
#[serde(flatten)]
270+
pub metadata: Metadata,
271+
264272
/// Logging configuration
265273
pub logging: Logging,
266274

@@ -326,6 +334,12 @@ impl Configuration {
326334

327335
let config: Configuration = figment.extract()?;
328336

337+
if config.metadata.version != Version::new(VERSION_2) {
338+
return Err(Error::UnsupportedVersion {
339+
version: config.metadata,
340+
});
341+
}
342+
329343
Ok(config)
330344
}
331345

@@ -378,7 +392,9 @@ mod tests {
378392

379393
#[cfg(test)]
380394
fn default_config_toml() -> String {
381-
let config = r#"[logging]
395+
let config = r#"version = "2"
396+
397+
[logging]
382398
threshold = "info"
383399
384400
[core]

share/default/config/tracker.container.mysql.toml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
version = "2"
2+
13
[core.database]
24
driver = "MySQL"
35
path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker"

share/default/config/tracker.container.sqlite3.toml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
version = "2"
2+
13
[core.database]
24
path = "/var/lib/torrust/tracker/database/sqlite3.db"
35

share/default/config/tracker.development.sqlite3.toml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
version = "2"
2+
13
[[udp_trackers]]
24
bind_address = "0.0.0.0:6969"
35

share/default/config/tracker.e2e.container.sqlite3.toml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
version = "2"
2+
13
[core.database]
24
path = "/var/lib/torrust/tracker/database/sqlite3.db"
35

share/default/config/tracker.udp.benchmarking.toml

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
version = "2"
2+
13
[logging]
24
threshold = "error"
35

0 commit comments

Comments
 (0)