cdk_ansible_cli/
version.rsuse cdk_ansible_static::*;
use serde::Serialize;
use std::fmt;
#[derive(Serialize)]
pub(crate) struct CommitInfo {
short_commit_hash: String,
commit_hash: String,
commit_date: String,
last_tag: Option<String>,
commits_since_last_tag: u32,
}
#[derive(Serialize)]
pub struct VersionInfo {
version: String,
commit_info: Option<CommitInfo>,
}
impl fmt::Display for VersionInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.version)?;
if let Some(ref ci) = self.commit_info {
if ci.commits_since_last_tag > 0 {
write!(f, "+{}", ci.commits_since_last_tag)?;
}
write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?;
}
Ok(())
}
}
pub fn pkg_version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
pub(crate) fn version() -> VersionInfo {
let version = pkg_version().to_string();
let commit_info = CDK_ANSIBLE_COMMIT_HASH.map(|commit_hash| CommitInfo {
short_commit_hash: CDK_ANSIBLE_COMMIT_SHORT_HASH.unwrap().to_string(),
commit_hash: commit_hash.to_string(),
commit_date: CDK_ANSIBLE_COMMIT_DATE.unwrap().to_string(),
last_tag: CDK_ANSIBLE_LAST_TAG.map(|tag| tag.to_string()),
commits_since_last_tag: CDK_ANSIBLE_LAST_TAG_DISTANCE
.map_or(0, |distance| distance.parse::<u32>().unwrap_or(0)),
});
VersionInfo {
version,
commit_info,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_version() {
assert_eq!(version().to_string(), env!("CARGO_PKG_VERSION").to_string());
}
}