Normalize v prefixes in the --version parameter of all commands

Change-type: patch
This commit is contained in:
Thodoris Greasidis 2024-01-02 12:52:13 +02:00
parent 801a25995c
commit b7b01ecd53
6 changed files with 28 additions and 12 deletions

View File

@ -259,6 +259,8 @@ export default class ConfigGenerateCmd extends Command {
if (!options.fleet && options.deviceType) { if (!options.fleet && options.deviceType) {
throw new ExpectedError(this.deviceTypeNotAllowedMessage); throw new ExpectedError(this.deviceTypeNotAllowedMessage);
} }
const { normalizeOsVersion } = await import('../../utils/normalization');
options.version = normalizeOsVersion(options.version);
const { validateDevOptionAndWarn } = await import('../../utils/config'); const { validateDevOptionAndWarn } = await import('../../utils/config');
await validateDevOptionAndWarn(options.dev, options.version); await validateDevOptionAndWarn(options.dev, options.version);
} }

View File

@ -100,6 +100,8 @@ export default class DeviceOsUpdateCmd extends Command {
// Get target OS version // Get target OS version
let targetOsVersion = options.version; let targetOsVersion = options.version;
if (targetOsVersion != null) { if (targetOsVersion != null) {
const { normalizeOsVersion } = await import('../../utils/normalization');
targetOsVersion = normalizeOsVersion(targetOsVersion);
if (!hupVersionInfo.versions.includes(targetOsVersion)) { if (!hupVersionInfo.versions.includes(targetOsVersion)) {
throw new ExpectedError( throw new ExpectedError(
`The provided version ${targetOsVersion} is not in the Host OS update targets for this device`, `The provided version ${targetOsVersion} is not in the Host OS update targets for this device`,

View File

@ -216,9 +216,15 @@ export default class OsConfigureCmd extends Command {
configJson = JSON.parse(rawConfig); configJson = JSON.parse(rawConfig);
} }
const osVersion = const { normalizeOsVersion } = await import('../../utils/normalization');
const osVersion = normalizeOsVersion(
options.version || options.version ||
(await getOsVersionFromImage(params.image, deviceTypeManifest, devInit)); (await getOsVersionFromImage(
params.image,
deviceTypeManifest,
devInit,
)),
);
const { validateDevOptionAndWarn } = await import('../../utils/config'); const { validateDevOptionAndWarn } = await import('../../utils/config');
await validateDevOptionAndWarn(options.dev, osVersion); await validateDevOptionAndWarn(options.dev, osVersion);

View File

@ -202,12 +202,8 @@ async function resolveOSVersion(
if (['menu', 'menu-esr'].includes(version)) { if (['menu', 'menu-esr'].includes(version)) {
return await selectOSVersionFromMenu(deviceType, version === 'menu-esr'); return await selectOSVersionFromMenu(deviceType, version === 'menu-esr');
} }
// Note that `version` may also be 'latest', 'recommended', 'default' const { normalizeOsVersion } = await import('./normalization');
if (/^v?\d+\.\d+\.\d+/.test(version)) { version = normalizeOsVersion(version);
if (version[0] === 'v') {
version = version.slice(1);
}
}
return version; return version;
} }

View File

@ -184,9 +184,9 @@ export async function validateDevOptionAndWarn(
* option. * option.
*/ */
export async function validateSecureBootOptionAndWarn( export async function validateSecureBootOptionAndWarn(
secureBoot?: boolean, secureBoot: boolean,
slug?: string, slug: string,
version?: string, version: string,
logger?: import('./logger'), logger?: import('./logger'),
) { ) {
if (!secureBoot) { if (!secureBoot) {
@ -202,7 +202,7 @@ export async function validateSecureBootOptionAndWarn(
const sdk = getBalenaSdk(); const sdk = getBalenaSdk();
const [osRelease] = await sdk.models.os.getAllOsVersions(slug, { const [osRelease] = await sdk.models.os.getAllOsVersions(slug, {
$select: 'contract', $select: 'contract',
$filter: { raw_version: `${version.replace(/^v/, '')}` }, $filter: { raw_version: version },
}); });
if (!osRelease) { if (!osRelease) {
throw new ExpectedError(`Error: No ${version} release for ${slug}`); throw new ExpectedError(`Error: No ${version} release for ${slug}`);

View File

@ -81,3 +81,13 @@ export async function disambiguateReleaseParam(
export async function lowercaseIfSlug(s: string) { export async function lowercaseIfSlug(s: string) {
return s.includes('/') ? s.toLowerCase() : s; return s.includes('/') ? s.toLowerCase() : s;
} }
export function normalizeOsVersion(version: string) {
// Note that `version` may also be 'latest', 'recommended', 'default'
if (/^v?\d+\.\d+\.\d+/.test(version)) {
if (version[0] === 'v') {
version = version.slice(1);
}
}
return version;
}