diff --git a/jslib b/jslib index 13a160fb..b5b4222b 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 13a160fb795a69bad1edbb3fc5fd5c7c15396e03 +Subproject commit b5b4222b325a5fab7fabfd53f23de1876ffccec8 diff --git a/src/commands/config.command.ts b/src/commands/config.command.ts index 28ca5916..48980a21 100644 --- a/src/commands/config.command.ts +++ b/src/commands/config.command.ts @@ -1,12 +1,13 @@ import * as program from 'commander'; import { EnvironmentService } from 'jslib/abstractions/environment.service'; +import { I18nService } from 'jslib/abstractions/i18n.service'; import { Response } from 'jslib/cli/models/response'; import { MessageResponse } from 'jslib/cli/models/response/messageResponse'; export class ConfigCommand { - constructor(private environmentService: EnvironmentService) { } + constructor(private environmentService: EnvironmentService, private i18nService: I18nService) { } async run(setting: string, value: string, cmd: program.Command): Promise { setting = setting.toLowerCase(); @@ -18,7 +19,7 @@ export class ConfigCommand { return Response.badRequest('Unknown setting.'); } - const res = new MessageResponse('Saved setting `' + setting + '`.', null); + const res = new MessageResponse(this.i18nService.t('savedSetting', setting), null); return Response.success(res); } diff --git a/src/commands/sync.command.ts b/src/commands/sync.command.ts new file mode 100644 index 00000000..7dc3edf2 --- /dev/null +++ b/src/commands/sync.command.ts @@ -0,0 +1,25 @@ +import * as program from 'commander'; + +import { I18nService } from 'jslib/abstractions/i18n.service'; + +import { SyncService } from '../services/sync.service'; + +import { Response } from 'jslib/cli/models/response'; +import { MessageResponse } from 'jslib/cli/models/response/messageResponse'; + +export class SyncCommand { + constructor(private syncService: SyncService, private i18nService: I18nService) { } + + async run(cmd: program.Command): Promise { + try { + const result = await this.syncService.sync(false, false); + const groupCount = result[0] != null ? result[0].length : 0; + const userCount = result[1] != null ? result[1].length : 0; + const res = new MessageResponse(this.i18nService.t('syncingComplete'), + this.i18nService.t('syncCounts', groupCount.toString(), userCount.toString())); + return Response.success(res); + } catch (e) { + return Response.error(e); + } + } +} diff --git a/src/locales/en/messages.json b/src/locales/en/messages.json index b4900c75..fa328b67 100644 --- a/src/locales/en/messages.json +++ b/src/locales/en/messages.json @@ -516,6 +516,9 @@ "noGroups": { "message": "No groups to list." }, + "syncingComplete": { + "message": "Syncing complete." + }, "syncingStarted": { "message": "Syncing started." }, @@ -565,5 +568,14 @@ }, "hideToTray": { "message": "Hide to Tray" + }, + "savedSetting": { + "message": "Saved setting `$SETTING_NAME$`.", + "placeholders": { + "setting_name": { + "content": "$1", + "example": "server" + } + } } } diff --git a/src/program.ts b/src/program.ts index a44ad6cc..5d67bae6 100644 --- a/src/program.ts +++ b/src/program.ts @@ -4,14 +4,12 @@ import * as program from 'commander'; import { Main } from './bwdc'; import { ConfigCommand } from './commands/config.command'; +import { SyncCommand } from './commands/sync.command'; import { TestCommand } from './commands/test.command'; import { UpdateCommand } from 'jslib/cli/commands/update.command'; -import { Response } from 'jslib/cli/models/response'; -import { ListResponse } from 'jslib/cli/models/response/listResponse'; -import { MessageResponse } from 'jslib/cli/models/response/messageResponse'; -import { StringResponse } from 'jslib/cli/models/response/stringResponse'; +import { BaseProgram } from 'jslib/cli/baseProgram'; const chalk = chk.default; const writeLn = (s: string, finalLine: boolean = false) => { @@ -22,8 +20,10 @@ const writeLn = (s: string, finalLine: boolean = false) => { } }; -export class Program { - constructor(private main: Main) { } +export class Program extends BaseProgram { + constructor(private main: Main) { + super(main.userService, writeLn); + } run() { program @@ -59,7 +59,8 @@ export class Program { writeLn('\n Examples:'); writeLn(''); writeLn(' bwdc test'); - writeLn(' bwdc config server bitwarden.com'); + writeLn(' bwdc sync'); + writeLn(' bwdc config server https://bw.company.com'); writeLn(' bwdc update'); writeLn('', true); }); @@ -82,6 +83,22 @@ export class Program { this.processResponse(response); }); + program + .command('sync') + .description('Sync the directory.') + .on('--help', () => { + writeLn('\n Examples:'); + writeLn(''); + writeLn(' bwdc sync'); + writeLn('', true); + }) + .action(async (cmd) => { + await this.exitIfNotAuthed(); + const command = new SyncCommand(this.main.syncService, this.main.i18nService); + const response = await command.run(cmd); + this.processResponse(response); + }); + program .command('config ') .description('Configure CLI settings.') @@ -97,7 +114,7 @@ export class Program { writeLn('', true); }) .action(async (setting, value, cmd) => { - const command = new ConfigCommand(this.main.environmentService); + const command = new ConfigCommand(this.main.environmentService, this.main.i18nService); const response = await command.run(setting, value, cmd); this.processResponse(response); }); @@ -131,95 +148,4 @@ export class Program { program.outputHelp(); } } - - private processResponse(response: Response, exitImmediately = false) { - if (!response.success) { - if (process.env.BW_QUIET !== 'true') { - if (process.env.BW_RESPONSE === 'true') { - writeLn(this.getJson(response), true); - } else { - writeLn(chalk.redBright(response.message), true); - } - } - if (exitImmediately) { - process.exit(1); - } else { - process.exitCode = 1; - } - return; - } - - if (process.env.BW_RESPONSE === 'true') { - writeLn(this.getJson(response), true); - } else if (response.data != null) { - let out: string = null; - if (response.data.object === 'string') { - const data = (response.data as StringResponse).data; - if (data != null) { - out = data; - } - } else if (response.data.object === 'list') { - out = this.getJson((response.data as ListResponse).data); - } else if (response.data.object === 'message') { - out = this.getMessage(response); - } else { - out = this.getJson(response.data); - } - - if (out != null && process.env.BW_QUIET !== 'true') { - writeLn(out, true); - } - } - if (exitImmediately) { - process.exit(0); - } else { - process.exitCode = 0; - } - } - - private getJson(obj: any): string { - if (process.env.BW_PRETTY === 'true') { - return JSON.stringify(obj, null, ' '); - } else { - return JSON.stringify(obj); - } - } - - private getMessage(response: Response) { - const message = (response.data as MessageResponse); - if (process.env.BW_RAW === 'true' && message.raw != null) { - return message.raw; - } - - let out: string = ''; - if (message.title != null) { - if (message.noColor) { - out = message.title; - } else { - out = chalk.greenBright(message.title); - } - } - if (message.message != null) { - if (message.title != null) { - out += '\n'; - } - out += message.message; - } - return out.trim() === '' ? null : out; - } - - private async exitIfAuthed() { - const authed = await this.main.userService.isAuthenticated(); - if (authed) { - const email = await this.main.userService.getEmail(); - this.processResponse(Response.error('You are already logged in as ' + email + '.'), true); - } - } - - private async exitIfNotAuthed() { - const authed = await this.main.userService.isAuthenticated(); - if (!authed) { - this.processResponse(Response.error('You are not logged in.'), true); - } - } }