mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-05 23:53:21 +00:00
base cli program and sync command
This commit is contained in:
2
jslib
2
jslib
Submodule jslib updated: 13a160fb79...b5b4222b32
@@ -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<Response> {
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
25
src/commands/sync.command.ts
Normal file
25
src/commands/sync.command.ts
Normal file
@@ -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<Response> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
124
src/program.ts
124
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 <setting> <value>')
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user