From 4afaf45f559a92c71fc762653b899f983c85d97b Mon Sep 17 00:00:00 2001 From: Dexter Chua Date: Wed, 2 Sep 2020 20:53:53 +0800 Subject: [PATCH] Make url parameter optional when generating registration The bridge may instead choose to let the user configure the url. Fixes #182 Signed-off-by: Dexter Chua --- changelog.d/217.feature | 1 + src/components/cli.ts | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 changelog.d/217.feature diff --git a/changelog.d/217.feature b/changelog.d/217.feature new file mode 100644 index 00000000..59d8addb --- /dev/null +++ b/changelog.d/217.feature @@ -0,0 +1 @@ +Make url parameter optional when generating registration. diff --git a/src/components/cli.ts b/src/components/cli.ts index dac52cae..61fe1881 100644 --- a/src/components/cli.ts +++ b/src/components/cli.ts @@ -38,12 +38,13 @@ interface CliOpts> { enableRegistration?: boolean; enableLocalpart: boolean; port: number; + urlFromConfig?: boolean; } interface CliArgs { "generate-registration": boolean; config: string; - url: string; + url?: string; localpart: string; port: number; file: string; @@ -69,6 +70,8 @@ export class Cli> { * (string) or the parsed schema file (Object). * @param opts.bridgeConfig.defaults The default options for the * config file. + * @param opts.urlFromConfig Appservice url is set in config file instead + * of cli parameter. * @param opts.enableRegistration Enable '--generate-registration'. * Default True. * @param opts.registrationPath The path to write the registration @@ -147,11 +150,16 @@ export class Cli> { } if (this.opts.enableRegistration && this.args["generate-registration"]) { - if (!this.args.url) { + if (!this.args.url && !this.opts.urlFromConfig) { this.printHelp(); console.log("Missing --url [-u]"); process.exit(1); } + else if (this.args.url && this.opts.urlFromConfig) { + this.printHelp(); + console.log("--url [-u] is not valid option for this bridge."); + process.exit(1); + } if (this.args.port) { this.printHelp(); console.log("--port [-p] is not valid when generating a registration file."); @@ -210,11 +218,8 @@ export class Cli> { return validator.validate(cfg, this.opts.bridgeConfig.defaults) as ConfigType; } - private generateRegistration(appServiceUrl: string, localpart: string) { - if (!appServiceUrl) { - throw Error("Missing app service URL"); - } - let reg = new AppServiceRegistration(appServiceUrl); + private generateRegistration(appServiceUrl: string | undefined, localpart: string) { + let reg = new AppServiceRegistration(appServiceUrl || ""); if (localpart) { reg.setSenderLocalpart(localpart); } @@ -256,8 +261,10 @@ export class Cli> { if (this.opts.enableRegistration) { help["--generate-registration -r"] = "Create a registration YAML file " + "for this application service"; - help["--url -u"] = "Registration Option. Required if -r is set. The URL " + - "where the application service is listening for HS requests"; + if (!this.opts.urlFromConfig) { + help["--url -u"] = "Registration Option. Required if -r is set. The " + + "URL where the application service is listening for HS requests"; + } if (this.opts.enableLocalpart) { help["--localpart -l"] = "Registration Option. Valid if -r is set. " + "The user_id localpart to assign to the AS.";