-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
224 lines (185 loc) · 7.88 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* global print, logError */
const
GLib = imports.gi.GLib,
Lang = imports.lang,
Option = imports.option.Option,
PluginRepository = imports.repository.PluginRepository;
const
FedyOption = {
LIST: new Option("list", GLib.OptionArg.NONE, "List available plugins", null, false),
ONELINE: new Option("oneline", GLib.OptionArg.NONE, "Render each plugin description on one line", null, false),
STATUS: new Option("status", GLib.OptionArg.STRING_ARRAY, "Report plugin status", "<plugin>", true),
ADD: new Option("add", GLib.OptionArg.STRING_ARRAY, "Add a plugin", "<plugin>", true),
REMOVE: new Option("remove", GLib.OptionArg.STRING_ARRAY, "Remove a plugin", "<plugin>", true),
FORCE: new Option("force", GLib.OptionArg.NONE, "Force plugins action", null, false)
},
FedyOptions = [
FedyOption.LIST,
FedyOption.ONELINE,
FedyOption.STATUS,
FedyOption.ADD,
FedyOption.REMOVE,
FedyOption.FORCE
];
var PluginAction = new Lang.Class({
Name: "PluginAction",
_init(option, plugin, forced) {
this.option = option;
this.plugin = plugin;
this.forced = forced;
},
actionLabel() {
return this.option.name.charAt(0).toUpperCase() + this.option.name.substr(1);
},
pluginLabel() {
return (this.isPluginUnknown()) ? this.plugin.name : this.plugin.label;
},
isCommand() {
return !this.isPluginUnknown() && !this.isStatus();
},
isPluginUnknown() {
return typeof this.plugin.label === "undefined";
},
isStatus() {
return FedyOption.STATUS.match(this.option.name);
}
});
var FedyCli = new Lang.Class({
Name: "FedyCli",
_init(fedy) {
FedyOptions.forEach(option => option.registerIn(fedy.application));
this.fedy = fedy;
if (typeof fedy.application.set_option_context_summary !== "undefined") {
fedy.application.set_option_context_parameter_string("app.js");
fedy.application.set_option_context_description("");
fedy.application.set_option_context_summary(
" fedy --list [--oneline]\n" +
" fedy [--force] [--status <plugin>]... [--remove <plugin>]... [--add <plugin>]..."
);
}
this.fedy.application.connect("handle_local_options", Lang.bind(this, this._onOptions));
},
_onOptions(application, options) {
if (!this._isCliOptions(options)) {
return -1;
}
this.pluginRepository = new PluginRepository(this.fedy);
const loop = GLib.MainLoop.new(null, false);
let promiseOfReports = (FedyOption.LIST.in(options)) ?
this._promiseOfCategoryReports(options) :
this._promiseOfActionReports(options);
Promise
.all(promiseOfReports)
.then(reports => this._printReports(reports))
.then(() => loop.quit());
loop.run();
return 0;
},
_isCliOptions(options) {
return FedyOptions.some(option => option.in(options));
},
_promiseOfCategoryReports(options) {
const oneline = FedyOption.ONELINE.in(options);
return this.pluginRepository
.listCategories()
.map(category => {
const promiseOfPluginsWithStatuses = this.pluginRepository
.listByCategory(category)
.map(plugin => this.pluginRepository
.promiseOfPluginStatus(plugin)
.then(pluginStatus => [ plugin, pluginStatus ]));
return Promise
.all(promiseOfPluginsWithStatuses)
.then(pluginsWithStatuses => this._buildCategoryReport(category, pluginsWithStatuses, oneline));
});
},
_buildCategoryReport(category, pluginsWithStatuses, oneline) {
let report = `Category: ${category}\n`;
pluginsWithStatuses.forEach(([ plugin, pluginStatus ]) => {
report += this._buildPluginReport(plugin, pluginStatus, oneline);
});
report += "\n";
return report;
},
_buildPluginReport(plugin, pluginStatus, oneline) {
const statusCharacter = pluginStatus.isPluginEnable() ? "✓" : "✗",
onelineReport = `${statusCharacter} [${plugin.name}] ${plugin.label}\n`,
license = (typeof plugin.license !== "undefined") ? ` (${plugin.license})\n` : "";
return (oneline) ? onelineReport : `${onelineReport} ${plugin.description}\n${license}`;
},
_promiseOfActionReports: function (options) {
const forced = FedyOption.FORCE.in(options);
return FedyOptions
.filter(option => option.in(options) && option.isAction)
.flatMap(option => this._pluginActions(options, option, forced))
.map(pluginActions => this._promiseOfActionReport(pluginActions));
},
_pluginActions(options, option, forced) {
return option
.parameters(options)
.map(pluginName => {
let plugin = this.pluginRepository.getByName(pluginName);
return new PluginAction(option, plugin, forced);
});
},
_promiseOfActionReport(pluginAction) {
return this.pluginRepository
.promiseOfPluginStatus(pluginAction.plugin)
.then(pluginStatus => {
if (!this._isCommandRequired(pluginAction, pluginStatus)) {
return this._buildReport(pluginAction, pluginStatus, 0);
}
return this.pluginRepository
.promiseOfCommandStatus(pluginAction.plugin.path, pluginStatus.action.command)
.then(commandStatusCode => this._buildReport(pluginAction, pluginStatus, commandStatusCode));
})
.catch(e => logError(e));
},
_buildReport(pluginAction, pluginStatus, commandStatusCode) {
const actionLabel = pluginAction.actionLabel(),
reportPrefix = `${actionLabel} of '${pluginAction.pluginLabel()}'`;
let report;
if (pluginAction.isPluginUnknown()) {
report = `✗ ${reportPrefix} fail: plugin name unknown`;
} else if (pluginAction.isStatus()) {
report = `✓ ${reportPrefix} ${(pluginStatus.isPluginEnable()) ? "enabled" : "disabled"}`;
} else if (this._isAlreadyApplied(pluginAction, pluginStatus)) {
report = `✓ ${reportPrefix} already done`;
} else if (pluginStatus.isMalicious && !pluginAction.forced) {
report = `✗ ${reportPrefix} aborted: the plugin is trying to ` +
`run the command '${pluginStatus.maliciousPart}' ` +
`which might ${pluginStatus.maliciousDescription}. Use -f option to force the execution.`;
} else if (commandStatusCode === 0) {
report = `✓ ${reportPrefix} (${pluginStatus.action.label}) successfully completed`;
} else {
report = `✗ ${reportPrefix} (${pluginStatus.action.label}) failed`;
}
return report;
},
_isCommandRequired(pluginAction, pluginStatus) {
const isAlreadyApplied = this._isAlreadyApplied(pluginAction, pluginStatus);
return !isAlreadyApplied && pluginAction.isCommand() && (!pluginStatus.isMalicious || pluginAction.forced);
},
_isAlreadyApplied(pluginAction, pluginStatus) {
switch (pluginAction.option.name) {
case FedyOption.ADD.name:
return pluginStatus.code === 0;
case FedyOption.REMOVE.name:
return pluginStatus.code !== 0;
default:
return false;
}
},
_printReports(reports) {
print();
print("-------");
reports.forEach(report => {
print(report);
});
}
});
const concat = (x, y) => x.concat(y),
flatMap = (f, xs) => xs.map(f).reduce(concat, []);
Array.prototype.flatMap = function(f) {
return flatMap(f, this);
};