From cd56eca96bc45cb0a919d2c7237da14fac72e351 Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Fri, 19 Apr 2024 11:43:40 -0400 Subject: [PATCH] Collect some specific lsb_release info --- dist/index.js | 39 ++++++++++++++++++++++------- dist/platform.js | 39 ++++++++++++++++++++++------- package.json | 2 +- src/platform.ts | 65 +++++++++++++++++++++++++++++++++++++----------- 4 files changed, 112 insertions(+), 33 deletions(-) diff --git a/dist/index.js b/dist/index.js index 3fe2db9..66af063 100644 --- a/dist/index.js +++ b/dist/index.js @@ -95296,6 +95296,7 @@ var linux_release_info_dist = __nccwpck_require__(7540); + const getWindowsInfo = async () => { const { stdout: version } = await exec.getExecOutput('powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Version"', undefined, { silent: true, @@ -95319,17 +95320,37 @@ const getMacOsInfo = async () => { version, }; }; +function getPropertyViaWithDefault(data, names, defaultValue) { + for (const name of names) { + const ret = getPropertyWithDefault(data, name, undefined); + if (ret !== undefined) { + return ret; + } + } + return defaultValue; +} +function getPropertyWithDefault(data, name, defaultValue) { + if (!data.hasOwnProperty(name)) { + return defaultValue; + } + const value = data[name]; + // NB. this check won't work for object instances + if (typeof value !== typeof defaultValue) { + return defaultValue; + } + return value; +} const getLinuxInfo = async () => { - const data = (0,linux_release_info_dist/* releaseInfo */.o)({ mode: "sync" }); - // eslint-disable-next-line no-console - console.log(data); - const { stdout } = await exec.getExecOutput("lsb_release", ["-i", "-r", "-s"], { - silent: true, - }); - const [name, version] = stdout.trim().split("\n"); + let data = {}; + try { + data = (0,linux_release_info_dist/* releaseInfo */.o)({ mode: "sync" }); + } + catch (e) { + core.debug(`Error collecting release info: ${e}`); + } return { - name, - version, + name: getPropertyViaWithDefault(data, ["id", "name", "pretty_name", "id_like"], "unknown"), + version: getPropertyViaWithDefault(data, ["version_id", "version", "version_codename"], "unknown"), }; }; const platform = external_os_.platform(); diff --git a/dist/platform.js b/dist/platform.js index a5b6ce7..72ef20c 100644 --- a/dist/platform.js +++ b/dist/platform.js @@ -2,6 +2,7 @@ // since it isn't in @actions/core 1.10.1 which is their current release as 2024-04-19 import os from "os"; import * as exec from "@actions/exec"; +import * as core from "@actions/core"; import { releaseInfo } from "linux-release-info"; const getWindowsInfo = async () => { const { stdout: version } = await exec.getExecOutput('powershell -command "(Get-CimInstance -ClassName Win32_OperatingSystem).Version"', undefined, { @@ -26,17 +27,37 @@ const getMacOsInfo = async () => { version, }; }; +function getPropertyViaWithDefault(data, names, defaultValue) { + for (const name of names) { + const ret = getPropertyWithDefault(data, name, undefined); + if (ret !== undefined) { + return ret; + } + } + return defaultValue; +} +function getPropertyWithDefault(data, name, defaultValue) { + if (!data.hasOwnProperty(name)) { + return defaultValue; + } + const value = data[name]; + // NB. this check won't work for object instances + if (typeof value !== typeof defaultValue) { + return defaultValue; + } + return value; +} const getLinuxInfo = async () => { - const data = releaseInfo({ mode: "sync" }); - // eslint-disable-next-line no-console - console.log(data); - const { stdout } = await exec.getExecOutput("lsb_release", ["-i", "-r", "-s"], { - silent: true, - }); - const [name, version] = stdout.trim().split("\n"); + let data = {}; + try { + data = releaseInfo({ mode: "sync" }); + } + catch (e) { + core.debug(`Error collecting release info: ${e}`); + } return { - name, - version, + name: getPropertyViaWithDefault(data, ["id", "name", "pretty_name", "id_like"], "unknown"), + version: getPropertyViaWithDefault(data, ["version_id", "version", "version_codename"], "unknown"), }; }; export const platform = os.platform(); diff --git a/package.json b/package.json index 5e6efbe..4117053 100644 --- a/package.json +++ b/package.json @@ -50,4 +50,4 @@ "prettier": "^3.2.5", "typescript": "^5.4.5" } -} \ No newline at end of file +} diff --git a/src/platform.ts b/src/platform.ts index 454de5e..7a8753a 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -3,6 +3,7 @@ import os from "os"; import * as exec from "@actions/exec"; +import * as core from "@actions/core"; import { releaseInfo } from "linux-release-info"; const getWindowsInfo = async (): Promise<{ name: string; version: string }> => { @@ -45,27 +46,63 @@ const getMacOsInfo = async (): Promise<{ }; }; +function getPropertyViaWithDefault( + data: object, + names: Property[], + defaultValue: T, +): T { + for (const name of names) { + const ret: T | undefined = getPropertyWithDefault(data, name, undefined); + if (ret !== undefined) { + return ret; + } + } + + return defaultValue; +} + +function getPropertyWithDefault( + data: object, + name: Property, + defaultValue: T, +): T { + if (!data.hasOwnProperty(name)) { + return defaultValue; + } + + const value = (data as { [K in Property]: T })[name]; + + // NB. this check won't work for object instances + if (typeof value !== typeof defaultValue) { + return defaultValue; + } + + return value; +} + const getLinuxInfo = async (): Promise<{ name: string; version: string; }> => { - const data = releaseInfo({ mode: "sync" }); - // eslint-disable-next-line no-console - console.log(data); + let data: object = {}; - const { stdout } = await exec.getExecOutput( - "lsb_release", - ["-i", "-r", "-s"], - { - silent: true, - }, - ); - - const [name, version] = stdout.trim().split("\n"); + try { + data = releaseInfo({ mode: "sync" }); + } catch (e) { + core.debug(`Error collecting release info: ${e}`); + } return { - name, - version, + name: getPropertyViaWithDefault( + data, + ["id", "name", "pretty_name", "id_like"], + "unknown", + ), + version: getPropertyViaWithDefault( + data, + ["version_id", "version", "version_codename"], + "unknown", + ), }; };