dedupe version getting code, rename to CONDUWUIT_VERSION_EXTRA

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-05-01 13:35:16 -04:00 committed by June
parent 8ec9372a8e
commit a496cc4705
7 changed files with 26 additions and 41 deletions

View file

@ -37,7 +37,7 @@ buildDepsOnlyEnv =
}); });
buildPackageEnv = { buildPackageEnv = {
CONDUIT_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev; CONDUWUIT_VERSION_EXTRA = inputs.self.shortRev or inputs.self.dirtyShortRev;
} // buildDepsOnlyEnv; } // buildDepsOnlyEnv;
commonAttrs = { commonAttrs = {

View file

@ -10,7 +10,7 @@ use ruma::api::client::{
error::ErrorKind, error::ErrorKind,
}; };
use crate::{services, Error, Result, Ruma}; use crate::{services, utils::conduwuit_version, Error, Result, Ruma};
/// # `GET /_matrix/client/versions` /// # `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!({ Ok(Json(serde_json::json!({
"server": server_url, "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 /// Conduwuit-specific API to get the server version, results akin to
/// `/_matrix/federation/v1/version` /// `/_matrix/federation/v1/version`
pub(crate) async fn conduwuit_server_version() -> Result<impl IntoResponse> { 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!({ Ok(Json(serde_json::json!({
"name": "Conduwuit", "name": "Conduwuit",
"version": version, "version": conduwuit_version(),
}))) })))
} }

View file

@ -66,15 +66,10 @@ use crate::{
pub(crate) async fn get_server_version_route( pub(crate) async fn get_server_version_route(
_body: Ruma<get_server_version::v1::Request>, _body: Ruma<get_server_version::v1::Request>,
) -> Result<get_server_version::v1::Response> { ) -> 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 { Ok(get_server_version::v1::Response {
server: Some(get_server_version::v1::Server { server: Some(get_server_version::v1::Server {
name: Some("Conduwuit".to_owned()), name: Some("Conduwuit".to_owned()),
version: Some(version), version: Some(utils::conduwuit_version()),
}), }),
}) })
} }

View file

@ -287,7 +287,7 @@ fn init(args: clap::Args) -> Result<Server, Error> {
database_path = ?config.database_path, database_path = ?config.database_path,
log_levels = ?config.log, log_levels = ?config.log,
"{}", "{}",
env!("CARGO_PKG_VERSION"), utils::conduwuit_version(),
); );
#[cfg(unix)] #[cfg(unix)]

View file

@ -2,7 +2,7 @@ use std::{sync::Arc, time::Duration};
use reqwest::redirect; 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) struct Client {
pub(crate) default: reqwest::Client, pub(crate) default: reqwest::Client,
@ -87,10 +87,7 @@ impl Client {
} }
fn base(config: &Config) -> Result<reqwest::ClientBuilder> { fn base(config: &Config) -> Result<reqwest::ClientBuilder> {
let version = match option_env!("CONDUIT_VERSION_EXTRA") { let version = conduwuit_version();
Some(extra) => format!("{} ({})", env!("CARGO_PKG_VERSION"), extra),
None => env!("CARGO_PKG_VERSION").to_owned(),
};
let mut builder = reqwest::Client::builder() let mut builder = reqwest::Client::builder()
.hickory_dns(true) .hickory_dns(true)

View file

@ -4,24 +4,11 @@ use std::path::PathBuf;
use clap::Parser; use clap::Parser;
/// Returns the current version of the crate with extra info if supplied use super::conduwuit_version;
///
/// 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(),
}
}
/// Commandline arguments /// Commandline arguments
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap(version = version(), about, long_about = None)] #[clap(version = conduwuit_version(), about, long_about = None)]
pub(crate) struct Args { pub(crate) struct Args {
#[arg(short, long)] #[arg(short, long)]
/// Optional argument to the path of a conduwuit config TOML file /// Optional argument to the path of a conduwuit config TOML file

View file

@ -187,3 +187,19 @@ impl fmt::Display for HtmlEscape<'_> {
Ok(()) 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(),
},
}
}