2023-01-31 22:08:37 +01:00
|
|
|
import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction";
|
2018-05-14 20:54:19 +02:00
|
|
|
|
2022-11-18 13:20:19 +01:00
|
|
|
import { Response } from "../models/response";
|
|
|
|
import { MessageResponse } from "../models/response/message.response";
|
|
|
|
import { StringResponse } from "../models/response/string.response";
|
2022-09-29 16:38:50 +02:00
|
|
|
import { CliUtils } from "../utils";
|
2022-01-19 16:45:14 +01:00
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
export class SyncCommand {
|
2018-05-14 19:37:52 +02:00
|
|
|
constructor(private syncService: SyncService) {}
|
2018-05-14 17:15:54 +02:00
|
|
|
|
2022-01-19 16:45:14 +01:00
|
|
|
async run(cmdOptions: Record<string, any>): Promise<Response> {
|
|
|
|
const normalizedOptions = new Options(cmdOptions);
|
|
|
|
if (normalizedOptions.last) {
|
2018-05-16 19:53:12 +02:00
|
|
|
return await this.getLastSync();
|
2018-05-14 17:15:54 +02:00
|
|
|
}
|
2018-05-16 19:53:12 +02:00
|
|
|
|
|
|
|
try {
|
2022-03-03 18:24:41 +01:00
|
|
|
await this.syncService.fullSync(normalizedOptions.force, true);
|
2018-05-16 19:53:12 +02:00
|
|
|
const res = new MessageResponse("Syncing complete.", null);
|
|
|
|
return Response.success(res);
|
2018-05-14 17:15:54 +02:00
|
|
|
} catch (e) {
|
2020-11-20 16:17:09 +01:00
|
|
|
return Response.error("Syncing failed: " + e.toString());
|
2018-05-16 19:53:12 +02:00
|
|
|
}
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2018-05-16 19:53:12 +02:00
|
|
|
private async getLastSync() {
|
2021-02-03 18:44:33 +01:00
|
|
|
const lastSyncDate = await this.syncService.getLastSync();
|
2018-05-16 19:53:12 +02:00
|
|
|
const res = new StringResponse(lastSyncDate == null ? null : lastSyncDate.toISOString());
|
|
|
|
return Response.success(res);
|
2021-12-20 18:04:00 +01:00
|
|
|
}
|
2018-05-14 17:15:54 +02:00
|
|
|
}
|
2022-01-19 16:45:14 +01:00
|
|
|
|
|
|
|
class Options {
|
|
|
|
last: boolean;
|
|
|
|
force: boolean;
|
|
|
|
|
|
|
|
constructor(passedOptions: Record<string, any>) {
|
2022-01-26 17:28:56 +01:00
|
|
|
this.last = CliUtils.convertBooleanOption(passedOptions?.last);
|
|
|
|
this.force = CliUtils.convertBooleanOption(passedOptions?.force);
|
2022-01-19 16:45:14 +01:00
|
|
|
}
|
|
|
|
}
|