Merge branch 'build-stamped-version'

This commit is contained in:
ari committed 2026-10-20 23:38:12 -04:00
commit b3b9103f2f
18 files changed
+322 -18

No files matched your search

+1 -1
View File
@@ -67,7 +67,7 @@ pub fn router(state: AppState) -> Router {
pub async fn healthz() -> Json<serde_json::Value> {
Json(serde_json::json!({
"status": "ok",
"version": env!("CARGO_PKG_VERSION"),
"version": crate::VERSION,
}))
}
+1 -1
View File
@@ -31,7 +31,7 @@ pub fn routes() -> Router<AppState> {
pub(crate) async fn ping() -> Json<serde_json::Value> {
Json(serde_json::json!({
"status": "ok",
"version": env!("CARGO_PKG_VERSION"),
"version": crate::VERSION,
}))
}
+1 -1
View File
@@ -12,5 +12,5 @@ async fn ping_reports_version() {
assert_eq!(status, StatusCode::OK);
let v = json(&body);
assert_eq!(v["status"], "ok");
assert_eq!(v["version"], env!("CARGO_PKG_VERSION"));
assert_eq!(v["version"], crate::VERSION);
}
+1 -1
View File
@@ -42,5 +42,5 @@ async fn timers_ok_and_unknown_route() {
async fn healthz_shape() {
let v = healthz().await.0;
assert_eq!(v["status"], "ok");
assert!(v["version"].is_string());
assert_eq!(v["version"], crate::VERSION);
}
+4
View File
@@ -18,3 +18,7 @@ pub mod sources;
pub mod state;
pub mod telemetry;
pub mod tls;
pub mod version;
/// Build-stamped version, `YYYY.MM.DD-<branch>-<sha>`; see `build.rs`.
pub const VERSION: &str = env!("SLINUXM_VERSION");
+4 -1
View File
@@ -18,7 +18,10 @@ use skyg_linux_tracker::state::AppState;
use skyg_linux_tracker::{cli, db, server, telemetry, tls};
#[derive(Debug, Parser)]
#[command(name = "slinuxm", version, about = "Read-only Linux system monitor")]
#[command(
name = "slinuxm",
version = skyg_linux_tracker::VERSION,
about = "Read-only Linux system monitor")]
struct Cli {
/// Config file (default: /etc/skyg-linux-tracker.toml if present).
#[arg(long, global = true, env = "SKYG_CONFIG")]
+1 -4
View File
@@ -461,10 +461,7 @@ impl ServerHandler for TrackerService {
.enable_resources()
.build(),
)
.with_server_info(Implementation::new(
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
))
.with_server_info(Implementation::new(env!("CARGO_PKG_NAME"), crate::VERSION))
.with_instructions(instructions(self.state.peers.is_some()))
}
+1 -4
View File
@@ -272,10 +272,7 @@ fn hostname() -> String {
fn build_resource(otel: &OtelConfig) -> Resource {
Resource::builder()
.with_service_name(otel.service_name.clone())
.with_attribute(KeyValue::new(
"service.version",
env!("CARGO_PKG_VERSION").to_owned(),
))
.with_attribute(KeyValue::new("service.version", crate::VERSION.to_owned()))
.with_attribute(KeyValue::new("host.name", hostname()))
.build()
}
+92
View File
@@ -0,0 +1,92 @@
//! Formatting of the build-stamped version, `YYYY.MM.DD-<branch>-<sha>`.
//!
//! `build.rs` compiles this file directly (`#[path]`), so it must stay
//! std-only and must not refer to anything else in the crate.
#![allow(dead_code)]
/// Joins the three parts. The inputs are expected to be clean already
/// (`date` from [`date_from_epoch`], the others from [`sanitize_branch`] and
/// [`sanitize_sha`]).
pub fn compose(date: &str, branch: &str, sha: &str) -> String {
format!("{date}-{branch}-{sha}")
}
/// Keeps `[A-Za-z0-9._-]`, turns everything else (notably the `/` in
/// `feature/x`) into `-`, and trims `-` from both ends. A detached HEAD
/// (`HEAD` from `git rev-parse --abbrev-ref`) and an empty result become
/// `detached` and `unknown`.
pub fn sanitize_branch(raw: &str) -> String {
let raw = raw.trim();
if raw == "HEAD" {
return "detached".to_owned();
}
let cleaned: String = raw
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-') {
c
} else {
'-'
}
})
.collect();
let cleaned = cleaned.trim_matches('-');
if cleaned.is_empty() {
"unknown".to_owned()
} else {
cleaned.to_owned()
}
}
/// Keeps `[A-Za-z0-9-]` so a `-dirty` suffix survives, lowercased.
pub fn sanitize_sha(raw: &str) -> String {
let cleaned: String = raw
.trim()
.chars()
.filter(|c| c.is_ascii_alphanumeric() || *c == '-')
.map(|c| c.to_ascii_lowercase())
.collect();
if cleaned.is_empty() {
"unknown".to_owned()
} else {
cleaned
}
}
/// UTC `YYYY.MM.DD` for a Unix timestamp.
pub fn date_from_epoch(secs: u64) -> String {
// Days to civil date, after Howard Hinnant's `civil_from_days`.
let z = (secs / 86_400) as i64 + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
let year = yoe + era * 400 + i64::from(month <= 2);
format!("{year:04}.{month:02}.{day:02}")
}
/// Whether `v` has the shape `YYYY.MM.DD-<branch>-<sha>`: a date, then a
/// non-empty remainder of `[A-Za-z0-9._-]` with at least one more `-`.
pub fn is_valid(v: &str) -> bool {
let b = v.as_bytes();
if b.len() < 13 || b[10] != b'-' {
return false;
}
let digits = |r: std::ops::Range<usize>| b[r].iter().all(u8::is_ascii_digit);
let date_ok = digits(0..4) && b[4] == b'.' && digits(5..7) && b[7] == b'.' && digits(8..10);
let rest = &v[11..];
date_ok
&& rest.contains('-')
&& !rest.starts_with('-')
&& !rest.ends_with('-')
&& rest
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
}
#[cfg(test)]
#[path = "version_tests.rs"]
mod tests;
+53
View File
@@ -0,0 +1,53 @@
use crate::version::{compose, date_from_epoch, is_valid, sanitize_branch, sanitize_sha};
#[test]
fn compiled_version_has_the_documented_shape() {
assert!(is_valid(crate::VERSION), "VERSION = {}", crate::VERSION);
}
#[test]
fn compose_joins_date_branch_and_sha() {
assert_eq!(
compose("2026.10.20", "main", "abc1234"),
"2026.10.20-main-abc1234"
);
}
#[test]
fn branch_is_sanitised() {
assert_eq!(sanitize_branch("main"), "main");
assert_eq!(sanitize_branch("feature/x y"), "feature-x-y");
assert_eq!(sanitize_branch("release/1.2_rc"), "release-1.2_rc");
assert_eq!(sanitize_branch("//"), "unknown");
assert_eq!(sanitize_branch(""), "unknown");
assert_eq!(sanitize_branch("HEAD"), "detached");
}
#[test]
fn sha_is_lowercased_and_keeps_the_dirty_suffix() {
assert_eq!(sanitize_sha("ABC1234\n"), "abc1234");
assert_eq!(sanitize_sha("abc1234-dirty"), "abc1234-dirty");
assert_eq!(sanitize_sha(""), "unknown");
}
#[test]
fn date_is_utc_and_zero_padded() {
assert_eq!(date_from_epoch(0), "1970.01.01");
assert_eq!(date_from_epoch(951_782_400), "2000.02.29");
assert_eq!(date_from_epoch(1_772_323_199), "2026.02.28");
assert_eq!(date_from_epoch(1_772_323_200), "2026.03.01");
assert_eq!(date_from_epoch(1_792_454_400), "2026.10.20");
}
#[test]
fn validator_accepts_and_rejects() {
assert!(is_valid("2026.10.20-main-abc1234"));
assert!(is_valid("2026.10.20-feature-x-abc1234-dirty"));
assert!(is_valid("2026.10.20-unknown-unknown"));
assert!(!is_valid("0.1.0"));
assert!(!is_valid("2026.10.20"));
assert!(!is_valid("2026.10.20-main"));
assert!(!is_valid("2026-10-20-main-abc1234"));
assert!(!is_valid("2026.10.20-feature/x-abc1234"));
assert!(!is_valid("2026.10.20--abc1234"));
}