Skip to content

Commit

Permalink
Validates short options. Closes #5657
Browse files Browse the repository at this point in the history
  • Loading branch information
waldekmastykarz committed Nov 10, 2023
1 parent 11c70fc commit b1eb2df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
27 changes: 14 additions & 13 deletions src/cli/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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<string | undefined>(settingsNames.output, 'json');
if (this.optionsFromArgs.options.output === undefined) {
this.optionsFromArgs.options.output = this.getSettingWithDefaultValue<string | undefined>(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);
Expand All @@ -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 */
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b1eb2df

Please sign in to comment.