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 6775e3a commit 706f6df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
34 changes: 21 additions & 13 deletions src/cli/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,39 +146,42 @@ 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);
}

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);
}
catch (e: any) {
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 validationResult = await this.commandToExecute.command.validate(optionsWithoutShorts, this.commandToExecute);
const validationResult = await this.commandToExecute.command.validate(this.optionsFromArgs, this.commandToExecute);
if (validationResult !== true) {
return this.closeWithError(validationResult, optionsWithoutShorts, true);
return this.closeWithError(validationResult, this.optionsFromArgs, true);
}

this.optionsFromArgs = Cli.removeShortOptions(this.optionsFromArgs);

return Cli
.executeCommand(this.commandToExecute.command, optionsWithoutShorts)
.then(_ => process.exit(0), err => this.closeWithError(err, optionsWithoutShorts));
.executeCommand(this.commandToExecute.command, this.optionsFromArgs)
.then(_ => process.exit(0), err => this.closeWithError(err, this.optionsFromArgs!));
}

public static async executeCommand(command: Command, args: { options: minimist.ParsedArgs }): Promise<void> {
Expand Down Expand Up @@ -1040,6 +1043,11 @@ export class Cli {
private static loadOptionValuesFromFiles(args: { options: minimist.ParsedArgs }): void {
const optionNames: string[] = Object.getOwnPropertyNames(args.options);
optionNames.forEach(option => {
// skip shorts because we'll be removing them anyway
if (option.length === 1) {
return;
}

const value = args.options[option];
if (!value ||
typeof value !== 'string' ||
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 706f6df

Please sign in to comment.