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