2018-05-17 06:07:44 +02:00
|
|
|
import * as program from "commander";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { EnvironmentService } from "@bitwarden/common/abstractions/environment.service";
|
|
|
|
import { Response } from "@bitwarden/node/cli/models/response";
|
|
|
|
import { MessageResponse } from "@bitwarden/node/cli/models/response/messageResponse";
|
|
|
|
import { StringResponse } from "@bitwarden/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);
|
2021-12-20 18:04:00 +01:00
|
|
|
default:
|
2018-05-17 06:07:44 +02:00
|
|
|
return Response.badRequest("Unknown setting.");
|
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2018-05-17 06:07:44 +02:00
|
|
|
|
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
|
|
|
) {
|
2019-10-11 03:23:56 +02:00
|
|
|
const stringRes = new StringResponse(
|
2018-05-17 06:07:44 +02:00
|
|
|
this.environmentService.hasBaseUrl()
|
|
|
|
? this.environmentService.getUrls().base
|
2021-02-03 18:44:33 +01:00
|
|
|
: "https://bitwarden.com"
|
2018-05-17 06:07:44 +02:00
|
|
|
);
|
2019-10-11 03:23:56 +02:00
|
|
|
return Response.success(stringRes);
|
2018-05-17 06:07:44 +02:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
|
2018-05-17 06:07:44 +02:00
|
|
|
url = url === "null" || url === "bitwarden.com" || url === "https://bitwarden.com" ? null : url;
|
2021-07-23 22:46:32 +02:00
|
|
|
await this.environmentService.setUrls({
|
2018-05-17 06:07:44 +02:00
|
|
|
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,
|
2021-11-09 19:00:16 +01:00
|
|
|
keyConnector: options.keyConnector || null,
|
2021-12-20 18:04:00 +01:00
|
|
|
});
|
2019-10-11 03:23:56 +02:00
|
|
|
const res = new MessageResponse("Saved setting `config`.", null);
|
|
|
|
return Response.success(res);
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2018-05-17 06:07:44 +02:00
|
|
|
}
|