summaryrefslogtreecommitdiff
path: root/src/cli/commands/version.rs
blob: 3e8db2179caa81d7b1447f2840f8c062bb422fb1 (plain)
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
75
76
77
78
79
80
81
82
83
use anyhow::Result;

pub struct VersionCommand;

impl VersionCommand {
    pub fn new() -> Self {
        Self
    }

    pub async fn execute(&self) -> Result<()> {
        println!("v{}", env!("CARGO_PKG_VERSION"));
        Ok(())
    }

    pub fn version_info() -> VersionInfo {
        VersionInfo {
            version: env!("CARGO_PKG_VERSION").to_string(),
            commit: option_env!("GIT_COMMIT").unwrap_or("unknown").to_string(),
            build_date: option_env!("BUILD_DATE").unwrap_or("unknown").to_string(),
            target: std::env::var("TARGET").unwrap_or_else(|_| "unknown".to_string()),
            rust_version: std::env::var("RUST_VERSION").unwrap_or_else(|_| "unknown".to_string()),
        }
    }

    pub async fn execute_detailed(&self) -> Result<()> {
        let info = Self::version_info();
        println!("spandx {}", info.version);
        println!("commit: {}", info.commit);
        println!("build date: {}", info.build_date);
        println!("target: {}", info.target);
        println!("rust version: {}", info.rust_version);
        Ok(())
    }
}

impl Default for VersionCommand {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone)]
pub struct VersionInfo {
    pub version: String,
    pub commit: String,
    pub build_date: String,
    pub target: String,
    pub rust_version: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_version_command() {
        let cmd = VersionCommand::new();
        let result = cmd.execute().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_version_command_detailed() {
        let cmd = VersionCommand::new();
        let result = cmd.execute_detailed().await;
        assert!(result.is_ok());
    }

    #[test]
    fn test_version_info() {
        let info = VersionCommand::version_info();
        assert!(!info.version.is_empty());
        assert!(!info.target.is_empty());
        assert!(!info.rust_version.is_empty());
    }

    #[test]
    fn test_version_command_default() {
        let cmd = VersionCommand::default();
        // Just ensure we can create the command with default
        assert!(true);
    }
}