cdk_ansible_cli/
version.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use 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, such as "1.2.3"
    version: String,
    /// Information about the git commit we may have been built from.
    ///
    /// `None` if not built from a git repo or if retrieval failed.
    commit_info: Option<CommitInfo>,
}

impl fmt::Display for VersionInfo {
    /// Formatted version information: "<version>[+<commits>] (<commit> <date>)"
    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(())
    }
}

/// Return the application version.
pub fn pkg_version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

/// Returns version information.
pub(crate) fn version() -> VersionInfo {
    // let version = cdk_ansible_version::version().to_string();
    let version = pkg_version().to_string();

    // Commit info is pulled from git and set by `build.rs`
    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());
    }
}