Introduces decorators to handle multiple authorities for URIs (#10507)

* Introduces decorators to handle multiple authorities for URIs

* Check for undefined first

* Add result type checking
This commit is contained in:
Amir Omidi
2020-05-26 16:32:25 -07:00
committed by GitHub
parent 9a55b0275d
commit 8f33e1c4c0

View File

@@ -32,8 +32,6 @@ import { find } from 'vs/base/common/arrays';
import { INativeEnvironmentService } from 'vs/platform/environment/node/environmentService';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
const connectAuthority = 'connect';
export interface SqlArgs {
_?: string[];
authenticationType?: string
@@ -44,6 +42,25 @@ export interface SqlArgs {
provider?: string;
}
//#region decorators
type PathHandler = (uri: URI) => Promise<boolean>;
const pathMappings: { [key: string]: PathHandler } = {};
interface PathHandlerOptions {
path: string
}
function pathHandler({ path }: PathHandlerOptions) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const method: PathHandler = descriptor.value;
pathMappings[path] = method;
};
}
//#endregion
export class CommandLineWorkbenchContribution implements IWorkbenchContribution, IURLHandler {
constructor(
@@ -152,27 +169,42 @@ export class CommandLineWorkbenchContribution implements IWorkbenchContribution,
}
public async handleURL(uri: URI): Promise<boolean> {
// Catch file URLs
let authority = uri.authority.toLowerCase();
if (authority === connectAuthority) {
try {
let args = this.parseProtocolArgs(uri);
if (!args.server) {
this._notificationService.warn(localize('warnServerRequired', "Cannot connect as no server information was provided"));
return true;
}
let isOpenOk = await this.confirmConnect(args);
if (isOpenOk) {
await this.processCommandLine(args);
}
} catch (err) {
this._notificationService.error(localize('errConnectUrl', "Could not open URL due to error {0}", getErrorMessage(err)));
}
// Handled either way
return true;
let key = uri.authority.toLowerCase();
let method = pathMappings[key];
if (!method) {
return false;
}
method = method.bind(this);
const result = await method(uri);
if (typeof result !== 'boolean') {
throw new Error('Invalid URL Handler used in commandLine code.');
}
return false;
return result;
}
@pathHandler({
path: 'connect'
})
public async handleConnect(uri: URI): Promise<boolean> {
try {
let args = this.parseProtocolArgs(uri);
if (!args.server) {
this._notificationService.warn(localize('warnServerRequired', "Cannot connect as no server information was provided"));
return true;
}
let isOpenOk = await this.confirmConnect(args);
if (isOpenOk) {
await this.processCommandLine(args);
}
} catch (err) {
this._notificationService.error(localize('errConnectUrl', "Could not open URL due to error {0}", getErrorMessage(err)));
}
// Handled either way
return true;
}
private async confirmConnect(args: SqlArgs): Promise<boolean> {