Skip to content

Commit 7a0bda5

Browse files
authored
Merge pull request #1038 from SteveL-MSFT/osinfo-export-name
Update OSInfo resource to return `_name`
2 parents a909230 + 336ca34 commit 7a0bda5

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

osinfo/osinfo.dsc.resource.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"executable": "osinfo"
1414
},
1515
"export": {
16-
"executable": "osinfo"
16+
"executable": "osinfo",
17+
"args": [
18+
"export"
19+
]
1720
},
1821
"schema": {
1922
"embedded": {

osinfo/src/config.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
use serde::Serialize;
5+
use std::fmt::Display;
56
use std::string::ToString;
67

78
/// Returns information about the operating system.
@@ -24,6 +25,8 @@ pub struct OsInfo {
2425
/// Defines the processor architecture as reported by `uname -m` on the operating system.
2526
#[serde(skip_serializing_if = "Option::is_none")]
2627
architecture: Option<String>,
28+
#[serde(rename = "_name", skip_serializing_if = "Option::is_none")]
29+
name: Option<String>,
2730
}
2831

2932
/// Defines whether the operating system is a 32-bit or 64-bit operating system.
@@ -46,10 +49,20 @@ pub enum Family {
4649
Windows,
4750
}
4851

52+
impl Display for Family {
53+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54+
match self {
55+
Family::Linux => write!(f, "Linux"),
56+
Family::MacOS => write!(f, "macOS"),
57+
Family::Windows => write!(f, "Windows"),
58+
}
59+
}
60+
}
61+
4962
const ID: &str = "https://developer.microsoft.com/json-schemas/dsc/os_info/20230303/Microsoft.Dsc.OS_Info.schema.json";
5063

5164
impl OsInfo {
52-
pub fn new() -> Self {
65+
pub fn new(include_name: bool) -> Self {
5366
let os_info = os_info::get();
5467
let edition = os_info.edition().map(ToString::to_string);
5568
let codename = os_info.codename().map(ToString::to_string);
@@ -64,14 +77,26 @@ impl OsInfo {
6477
os_info::Bitness::X64 => Bitness::Bit64,
6578
_ => Bitness::Unknown,
6679
};
80+
let version = os_info.version().to_string();
81+
let name = if include_name {
82+
Some(
83+
match &architecture {
84+
Some(arch) => format!("{family} {version} {arch}"),
85+
None => format!("{family:?} {version}"),
86+
}
87+
)
88+
} else {
89+
None
90+
};
6791
Self {
6892
id: ID.to_string(),
6993
family,
70-
version: os_info.version().to_string(),
94+
version,
7195
edition,
7296
codename,
7397
bitness: bits,
7498
architecture,
99+
name,
75100
}
76101
}
77102
}

osinfo/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
mod config;
55

66
fn main() {
7-
let json = serde_json::to_string(&config::OsInfo::new()).unwrap();
7+
let args: Vec<String> = std::env::args().collect();
8+
let include_name = args.len() > 1 && args[1] == "export";
9+
let json = serde_json::to_string(&config::OsInfo::new(include_name)).unwrap();
810
println!("{json}");
911
}

osinfo/tests/osinfo.tests.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Describe 'osinfo resource tests' {
2222
else {
2323
$out.actualState.bitness | Should -BeExactly '32'
2424
}
25+
26+
$out._name | Should -BeNullOrEmpty
2527
}
2628

2729
It 'should perform synthetic test' {
@@ -52,5 +54,6 @@ Describe 'osinfo resource tests' {
5254
elseif ($IsMacOS) {
5355
$out.resources[0].properties.family | Should -BeExactly 'macOS'
5456
}
57+
$out.resources[0].name | Should -BeExactly "$($out.resources[0].properties.family) $($out.resources[0].properties.version) $($out.resources[0].properties.architecture)"
5558
}
5659
}

0 commit comments

Comments
 (0)