2017-11-03 00:09:34 +00:00
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { reg } = require('lib/registry.js');
|
2017-10-07 18:07:38 +01:00
|
|
|
|
2017-07-10 20:03:46 +00:00
|
|
|
class BaseCommand {
|
2017-10-07 17:30:27 +01:00
|
|
|
constructor() {
|
|
|
|
this.stdout_ = null;
|
2017-10-07 18:07:38 +01:00
|
|
|
this.prompt_ = null;
|
2017-10-07 17:30:27 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:03:46 +00:00
|
|
|
usage() {
|
|
|
|
throw new Error('Usage not defined');
|
|
|
|
}
|
|
|
|
|
2017-12-26 11:38:53 +01:00
|
|
|
encryptionCheck(item) {
|
|
|
|
if (item && item.encryption_applied) throw new Error(_('Cannot change encrypted item'));
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:03:46 +00:00
|
|
|
description() {
|
|
|
|
throw new Error('Description not defined');
|
|
|
|
}
|
|
|
|
|
2019-09-12 22:16:42 +00:00
|
|
|
async action() {
|
2017-07-10 20:03:46 +00:00
|
|
|
throw new Error('Action not defined');
|
|
|
|
}
|
|
|
|
|
2017-10-24 22:37:36 +01:00
|
|
|
compatibleUis() {
|
|
|
|
return ['cli', 'gui'];
|
|
|
|
}
|
|
|
|
|
|
|
|
supportsUi(ui) {
|
|
|
|
return this.compatibleUis().indexOf(ui) >= 0;
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:03:46 +00:00
|
|
|
options() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-07-18 18:21:03 +00:00
|
|
|
hidden() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-18 23:14:20 +01:00
|
|
|
enabled() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-20 16:29:18 +02:00
|
|
|
cancellable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:03:46 +00:00
|
|
|
async cancel() {}
|
|
|
|
|
2017-08-04 17:51:01 +00:00
|
|
|
name() {
|
|
|
|
let r = this.usage().split(' ');
|
|
|
|
return r[0];
|
|
|
|
}
|
|
|
|
|
2017-10-14 22:44:50 +01:00
|
|
|
setDispatcher(fn) {
|
|
|
|
this.dispatcher_ = fn;
|
2017-10-07 17:30:27 +01:00
|
|
|
}
|
|
|
|
|
2017-10-14 22:44:50 +01:00
|
|
|
dispatch(action) {
|
|
|
|
if (!this.dispatcher_) throw new Error('Dispatcher not defined');
|
|
|
|
return this.dispatcher_(action);
|
2017-10-07 17:30:27 +01:00
|
|
|
}
|
|
|
|
|
2017-10-14 22:44:50 +01:00
|
|
|
setStdout(fn) {
|
|
|
|
this.stdout_ = fn;
|
2017-10-14 19:03:23 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 18:18:31 +01:00
|
|
|
stdout(text) {
|
|
|
|
if (this.stdout_) this.stdout_(text);
|
2017-10-14 19:03:23 +01:00
|
|
|
}
|
|
|
|
|
2017-10-07 18:07:38 +01:00
|
|
|
setPrompt(fn) {
|
|
|
|
this.prompt_ = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
async prompt(message, options = null) {
|
|
|
|
if (!this.prompt_) throw new Error('Prompt is undefined');
|
|
|
|
return await this.prompt_(message, options);
|
|
|
|
}
|
|
|
|
|
2017-08-04 17:51:01 +00:00
|
|
|
metadata() {
|
|
|
|
return {
|
|
|
|
name: this.name(),
|
|
|
|
usage: this.usage(),
|
|
|
|
options: this.options(),
|
|
|
|
hidden: this.hidden(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-09 20:57:00 +01:00
|
|
|
logger() {
|
|
|
|
return reg.logger();
|
|
|
|
}
|
2017-07-10 20:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
module.exports = { BaseCommand };
|