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-14 22:25:14 +02:00
|
|
|
import { DeleteCommand } from './commands/delete.command';
|
|
|
|
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 { Response } from './models/response';
|
2018-05-15 03:19:49 +02:00
|
|
|
import { CreateCommand } from './commands/create.command';
|
|
|
|
import { EncodeCommand } from './commands/encode.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-14 17:15:54 +02:00
|
|
|
export class Program {
|
|
|
|
constructor(private main: Main) { }
|
|
|
|
|
|
|
|
run() {
|
|
|
|
program
|
|
|
|
.version('1.0.0', '-v, --version');
|
|
|
|
|
|
|
|
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);
|
|
|
|
this.processResponse(response);
|
2018-05-14 17:15:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
program
|
|
|
|
.command('logout')
|
|
|
|
.description('Log out of the current Bitwarden user account.')
|
|
|
|
.action((cmd) => {
|
|
|
|
console.log('Logging out...');
|
2018-05-14 17:59:34 +02:00
|
|
|
process.exit();
|
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);
|
|
|
|
this.processResponse(response);
|
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);
|
|
|
|
this.processResponse(response);
|
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);
|
|
|
|
this.processResponse(response);
|
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);
|
|
|
|
this.processResponse(response);
|
|
|
|
});
|
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
program
|
|
|
|
.command('edit <object> <id>')
|
|
|
|
.description('Edit an object.')
|
|
|
|
.action((object, id, cmd) => {
|
|
|
|
console.log('Editing...');
|
|
|
|
console.log(object);
|
|
|
|
console.log(id);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
this.processResponse(response);
|
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);
|
|
|
|
this.processResponse(response);
|
|
|
|
});
|
|
|
|
|
2018-05-14 17:15:54 +02:00
|
|
|
program
|
|
|
|
.parse(process.argv);
|
|
|
|
}
|
2018-05-14 20:54:19 +02:00
|
|
|
|
|
|
|
private processResponse(response: Response) {
|
|
|
|
if (response.success) {
|
|
|
|
if (response.data != null) {
|
|
|
|
if (response.data.object === 'string') {
|
|
|
|
console.log((response.data as StringResponse).data);
|
|
|
|
} else if (response.data.object === 'list') {
|
|
|
|
console.log(JSON.stringify((response.data as ListResponse).data));
|
2018-05-14 22:25:14 +02:00
|
|
|
} else if (response.data.object === 'template') {
|
|
|
|
console.log(JSON.stringify((response.data as TemplateResponse).template));
|
2018-05-14 20:54:19 +02:00
|
|
|
} else {
|
|
|
|
console.log(JSON.stringify(response.data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
process.exit();
|
|
|
|
} else {
|
|
|
|
console.log(response.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 17:15:54 +02:00
|
|
|
}
|