mirror of
https://github.com/bitwarden/browser.git
synced 2024-11-30 13:03:53 +01:00
Add status command (#145)
* Show vault status. The status is shown as 'unauthenticated', 'locked', or 'unlocked'. * Add more status command fields. Added `serverUrl`, `lastSync`, `userEmail`, and `userId`. * Add status help text.
This commit is contained in:
parent
c0c368cbfe
commit
62c7c30cb4
56
src/commands/status.command.ts
Normal file
56
src/commands/status.command.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import * as program from 'commander';
|
||||
|
||||
import { EnvironmentService, SyncService, UserService, VaultTimeoutService } from 'jslib/abstractions';
|
||||
import { Response } from 'jslib/cli/models/response';
|
||||
import { TemplateResponse } from '../models/response/templateResponse';
|
||||
|
||||
export class StatusCommand {
|
||||
constructor(
|
||||
private envService: EnvironmentService,
|
||||
private syncService: SyncService,
|
||||
private userService: UserService,
|
||||
private vaultTimeoutService: VaultTimeoutService) {
|
||||
}
|
||||
|
||||
async run(cmd: program.Command): Promise<Response> {
|
||||
try {
|
||||
const baseUrl = this.baseUrl();
|
||||
const status = await this.status();
|
||||
const lastSync = await this.syncService.getLastSync();
|
||||
const userId = await this.userService.getUserId();
|
||||
const email = await this.userService.getEmail();
|
||||
|
||||
return Response.success(new TemplateResponse({
|
||||
serverUrl: baseUrl,
|
||||
lastSync: lastSync,
|
||||
userEmail: email,
|
||||
userId: userId,
|
||||
status: status,
|
||||
}));
|
||||
} catch (e) {
|
||||
return Response.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private baseUrl(): string {
|
||||
let url = this.envService.baseUrl;
|
||||
if (url == null) {
|
||||
url = 'https://bitwarden.com';
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
private async status(): Promise<string> {
|
||||
const isAuthed = await this.userService.isAuthenticated();
|
||||
if (!isAuthed) {
|
||||
return 'unauthenticated';
|
||||
}
|
||||
|
||||
const isLocked = await this.vaultTimeoutService.isLocked();
|
||||
if (isLocked) {
|
||||
return 'locked';
|
||||
} else {
|
||||
return 'unlocked';
|
||||
}
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@ import { LockCommand } from './commands/lock.command';
|
||||
import { LoginCommand } from './commands/login.command';
|
||||
import { RestoreCommand } from './commands/restore.command';
|
||||
import { ShareCommand } from './commands/share.command';
|
||||
import { StatusCommand } from './commands/status.command';
|
||||
import { SyncCommand } from './commands/sync.command';
|
||||
import { UnlockCommand } from './commands/unlock.command';
|
||||
|
||||
@ -682,6 +683,38 @@ export class Program extends BaseProgram {
|
||||
this.processResponse(response);
|
||||
});
|
||||
|
||||
program
|
||||
.command('status')
|
||||
.description('Show server, last sync, user information, and vault status.')
|
||||
.on('--help', () => {
|
||||
writeLn('');
|
||||
writeLn('');
|
||||
writeLn(' Example return value:');
|
||||
writeLn('');
|
||||
writeLn(' {');
|
||||
writeLn(' "serverUrl": "https://bitwarden.example.com",');
|
||||
writeLn(' "lastSync": "2020-06-16T06:33:51.419Z",');
|
||||
writeLn(' "userEmail": "user@example.com,');
|
||||
writeLn(' "userId": "00000000-0000-0000-0000-000000000000",');
|
||||
writeLn(' "status": "locked"');
|
||||
writeLn(' }');
|
||||
writeLn('');
|
||||
writeLn(' The "status" is one of "unauthenticated", "locked", "unlocked":');
|
||||
writeLn(' - "unauthenticated" when you are not logged in');
|
||||
writeLn(' - "locked" when you are logged in and the vault is locked');
|
||||
writeLn(' - "unlocked" when you are logged in and the vault is unlocked');
|
||||
writeLn('', true);
|
||||
})
|
||||
.action(async (cmd: program.Command) => {
|
||||
const command = new StatusCommand(
|
||||
this.main.environmentService,
|
||||
this.main.syncService,
|
||||
this.main.userService,
|
||||
this.main.vaultTimeoutService);
|
||||
const response = await command.run(cmd);
|
||||
this.processResponse(response);
|
||||
});
|
||||
|
||||
program
|
||||
.parse(process.argv);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user