From b1eb2dfaa1e304fe9e95726a85445a1b17e7d42f Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Fri, 10 Nov 2023 20:11:29 +0100 Subject: [PATCH] Validates short options. Closes #5657 --- src/cli/Cli.ts | 27 ++++++++++++++------------- src/utils/validation.ts | 7 +++++-- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/cli/Cli.ts b/src/cli/Cli.ts index 43f8c939259..a2ab8ddf6ad 100644 --- a/src/cli/Cli.ts +++ b/src/cli/Cli.ts @@ -142,22 +142,23 @@ export class Cli { return Promise.resolve(); } - const optionsWithoutShorts = Cli.removeShortOptions(this.optionsFromArgs); + delete (this.optionsFromArgs.options as any)._; + delete (this.optionsFromArgs.options as any)['--']; try { // replace values staring with @ with file contents - Cli.loadOptionValuesFromFiles(optionsWithoutShorts); + Cli.loadOptionValuesFromFiles(this.optionsFromArgs); } catch (e) { - return this.closeWithError(e, optionsWithoutShorts); + return this.closeWithError(e, this.optionsFromArgs); } const startProcessing = process.hrtime.bigint(); try { // process options before passing them on to validation stage - const contextCommandOptions = await this.loadOptionsFromContext(this.commandToExecute.options, optionsWithoutShorts.options.debug); - optionsWithoutShorts.options = { ...contextCommandOptions, ...optionsWithoutShorts.options }; - await this.commandToExecute.command.processOptions(optionsWithoutShorts.options); + const contextCommandOptions = await this.loadOptionsFromContext(this.commandToExecute.options, this.optionsFromArgs.options.debug); + this.optionsFromArgs.options = { ...contextCommandOptions, ...this.optionsFromArgs.options }; + await this.commandToExecute.command.processOptions(this.optionsFromArgs.options); const endProcessing = process.hrtime.bigint(); timings.options.push(Number(endProcessing - startProcessing)); @@ -166,27 +167,27 @@ export class Cli { const endProcessing = process.hrtime.bigint(); timings.options.push(Number(endProcessing - startProcessing)); - return this.closeWithError(e.message, optionsWithoutShorts, false); + return this.closeWithError(e.message, this.optionsFromArgs, false); } // if output not specified, set the configured output value (if any) - if (optionsWithoutShorts.options.output === undefined) { - optionsWithoutShorts.options.output = this.getSettingWithDefaultValue(settingsNames.output, 'json'); + if (this.optionsFromArgs.options.output === undefined) { + this.optionsFromArgs.options.output = this.getSettingWithDefaultValue(settingsNames.output, 'json'); } const startValidation = process.hrtime.bigint(); - const validationResult = await this.commandToExecute.command.validate(optionsWithoutShorts, this.commandToExecute); + const validationResult = await this.commandToExecute.command.validate(this.optionsFromArgs, this.commandToExecute); const endValidation = process.hrtime.bigint(); timings.validation.push(Number(endValidation - startValidation)); if (validationResult !== true) { - return this.closeWithError(validationResult, optionsWithoutShorts, true); + return this.closeWithError(validationResult, this.optionsFromArgs, true); } const end = process.hrtime.bigint(); timings.core.push(Number(end - start)); try { - await Cli.executeCommand(this.commandToExecute.command, optionsWithoutShorts); + await Cli.executeCommand(this.commandToExecute.command, this.optionsFromArgs); const endTotal = process.hrtime.bigint(); timings.total.push(Number(endTotal - start)); this.printTimings(rawArgs); @@ -196,7 +197,7 @@ export class Cli { const endTotal = process.hrtime.bigint(); timings.total.push(Number(endTotal - start)); this.printTimings(rawArgs); - await this.closeWithError(err, optionsWithoutShorts); + await this.closeWithError(err, this.optionsFromArgs); /* c8 ignore next */ } } diff --git a/src/utils/validation.ts b/src/utils/validation.ts index ede7bc85900..525b893a6d5 100644 --- a/src/utils/validation.ts +++ b/src/utils/validation.ts @@ -12,8 +12,11 @@ export const validation = { const guidRegEx: RegExp = new RegExp(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i); - // verify if the guid is a valid guid. @meid will be replaced in a later stage with the actual user id of the logged in user - return guidRegEx.test(guid) || guid.toLowerCase().trim() === '@meid'; + // verify if the guid is a valid guid. @meid will be replaced in a later + // stage with the actual user id of the logged in user + // we also need to make it toString in case the args is resolved as number + // or boolean + return guidRegEx.test(guid) || guid.toString().toLowerCase().trim() === '@meid'; }, isValidTeamsChannelId(guid: string): boolean {