diff --git a/osinfo/osinfo.dsc.resource.json b/osinfo/osinfo.dsc.resource.json index 037355fa4..cabf70518 100644 --- a/osinfo/osinfo.dsc.resource.json +++ b/osinfo/osinfo.dsc.resource.json @@ -13,7 +13,10 @@ "executable": "osinfo" }, "export": { - "executable": "osinfo" + "executable": "osinfo", + "args": [ + "export" + ] }, "schema": { "embedded": { diff --git a/osinfo/src/config.rs b/osinfo/src/config.rs index 2e8792943..a6ebf6aae 100644 --- a/osinfo/src/config.rs +++ b/osinfo/src/config.rs @@ -2,6 +2,7 @@ // Licensed under the MIT License. use serde::Serialize; +use std::fmt::Display; use std::string::ToString; /// Returns information about the operating system. @@ -24,6 +25,8 @@ pub struct OsInfo { /// Defines the processor architecture as reported by `uname -m` on the operating system. #[serde(skip_serializing_if = "Option::is_none")] architecture: Option, + #[serde(rename = "_name", skip_serializing_if = "Option::is_none")] + name: Option, } /// Defines whether the operating system is a 32-bit or 64-bit operating system. @@ -46,10 +49,20 @@ pub enum Family { Windows, } +impl Display for Family { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Family::Linux => write!(f, "Linux"), + Family::MacOS => write!(f, "macOS"), + Family::Windows => write!(f, "Windows"), + } + } +} + const ID: &str = "https://developer.microsoft.com/json-schemas/dsc/os_info/20230303/Microsoft.Dsc.OS_Info.schema.json"; impl OsInfo { - pub fn new() -> Self { + pub fn new(include_name: bool) -> Self { let os_info = os_info::get(); let edition = os_info.edition().map(ToString::to_string); let codename = os_info.codename().map(ToString::to_string); @@ -64,14 +77,26 @@ impl OsInfo { os_info::Bitness::X64 => Bitness::Bit64, _ => Bitness::Unknown, }; + let version = os_info.version().to_string(); + let name = if include_name { + Some( + match &architecture { + Some(arch) => format!("{family} {version} {arch}"), + None => format!("{family:?} {version}"), + } + ) + } else { + None + }; Self { id: ID.to_string(), family, - version: os_info.version().to_string(), + version, edition, codename, bitness: bits, architecture, + name, } } } diff --git a/osinfo/src/main.rs b/osinfo/src/main.rs index 6375171d4..1963f3ab5 100644 --- a/osinfo/src/main.rs +++ b/osinfo/src/main.rs @@ -4,6 +4,8 @@ mod config; fn main() { - let json = serde_json::to_string(&config::OsInfo::new()).unwrap(); + let args: Vec = std::env::args().collect(); + let include_name = args.len() > 1 && args[1] == "export"; + let json = serde_json::to_string(&config::OsInfo::new(include_name)).unwrap(); println!("{json}"); } diff --git a/osinfo/tests/osinfo.tests.ps1 b/osinfo/tests/osinfo.tests.ps1 index ee328316b..b07e81ced 100644 --- a/osinfo/tests/osinfo.tests.ps1 +++ b/osinfo/tests/osinfo.tests.ps1 @@ -22,6 +22,8 @@ Describe 'osinfo resource tests' { else { $out.actualState.bitness | Should -BeExactly '32' } + + $out._name | Should -BeNullOrEmpty } It 'should perform synthetic test' { @@ -52,5 +54,6 @@ Describe 'osinfo resource tests' { elseif ($IsMacOS) { $out.resources[0].properties.family | Should -BeExactly 'macOS' } + $out.resources[0].name | Should -BeExactly "$($out.resources[0].properties.family) $($out.resources[0].properties.version) $($out.resources[0].properties.architecture)" } }