2018-05-17 06:07:44 +02:00
|
|
|
import * as program from 'commander';
|
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
|
2018-05-17 06:07:44 +02:00
|
|
|
|
2021-06-07 19:25:55 +02:00
|
|
|
import { Response } from 'jslib-node/cli/models/response';
|
|
|
|
import { MessageResponse } from 'jslib-node/cli/models/response/messageResponse';
|
|
|
|
import { StringResponse } from 'jslib-node/cli/models/response/stringResponse';
|
2018-05-17 06:07:44 +02:00
|
|
|
|
|
|
|
export class ConfigCommand {
|
|
|
|
constructor(private environmentService: EnvironmentService) { }
|
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
async run(setting: string, value: string, options: program.OptionValues): Promise<Response> {
|
2018-05-17 06:07:44 +02:00
|
|
|
setting = setting.toLowerCase();
|
|
|
|
switch (setting) {
|
|
|
|
case 'server':
|
2021-02-03 18:44:33 +01:00
|
|
|
return await this.getOrSetServer(value, options);
|
2018-05-17 06:07:44 +02:00
|
|
|
default:
|
|
|
|
return Response.badRequest('Unknown setting.');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:44:33 +01:00
|
|
|
private async getOrSetServer(url: string, options: program.OptionValues): Promise<Response> {
|
2020-04-14 19:04:19 +02:00
|
|
|
if ((url == null || url.trim() === '') &&
|
2021-02-03 18:44:33 +01:00
|
|
|
!options.webVault && !options.api && !options.identity && !options.icons && !options.notifications && !options.events) {
|
2021-07-23 22:46:32 +02:00
|
|
|
const stringRes = new StringResponse(
|
|
|
|
this.environmentService.hasBaseUrl() ? this.environmentService.getUrls().base : 'https://bitwarden.com'
|
|
|
|
);
|
2019-10-11 03:23:56 +02:00
|
|
|
return Response.success(stringRes);
|
|
|
|
}
|
|
|
|
|
2018-05-17 06:07:44 +02:00
|
|
|
url = (url === 'null' || url === 'bitwarden.com' || url === 'https://bitwarden.com' ? null : url);
|
|
|
|
await this.environmentService.setUrls({
|
|
|
|
base: url,
|
2021-02-03 18:44:33 +01:00
|
|
|
webVault: options.webVault || null,
|
|
|
|
api: options.api || null,
|
|
|
|
identity: options.identity || null,
|
|
|
|
icons: options.icons || null,
|
|
|
|
notifications: options.notifications || null,
|
|
|
|
events: options.events || null,
|
2018-05-17 06:07:44 +02:00
|
|
|
});
|
2019-10-11 03:23:56 +02:00
|
|
|
const res = new MessageResponse('Saved setting `config`.', null);
|
|
|
|
return Response.success(res);
|
2018-05-17 06:07:44 +02:00
|
|
|
}
|
|
|
|
}
|