diff --git a/nix/pkgs/main/default.nix b/nix/pkgs/main/default.nix index 9ac910391c388e1e94d3e8cc0e22fab061359478..3ede7ffda674ebf57aeb3b7d3e730c63098f67c6 100644 --- a/nix/pkgs/main/default.nix +++ b/nix/pkgs/main/default.nix @@ -37,7 +37,7 @@ buildDepsOnlyEnv = }); buildPackageEnv = { - CONDUIT_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev; + CONDUWUIT_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev; } // buildDepsOnlyEnv; commonAttrs = { diff --git a/src/api/client_server/unversioned.rs b/src/api/client_server/unversioned.rs index c9dd1a4b94364fa7a39b1bfd491ad9f8c95540c8..725b88bbaf0c35b0e8b659753350aa47fb8932c7 100644 --- a/src/api/client_server/unversioned.rs +++ b/src/api/client_server/unversioned.rs @@ -10,7 +10,7 @@ error::ErrorKind, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, utils::conduwuit_version, Error, Result, Ruma}; /// # `GET /_matrix/client/versions` /// @@ -142,14 +142,9 @@ pub(crate) async fn syncv3_client_server_json() -> Result<impl IntoResponse> { }, }; - let version = match option_env!("CONDUIT_VERSION_EXTRA") { - Some(extra) => format!("{} ({})", env!("CARGO_PKG_VERSION"), extra), - None => env!("CARGO_PKG_VERSION").to_owned(), - }; - Ok(Json(serde_json::json!({ "server": server_url, - "version": version, + "version": conduwuit_version(), }))) } @@ -158,13 +153,8 @@ pub(crate) async fn syncv3_client_server_json() -> Result<impl IntoResponse> { /// Conduwuit-specific API to get the server version, results akin to /// `/_matrix/federation/v1/version` pub(crate) async fn conduwuit_server_version() -> Result<impl IntoResponse> { - let version = match option_env!("CONDUIT_VERSION_EXTRA") { - Some(extra) => format!("{} ({})", env!("CARGO_PKG_VERSION"), extra), - None => env!("CARGO_PKG_VERSION").to_owned(), - }; - Ok(Json(serde_json::json!({ "name": "Conduwuit", - "version": version, + "version": conduwuit_version(), }))) } diff --git a/src/api/server_server.rs b/src/api/server_server.rs index 66fccc7e298274995f27858f760356de024e7f0a..1d0cbd9d079b2d216b6a99e001340f5dbae437b1 100644 --- a/src/api/server_server.rs +++ b/src/api/server_server.rs @@ -66,15 +66,10 @@ pub(crate) async fn get_server_version_route( _body: Ruma<get_server_version::v1::Request>, ) -> Result<get_server_version::v1::Response> { - let version = match option_env!("CONDUIT_VERSION_EXTRA") { - Some(extra) => format!("{} ({})", env!("CARGO_PKG_VERSION"), extra), - None => env!("CARGO_PKG_VERSION").to_owned(), - }; - Ok(get_server_version::v1::Response { server: Some(get_server_version::v1::Server { name: Some("Conduwuit".to_owned()), - version: Some(version), + version: Some(utils::conduwuit_version()), }), }) } diff --git a/src/main.rs b/src/main.rs index 81b714357ecce7d645fb496bbf340187fdbac2b5..c82d81192ec87d4fd619a82e3402a6b01f6314e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -287,7 +287,7 @@ fn init(args: clap::Args) -> Result<Server, Error> { database_path = ?config.database_path, log_levels = ?config.log, "{}", - env!("CARGO_PKG_VERSION"), + utils::conduwuit_version(), ); #[cfg(unix)] diff --git a/src/service/globals/client.rs b/src/service/globals/client.rs index 9b67c0b41b67e5ba47b49a40faba07d4de2229af..3b608c73b8acda5aa1d9c7f3bc488817c045fa3f 100644 --- a/src/service/globals/client.rs +++ b/src/service/globals/client.rs @@ -2,7 +2,7 @@ use reqwest::redirect; -use crate::{service::globals::resolver, Config, Result}; +use crate::{service::globals::resolver, utils::conduwuit_version, Config, Result}; pub(crate) struct Client { pub(crate) default: reqwest::Client, @@ -87,10 +87,7 @@ pub(crate) fn new(config: &Config, resolver: &Arc<resolver::Resolver>) -> Client } fn base(config: &Config) -> Result<reqwest::ClientBuilder> { - let version = match option_env!("CONDUIT_VERSION_EXTRA") { - Some(extra) => format!("{} ({})", env!("CARGO_PKG_VERSION"), extra), - None => env!("CARGO_PKG_VERSION").to_owned(), - }; + let version = conduwuit_version(); let mut builder = reqwest::Client::builder() .hickory_dns(true) diff --git a/src/utils/clap.rs b/src/utils/clap.rs index 22ff6cab442ec62d3e631d6ec382b554bec4a9ca..4c88d836d9d6367be6951475a6a083fe522adfc4 100644 --- a/src/utils/clap.rs +++ b/src/utils/clap.rs @@ -4,24 +4,11 @@ use clap::Parser; -/// Returns the current version of the crate with extra info if supplied -/// -/// Set the environment variable `CONDUIT_VERSION_EXTRA` to any UTF-8 string to -/// include it in parenthesis after the SemVer version. A common value are git -/// commit hashes. -#[allow(clippy::doc_markdown)] -fn version() -> String { - let cargo_pkg_version = env!("CARGO_PKG_VERSION"); - - match option_env!("CONDUIT_VERSION_EXTRA") { - Some(x) => format!("{} ({})", cargo_pkg_version, x), - None => cargo_pkg_version.to_owned(), - } -} +use super::conduwuit_version; /// Commandline arguments #[derive(Parser, Debug)] -#[clap(version = version(), about, long_about = None)] +#[clap(version = conduwuit_version(), about, long_about = None)] pub(crate) struct Args { #[arg(short, long)] /// Optional argument to the path of a conduwuit config TOML file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 8b9881513cc85bf13423e8b30b4a0a66bd54e8eb..1184e1812b02c9e6d7c344e568989a6dd477270f 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -187,3 +187,19 @@ fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { Ok(()) } } + +/// one true function for returning the conduwuit version with the necessary +/// CONDUWUIT_VERSION_EXTRA env variables used if specified +/// +/// Set the environment variable `CONDUWUIT_VERSION_EXTRA` to any UTF-8 string +/// to include it in parenthesis after the SemVer version. A common value are +/// git commit hashes. +pub(crate) fn conduwuit_version() -> String { + match option_env!("CONDUWUIT_VERSION_EXTRA") { + Some(extra) => format!("{} ({})", env!("CARGO_PKG_VERSION"), extra), + None => match option_env!("CONDUIT_VERSION_EXTRA") { + Some(extra) => format!("{} ({})", env!("CARGO_PKG_VERSION"), extra), + None => env!("CARGO_PKG_VERSION").to_owned(), + }, + } +}