1 | var util = require('./util');
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var Plugin = function(modem, name, remote) {
|
9 | this.modem = modem;
|
10 | this.name = name;
|
11 | this.remote = remote || name;
|
12 | };
|
13 |
|
14 | Plugin.prototype[require('util').inspect.custom] = function() { return this; };
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | Plugin.prototype.inspect = function(callback) {
|
22 | var self = this;
|
23 |
|
24 | var optsf = {
|
25 | path: '/plugins/' + this.name,
|
26 | method: 'GET',
|
27 | statusCodes: {
|
28 | 200: true,
|
29 | 404: 'plugin is not installed',
|
30 | 500: 'server error'
|
31 | }
|
32 | };
|
33 |
|
34 | if(callback === undefined) {
|
35 | return new this.modem.Promise(function(resolve, reject) {
|
36 | self.modem.dial(optsf, function(err, data) {
|
37 | if (err) {
|
38 | return reject(err);
|
39 | }
|
40 | resolve(data);
|
41 | });
|
42 | });
|
43 | } else {
|
44 | this.modem.dial(optsf, function(err, data) {
|
45 | callback(err, data);
|
46 | });
|
47 | }
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | Plugin.prototype.remove = function(opts, callback) {
|
56 | var self = this;
|
57 | var args = util.processArgs(opts, callback);
|
58 |
|
59 | var optsf = {
|
60 | path: '/plugins/' + this.name + '?',
|
61 | method: 'DELETE',
|
62 | statusCodes: {
|
63 | 200: true,
|
64 | 404: 'plugin is not installed',
|
65 | 500: 'server error'
|
66 | },
|
67 | options: args.opts
|
68 | };
|
69 |
|
70 | if(args.callback === undefined) {
|
71 | return new this.modem.Promise(function(resolve, reject) {
|
72 | self.modem.dial(optsf, function(err, data) {
|
73 | if (err) {
|
74 | return reject(err);
|
75 | }
|
76 | resolve(data);
|
77 | });
|
78 | });
|
79 | } else {
|
80 | this.modem.dial(optsf, function(err, data) {
|
81 | if (err) return args.callback(err, data);
|
82 | args.callback(err, data);
|
83 | });
|
84 | }
|
85 | };
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | Plugin.prototype.privileges = function(callback) {
|
93 | var self = this;
|
94 | var optsf = {
|
95 | path: '/plugins/privileges?',
|
96 | method: 'GET',
|
97 | options: {
|
98 | 'remote': this.remote
|
99 | },
|
100 | statusCodes: {
|
101 | 200: true,
|
102 | 500: 'server error'
|
103 | }
|
104 | };
|
105 |
|
106 | if(callback === undefined) {
|
107 | return new this.modem.Promise(function(resolve, reject) {
|
108 | self.modem.dial(optsf, function(err, data) {
|
109 | if (err) {
|
110 | return reject(err);
|
111 | }
|
112 | resolve(data);
|
113 | });
|
114 | });
|
115 | } else {
|
116 | this.modem.dial(optsf, function(err, data) {
|
117 | callback(err, data);
|
118 | });
|
119 | }
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | Plugin.prototype.pull = function(opts, callback) {
|
129 | var self = this;
|
130 | var args = util.processArgs(opts, callback);
|
131 |
|
132 | if(args.opts._query && !args.opts._query.name) {
|
133 | args.opts._query.name = this.name;
|
134 | }
|
135 | if(args.opts._query && !args.opts._query.remote) {
|
136 | args.opts._query.remote = this.remote;
|
137 | }
|
138 |
|
139 | var optsf = {
|
140 | path: '/plugins/pull?',
|
141 | method: 'POST',
|
142 | isStream: true,
|
143 | options: args.opts,
|
144 | statusCodes: {
|
145 | 200: true,
|
146 | 204: true,
|
147 | 500: 'server error'
|
148 | }
|
149 | };
|
150 |
|
151 | if(args.callback === undefined) {
|
152 | return new this.modem.Promise(function(resolve, reject) {
|
153 | self.modem.dial(optsf, function(err, data) {
|
154 | if (err) {
|
155 | return reject(err);
|
156 | }
|
157 | resolve(data);
|
158 | });
|
159 | });
|
160 | } else {
|
161 | this.modem.dial(optsf, function(err, data) {
|
162 | args.callback(err, data);
|
163 | });
|
164 | }
|
165 | };
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 | Plugin.prototype.enable = function(opts, callback) {
|
174 | var self = this;
|
175 | var args = util.processArgs(opts, callback);
|
176 |
|
177 | var optsf = {
|
178 | path: '/plugins/' + this.name + '/enable?',
|
179 | method: 'POST',
|
180 | statusCodes: {
|
181 | 200: true,
|
182 | 500: 'server error'
|
183 | },
|
184 | options: args.opts
|
185 | };
|
186 |
|
187 | if(args.callback === undefined) {
|
188 | return new this.modem.Promise(function(resolve, reject) {
|
189 | self.modem.dial(optsf, function(err, data) {
|
190 | if (err) {
|
191 | return reject(err);
|
192 | }
|
193 | resolve(data);
|
194 | });
|
195 | });
|
196 | } else {
|
197 | this.modem.dial(optsf, function(err, data) {
|
198 | args.callback(err, data);
|
199 | });
|
200 | }
|
201 | };
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | Plugin.prototype.disable = function(opts, callback) {
|
209 | var self = this;
|
210 | var args = util.processArgs(opts, callback);
|
211 |
|
212 | var optsf = {
|
213 | path: '/plugins/' + this.name + '/disable',
|
214 | method: 'POST',
|
215 | statusCodes: {
|
216 | 200: true,
|
217 | 500: 'server error'
|
218 | },
|
219 | options: args.opts
|
220 | };
|
221 |
|
222 | if(args.callback === undefined) {
|
223 | return new this.modem.Promise(function(resolve, reject) {
|
224 | self.modem.dial(optsf, function(err, data) {
|
225 | if (err) {
|
226 | return reject(err);
|
227 | }
|
228 | resolve(data);
|
229 | });
|
230 | });
|
231 | } else {
|
232 | this.modem.dial(optsf, function(err, data) {
|
233 | args.callback(err, data);
|
234 | });
|
235 | }
|
236 | };
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | Plugin.prototype.push = function(opts, callback) {
|
244 | var self = this;
|
245 | var args = util.processArgs(opts, callback);
|
246 |
|
247 | var optsf = {
|
248 | path: '/plugins/' + this.name + '/push',
|
249 | method: 'POST',
|
250 | statusCodes: {
|
251 | 200: true,
|
252 | 404: 'plugin not installed',
|
253 | 500: 'server error'
|
254 | },
|
255 | options: args.opts
|
256 | };
|
257 |
|
258 | if(args.callback === undefined) {
|
259 | return new this.modem.Promise(function(resolve, reject) {
|
260 | self.modem.dial(optsf, function(err, data) {
|
261 | if (err) {
|
262 | return reject(err);
|
263 | }
|
264 | resolve(data);
|
265 | });
|
266 | });
|
267 | } else {
|
268 | this.modem.dial(optsf, function(err, data) {
|
269 | args.callback(err, data);
|
270 | });
|
271 | }
|
272 | };
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | Plugin.prototype.configure = function(opts, callback) {
|
280 | var self = this;
|
281 | var args = util.processArgs(opts, callback);
|
282 |
|
283 | var optsf = {
|
284 | path: '/plugins/' + this.name + '/set',
|
285 | method: 'POST',
|
286 | statusCodes: {
|
287 | 200: true,
|
288 | 404: 'plugin not installed',
|
289 | 500: 'server error'
|
290 | },
|
291 | options: args.opts
|
292 | };
|
293 |
|
294 | if(args.callback === undefined) {
|
295 | return new this.modem.Promise(function(resolve, reject) {
|
296 | self.modem.dial(optsf, function(err, data) {
|
297 | if (err) {
|
298 | return reject(err);
|
299 | }
|
300 | resolve(data);
|
301 | });
|
302 | });
|
303 | } else {
|
304 | this.modem.dial(optsf, function(err, data) {
|
305 | args.callback(err, data);
|
306 | });
|
307 | }
|
308 | };
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 | Plugin.prototype.upgrade = function(auth, opts, callback) {
|
319 | var self = this;
|
320 | if (!callback && typeof opts === 'function') {
|
321 | callback = opts;
|
322 | opts = auth;
|
323 | auth = opts.authconfig || undefined;
|
324 | }
|
325 |
|
326 | var optsf = {
|
327 | path: '/plugins/' + this.name + '/upgrade?',
|
328 | method: 'POST',
|
329 | statusCodes: {
|
330 | 200: true,
|
331 | 204: true,
|
332 | 404: 'plugin not installed',
|
333 | 500: 'server error'
|
334 | },
|
335 | authconfig: auth,
|
336 | options: opts
|
337 | };
|
338 |
|
339 | if(callback === undefined) {
|
340 | return new this.modem.Promise(function(resolve, reject) {
|
341 | self.modem.dial(optsf, function(err, data) {
|
342 | if (err) {
|
343 | return reject(err);
|
344 | }
|
345 | resolve(data);
|
346 | });
|
347 | });
|
348 | } else {
|
349 | this.modem.dial(optsf, function(err, data) {
|
350 | callback(err, data);
|
351 | });
|
352 | }
|
353 | };
|
354 |
|
355 |
|
356 | module.exports = Plugin;
|