2018-05-17 06:07:44 +02:00
|
|
|
import * as program from 'commander';
|
|
|
|
|
|
|
|
import { EnvironmentService } from 'jslib/abstractions/environment.service';
|
|
|
|
|
2019-03-16 03:34:59 +01:00
|
|
|
import { Response } from 'jslib/cli/models/response';
|
|
|
|
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
|
2019-10-11 03:23:56 +02:00
|
|
|
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
|
2018-05-17 06:07:44 +02:00
|
|
|
|
|
|
|
export class ConfigCommand {
|
|
|
|
constructor(private environmentService: EnvironmentService) { }
|
|
|
|
|
|
|
|
async run(setting: string, value: string, cmd: program.Command): Promise<Response> {
|
|
|
|
setting = setting.toLowerCase();
|
|
|
|
switch (setting) {
|
|
|
|
case 'server':
|
2020-04-14 19:04:19 +02:00
|
|
|
return await this.getOrSetServer(value, cmd);
|
2018-05-17 06:07:44 +02:00
|
|
|
default:
|
|
|
|
return Response.badRequest('Unknown setting.');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-14 19:04:19 +02:00
|
|
|
private async getOrSetServer(url: string, cmd: program.Command): Promise<Response> {
|
|
|
|
if ((url == null || url.trim() === '') &&
|
|
|
|
!cmd.webVault && !cmd.api && !cmd.identity && !cmd.icons && !cmd.notifications && !cmd.events) {
|
2019-10-11 03:23:56 +02:00
|
|
|
const baseUrl = this.environmentService.baseUrl;
|
|
|
|
const stringRes = new StringResponse(baseUrl == null ? 'https://bitwarden.com' : baseUrl);
|
|
|
|
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,
|
2020-04-14 19:04:19 +02:00
|
|
|
webVault: cmd.webVault || null,
|
|
|
|
api: cmd.api || null,
|
|
|
|
identity: cmd.identity || null,
|
|
|
|
icons: cmd.icons || null,
|
|
|
|
notifications: cmd.notifications || null,
|
|
|
|
events: cmd.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
|
|
|
}
|
|
|
|
}
|