diff --git a/src/sql/workbench/contrib/commandLine/electron-browser/commandLine.ts b/src/sql/workbench/contrib/commandLine/electron-browser/commandLine.ts index 0ede40229d..04f6557e9c 100644 --- a/src/sql/workbench/contrib/commandLine/electron-browser/commandLine.ts +++ b/src/sql/workbench/contrib/commandLine/electron-browser/commandLine.ts @@ -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; + +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 { - // 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 { + 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 {