mirror of
https://github.com/bitwarden/directory-connector
synced 2025-12-10 21:33:20 +00:00
data-file and last-sync commands
This commit is contained in:
18
src/bwdc.ts
18
src/bwdc.ts
@@ -31,6 +31,7 @@ import { Program } from './program';
|
|||||||
const packageJson = require('./package.json');
|
const packageJson = require('./package.json');
|
||||||
|
|
||||||
export class Main {
|
export class Main {
|
||||||
|
dataFilePath: string;
|
||||||
logService: ConsoleLogService;
|
logService: ConsoleLogService;
|
||||||
messagingService: NoopMessagingService;
|
messagingService: NoopMessagingService;
|
||||||
storageService: LowdbStorageService;
|
storageService: LowdbStorageService;
|
||||||
@@ -53,21 +54,20 @@ export class Main {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const applicationName = 'Bitwarden Directory Connector';
|
const applicationName = 'Bitwarden Directory Connector';
|
||||||
let p = null;
|
|
||||||
if (process.env.BITWARDENCLI_CONNECTOR_APPDATA_DIR) {
|
if (process.env.BITWARDENCLI_CONNECTOR_APPDATA_DIR) {
|
||||||
p = path.resolve(process.env.BITWARDENCLI_CONNECTOR_APPDATA_DIR);
|
this.dataFilePath = path.resolve(process.env.BITWARDENCLI_CONNECTOR_APPDATA_DIR);
|
||||||
} else if (process.env.BITWARDEN_CONNECTOR_APPDATA_DIR) {
|
} else if (process.env.BITWARDEN_CONNECTOR_APPDATA_DIR) {
|
||||||
p = path.resolve(process.env.BITWARDEN_CONNECTOR_APPDATA_DIR);
|
this.dataFilePath = path.resolve(process.env.BITWARDEN_CONNECTOR_APPDATA_DIR);
|
||||||
} else if (fs.existsSync(path.join(__dirname, 'bitwarden-connector-appdata'))) {
|
} else if (fs.existsSync(path.join(__dirname, 'bitwarden-connector-appdata'))) {
|
||||||
p = path.join(__dirname, 'bitwarden-connector-appdata');
|
this.dataFilePath = path.join(__dirname, 'bitwarden-connector-appdata');
|
||||||
} else if (process.platform === 'darwin') {
|
} else if (process.platform === 'darwin') {
|
||||||
p = path.join(process.env.HOME, 'Library/Application Support/' + applicationName);
|
this.dataFilePath = path.join(process.env.HOME, 'Library/Application Support/' + applicationName);
|
||||||
} else if (process.platform === 'win32') {
|
} else if (process.platform === 'win32') {
|
||||||
p = path.join(process.env.APPDATA, applicationName);
|
this.dataFilePath = path.join(process.env.APPDATA, applicationName);
|
||||||
} else if (process.env.XDG_CONFIG_HOME) {
|
} else if (process.env.XDG_CONFIG_HOME) {
|
||||||
p = path.join(process.env.XDG_CONFIG_HOME, applicationName);
|
this.dataFilePath = path.join(process.env.XDG_CONFIG_HOME, applicationName);
|
||||||
} else {
|
} else {
|
||||||
p = path.join(process.env.HOME, '.config/' + applicationName);
|
this.dataFilePath = path.join(process.env.HOME, '.config/' + applicationName);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.i18nService = new I18nService('en', './locales');
|
this.i18nService = new I18nService('en', './locales');
|
||||||
@@ -75,7 +75,7 @@ export class Main {
|
|||||||
this.logService = new ConsoleLogService(this.platformUtilsService.isDev(),
|
this.logService = new ConsoleLogService(this.platformUtilsService.isDev(),
|
||||||
(level) => process.env.BWCLI_DEBUG !== 'true' && level <= LogLevelType.Info);
|
(level) => process.env.BWCLI_DEBUG !== 'true' && level <= LogLevelType.Info);
|
||||||
this.cryptoFunctionService = new NodeCryptoFunctionService();
|
this.cryptoFunctionService = new NodeCryptoFunctionService();
|
||||||
this.storageService = new LowdbStorageService(null, p, true);
|
this.storageService = new LowdbStorageService(null, this.dataFilePath, true);
|
||||||
this.secureStorageService = new KeytarSecureStorageService(applicationName);
|
this.secureStorageService = new KeytarSecureStorageService(applicationName);
|
||||||
this.cryptoService = new CryptoService(this.storageService, this.secureStorageService,
|
this.cryptoService = new CryptoService(this.storageService, this.secureStorageService,
|
||||||
this.cryptoFunctionService);
|
this.cryptoFunctionService);
|
||||||
|
|||||||
29
src/commands/lastSync.command.ts
Normal file
29
src/commands/lastSync.command.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import * as program from 'commander';
|
||||||
|
|
||||||
|
import { ConfigurationService } from '../services/configuration.service';
|
||||||
|
|
||||||
|
import { Response } from 'jslib/cli/models/response';
|
||||||
|
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
|
||||||
|
|
||||||
|
export class LastSyncCommand {
|
||||||
|
constructor(private configurationService: ConfigurationService) { }
|
||||||
|
|
||||||
|
async run(object: string, cmd: program.Command): Promise<Response> {
|
||||||
|
try {
|
||||||
|
switch (object.toLowerCase()) {
|
||||||
|
case 'groups':
|
||||||
|
const groupsDate = await this.configurationService.getLastGroupSyncDate();
|
||||||
|
const groupsRes = new StringResponse(groupsDate == null ? null : groupsDate.toISOString());
|
||||||
|
return Response.success(groupsRes);
|
||||||
|
case 'users':
|
||||||
|
const usersDate = await this.configurationService.getLastUserSyncDate();
|
||||||
|
const usersRes = new StringResponse(usersDate == null ? null : usersDate.toISOString());
|
||||||
|
return Response.success(usersRes);
|
||||||
|
default:
|
||||||
|
return Response.badRequest('Unknown object.');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return Response.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
import * as chk from 'chalk';
|
import * as chk from 'chalk';
|
||||||
import * as program from 'commander';
|
import * as program from 'commander';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
import { Main } from './bwdc';
|
import { Main } from './bwdc';
|
||||||
|
|
||||||
import { ClearCacheCommand } from './commands/clearCache.command';
|
import { ClearCacheCommand } from './commands/clearCache.command';
|
||||||
import { ConfigCommand } from './commands/config.command';
|
import { ConfigCommand } from './commands/config.command';
|
||||||
|
import { LastSyncCommand } from './commands/lastSync.command';
|
||||||
import { SyncCommand } from './commands/sync.command';
|
import { SyncCommand } from './commands/sync.command';
|
||||||
import { TestCommand } from './commands/test.command';
|
import { TestCommand } from './commands/test.command';
|
||||||
|
|
||||||
@@ -14,6 +16,9 @@ import { UpdateCommand } from 'jslib/cli/commands/update.command';
|
|||||||
|
|
||||||
import { BaseProgram } from 'jslib/cli/baseProgram';
|
import { BaseProgram } from 'jslib/cli/baseProgram';
|
||||||
|
|
||||||
|
import { Response } from 'jslib/cli/models/response';
|
||||||
|
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
|
||||||
|
|
||||||
const chalk = chk.default;
|
const chalk = chk.default;
|
||||||
const writeLn = (s: string, finalLine: boolean = false) => {
|
const writeLn = (s: string, finalLine: boolean = false) => {
|
||||||
if (finalLine && process.platform === 'win32') {
|
if (finalLine && process.platform === 'win32') {
|
||||||
@@ -61,8 +66,10 @@ export class Program extends BaseProgram {
|
|||||||
program.on('--help', () => {
|
program.on('--help', () => {
|
||||||
writeLn('\n Examples:');
|
writeLn('\n Examples:');
|
||||||
writeLn('');
|
writeLn('');
|
||||||
|
writeLn(' bwdc login');
|
||||||
writeLn(' bwdc test');
|
writeLn(' bwdc test');
|
||||||
writeLn(' bwdc sync');
|
writeLn(' bwdc sync');
|
||||||
|
writeLn(' bwdc last-sync');
|
||||||
writeLn(' bwdc config server https://bw.company.com');
|
writeLn(' bwdc config server https://bw.company.com');
|
||||||
writeLn(' bwdc update');
|
writeLn(' bwdc update');
|
||||||
writeLn('', true);
|
writeLn('', true);
|
||||||
@@ -143,6 +150,27 @@ export class Program extends BaseProgram {
|
|||||||
this.processResponse(response);
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('last-sync <object>')
|
||||||
|
.description('Get the last successful sync date.')
|
||||||
|
.on('--help', () => {
|
||||||
|
writeLn('\n Notes:');
|
||||||
|
writeLn('');
|
||||||
|
writeLn(' Returns empty response if no sync has been performed for the given object.');
|
||||||
|
writeLn('');
|
||||||
|
writeLn(' Examples:');
|
||||||
|
writeLn('');
|
||||||
|
writeLn(' bwdc last-sync groups');
|
||||||
|
writeLn(' bwdc last-sync users');
|
||||||
|
writeLn('', true);
|
||||||
|
})
|
||||||
|
.action(async (object: string, cmd: program.Command) => {
|
||||||
|
await this.exitIfNotAuthed();
|
||||||
|
const command = new LastSyncCommand(this.main.configurationService);
|
||||||
|
const response = await command.run(object, cmd);
|
||||||
|
this.processResponse(response);
|
||||||
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('config <setting> <value>')
|
.command('config <setting> <value>')
|
||||||
.description('Configure settings.')
|
.description('Configure settings.')
|
||||||
@@ -174,6 +202,20 @@ export class Program extends BaseProgram {
|
|||||||
this.processResponse(response);
|
this.processResponse(response);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('data-file')
|
||||||
|
.description('Path to data.json database file.')
|
||||||
|
.on('--help', () => {
|
||||||
|
writeLn('\n Examples:');
|
||||||
|
writeLn('');
|
||||||
|
writeLn(' bwdc data-file');
|
||||||
|
writeLn('', true);
|
||||||
|
})
|
||||||
|
.action(() => {
|
||||||
|
this.processResponse(
|
||||||
|
Response.success(new StringResponse(path.join(this.main.dataFilePath, 'data.json'))));
|
||||||
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('clear-cache')
|
.command('clear-cache')
|
||||||
.description('Clear the sync cache.')
|
.description('Clear the sync cache.')
|
||||||
|
|||||||
Reference in New Issue
Block a user