Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validates short options. Closes #5657 #5661

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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