From a44cf5f8244bcabf427337c1fd71ea713ee8dca6 Mon Sep 17 00:00:00 2001 From: Pierre Gimalac Date: Tue, 2 May 2023 11:43:50 +0200 Subject: [PATCH] fix: return default values for fields that were removed (#172) --- cpu/cpu_windows.go | 26 ++++++++++++++++++++------ platform/platform_windows.go | 19 +++++++++++++------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 5b7c052..f19b3ca 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -218,8 +218,25 @@ func getSystemInfo() (si SYSTEM_INFO) { } // GetCpuInfo returns map of interesting bits of information about the CPU -func GetCpuInfo() (cpuInfo map[string]string, err error) { - cpuInfo = make(map[string]string) +func GetCpuInfo() (map[string]string, error) { + // Initialize cpuInfo with all fields to avoid missing a field which + // could be expected by the backend or by users + // TODO: make sure that the backend actually works with any subset of fields + cpuInfo := map[string]string{ + "mhz": "0", + "model_name": "", + "vendor_id": "", + "family": "", + "cpu_pkgs": "0", + "cpu_numa_nodes": "0", + "cpu_cores": "0", + "cpu_logical_processors": "0", + "cache_size_l1": "0", + "cache_size_l2": "0", + "cache_size_l3": "0", + "model": "0", + "stepping": "0", + } k, err := registry.OpenKey(registry.LOCAL_MACHINE, registryHive, @@ -266,10 +283,7 @@ func GetCpuInfo() (cpuInfo map[string]string, err error) { cpuInfo["model"] = strconv.Itoa(int((si.wProcessorRevision >> 8) & 0xFF)) cpuInfo["stepping"] = strconv.Itoa(int(si.wProcessorRevision & 0xFF)) - // cpuInfo cannot be empty - err = nil - - return + return cpuInfo, nil } func extract(caption, field string) string { diff --git a/platform/platform_windows.go b/platform/platform_windows.go index f2ab53f..4171596 100644 --- a/platform/platform_windows.go +++ b/platform/platform_windows.go @@ -295,8 +295,18 @@ func getNativeArchInfo() string { } // GetArchInfo returns basic host architecture information -func GetArchInfo() (systemInfo map[string]string, err error) { - systemInfo = map[string]string{} +func GetArchInfo() (map[string]string, error) { + // Initialize systemInfo with all fields to avoid missing a field which + // could be expected by the backend or by users + // TODO: make sure that the backend actually works with any subset of fields + systemInfo := map[string]string{ + "hostname": "", + "machine": "", + "os": "", + "kernel_release": "0.0.0", + "kernel_name": "", + "family": "", + } hostname, err := os.Hostname() if err == nil { @@ -343,8 +353,5 @@ func GetArchInfo() (systemInfo map[string]string, err error) { } systemInfo["family"] = family - // systemInfo is never empty so we never return an error - err = nil - - return + return systemInfo, nil }