1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-30 08:10:34 +01:00
bitwarden-browser/src/program.ts

159 lines
6.0 KiB
TypeScript
Raw Normal View History

2018-05-15 16:50:06 +02:00
import * as chk from 'chalk';
2018-05-14 17:15:54 +02:00
import * as program from 'commander';
2018-05-15 05:16:59 +02:00
import { Main } from './bw';
2018-05-15 05:40:11 +02:00
import { CreateCommand } from './commands/create.command';
2018-05-14 22:25:14 +02:00
import { DeleteCommand } from './commands/delete.command';
2018-05-15 17:30:56 +02:00
import { EditCommand } from './commands/edit.command';
2018-05-15 05:40:11 +02:00
import { EncodeCommand } from './commands/encode.command';
2018-05-14 22:25:14 +02:00
import { GetCommand } from './commands/get.command';
import { ListCommand } from './commands/list.command';
2018-05-14 17:15:54 +02:00
import { LoginCommand } from './commands/login.command';
import { SyncCommand } from './commands/sync.command';
2018-05-14 20:54:19 +02:00
import { ListResponse } from './models/response/listResponse';
import { StringResponse } from './models/response/stringResponse';
2018-05-14 22:25:14 +02:00
import { TemplateResponse } from './models/response/templateResponse';
2018-05-14 20:54:19 +02:00
2018-05-15 05:40:11 +02:00
import { Response } from './models/response';
2018-05-15 16:50:06 +02:00
const chalk = chk.default;
2018-05-14 17:15:54 +02:00
export class Program {
constructor(private main: Main) { }
run() {
program
2018-05-15 18:44:10 +02:00
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version')
.option('--pretty', 'Format stdout.');
2018-05-14 17:15:54 +02:00
program
.command('login <email> <password>')
.description('Log into a Bitwarden user account.')
.option('-m, --method <method>', '2FA method.')
.option('-c, --code <code>', '2FA code.')
.action(async (email: string, password: string, cmd: program.Command) => {
const command = new LoginCommand(this.main.authService);
2018-05-14 20:54:19 +02:00
const response = await command.run(email, password, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
program
.command('logout')
.description('Log out of the current Bitwarden user account.')
.action((cmd) => {
2018-05-15 05:40:11 +02:00
// TODO
2018-05-14 17:15:54 +02:00
});
program
.command('sync')
.description('Sync user\'s vault from server.')
.option('-f, --force', 'Force a full sync.')
.action(async (cmd) => {
const command = new SyncCommand(this.main.syncService);
2018-05-14 20:54:19 +02:00
const response = await command.run(cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
program
.command('list <object>')
.description('List objects.')
2018-05-14 19:37:52 +02:00
.action(async (object, cmd) => {
const command = new ListCommand(this.main.cipherService, this.main.folderService,
this.main.collectionService);
2018-05-14 20:54:19 +02:00
const response = await command.run(object, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
program
.command('get <object> <id>')
.description('Get an object.')
2018-05-14 19:37:52 +02:00
.action(async (object, id, cmd) => {
const command = new GetCommand(this.main.cipherService, this.main.folderService,
this.main.collectionService, this.main.totpService);
2018-05-14 20:54:19 +02:00
const response = await command.run(object, id, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
2018-05-15 03:19:49 +02:00
program
.command('create <object> <encodedData>')
.description('Create an object.')
.action(async (object, encodedData, cmd) => {
const command = new CreateCommand(this.main.cipherService, this.main.folderService);
const response = await command.run(object, encodedData, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-15 03:19:49 +02:00
});
2018-05-14 17:15:54 +02:00
program
2018-05-15 17:30:56 +02:00
.command('edit <object> <id> <encodedData>')
2018-05-14 17:15:54 +02:00
.description('Edit an object.')
2018-05-15 17:30:56 +02:00
.action(async (object, id, encodedData, cmd) => {
const command = new EditCommand(this.main.cipherService, this.main.folderService);
const response = await command.run(object, id, encodedData, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
program
.command('delete <object> <id>')
.description('Delete an object.')
2018-05-14 21:08:48 +02:00
.action(async (object, id, cmd) => {
const command = new DeleteCommand(this.main.cipherService, this.main.folderService);
const response = await command.run(object, id, cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 17:15:54 +02:00
});
2018-05-14 23:13:57 +02:00
program
.command('encode')
.description('Base64 encode stdin.')
.action(async (object, id, cmd) => {
const command = new EncodeCommand();
const response = await command.run(cmd);
2018-05-15 18:44:10 +02:00
this.processResponse(response, cmd);
2018-05-14 23:13:57 +02:00
});
2018-05-14 17:15:54 +02:00
program
.parse(process.argv);
}
2018-05-14 20:54:19 +02:00
2018-05-15 18:44:10 +02:00
private processResponse(response: Response, cmd: program.Command) {
2018-05-14 20:54:19 +02:00
if (response.success) {
if (response.data != null) {
if (response.data.object === 'string') {
2018-05-15 05:40:11 +02:00
process.stdout.write((response.data as StringResponse).data);
2018-05-14 20:54:19 +02:00
} else if (response.data.object === 'list') {
2018-05-15 18:44:10 +02:00
this.printJson((response.data as ListResponse).data, cmd);
2018-05-14 22:25:14 +02:00
} else if (response.data.object === 'template') {
2018-05-15 18:44:10 +02:00
this.printJson((response.data as TemplateResponse).template, cmd);
2018-05-14 20:54:19 +02:00
} else {
2018-05-15 18:44:10 +02:00
this.printJson(response.data, cmd);
2018-05-14 20:54:19 +02:00
}
}
process.exit();
} else {
2018-05-15 16:50:06 +02:00
process.stdout.write(chalk.redBright(response.message));
2018-05-14 20:54:19 +02:00
process.exit(1);
}
}
2018-05-15 18:44:10 +02:00
private printJson(obj: any, cmd: program.Command) {
if (this.hasGlobalOption('pretty', cmd)) {
process.stdout.write(JSON.stringify(obj, null, ' '));
} else {
process.stdout.write(JSON.stringify(obj));
}
}
private hasGlobalOption(option: string, cmd: program.Command): boolean {
if (cmd[option] || false) {
return true;
}
if (cmd.parent == null) {
return false;
}
return this.hasGlobalOption(option, cmd.parent);
}
2018-05-14 17:15:54 +02:00
}