bitwarden-estensione-browser/src/program.ts

178 lines
6.5 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
2018-05-15 23:17:47 +02:00
.command('login [email] [password]')
2018-05-14 17:15:54 +02:00
.description('Log into a Bitwarden user account.')
2018-05-15 23:17:47 +02:00
.option('-m, --method <method>', 'Two-step login method.')
.option('-c, --code <code>', 'Two-step login code.')
2018-05-14 17:15:54 +02:00
.action(async (email: string, password: string, cmd: program.Command) => {
2018-05-16 03:11:58 +02:00
const command = new LoginCommand(this.main.authService, this.main.apiService,
this.main.cryptoFunctionService);
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
});
2018-05-15 20:21:42 +02:00
program
.command('lock')
.description('Lock the vault and destroy the current session token.')
.action((cmd) => {
// TODO
});
program
2018-05-15 23:17:47 +02:00
.command('unlock [password]')
2018-05-15 20:21:42 +02:00
.description('Unlock the vault and obtain a new session token.')
.action((cmd) => {
// 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
2018-05-16 03:22:39 +02:00
.command('get <object> [id]')
2018-05-14 17:15:54 +02:00
.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,
2018-05-16 03:22:39 +02:00
this.main.collectionService, this.main.totpService, this.main.syncService);
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-15 20:21:42 +02:00
if (!response.success) {
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 20:21:42 +02:00
return;
}
if (response.data != null) {
if (response.data.object === 'string') {
2018-05-16 03:22:39 +02:00
const data = (response.data as StringResponse).data;
if (data != null) {
process.stdout.write(data);
}
2018-05-15 20:21:42 +02:00
} else if (response.data.object === 'list') {
this.printJson((response.data as ListResponse).data, cmd);
} else if (response.data.object === 'template') {
this.printJson((response.data as TemplateResponse).template, cmd);
} else {
this.printJson(response.data, cmd);
}
2018-05-14 20:54:19 +02:00
}
2018-05-15 20:21:42 +02:00
process.exit();
2018-05-14 20:54:19 +02:00
}
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
}