/
commands.js
107 lines (90 loc) · 2.69 KB
/
commands.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
var winston = require('winston'),
jitsu = require('jitsu');
var commands = exports;
// Setup the commands organized by resource
commands.commands = {
apps: require('jitsu/commands/apps'),
config: require('jitsu/commands/config'),
snapshots: require('jitsu/commands/snapshots'),
help: require('jitsu/commands/help')
};
var requireAuth = ['apps', 'users', 'snapshots', 'logs'];
//
// ### function parse (command)
// #### @command {string} Command to parse
// Parses the specified command string.
//
commands.parse = function (command) {
//
// Remark (indexzero): Do we need more parsing than this?
//
return command.split(' ');
};
//
// ### function run (command, callback)
// #### @command {Array} Command to run
// #### @callback {function} Continuation to pass control to when complete.
// Runs the specified command.
//
commands.run = function (command, callback) {
var parts = commands.parse(command),
name = parts.shift(),
action = parts.shift(),
expected, resource;
//
// If we have been asked for `help` or `usage`
// about a particular resource, print the help
// for that resource.
//
if (action === 'usage' || action === 'help') {
commands.commands.help.show(name);
return callback();
}
if (!commands.commands[name]) {
return callback(new Error('Cannot run command on unknown resource: ' + name), true);
}
resource = commands.commands[name];
if (!resource[action]) {
return callback(new Error('Cannot run unknown action ' + action + ' on resource ' + name), true);
}
//
// When the command completes, check for an error, and if
// we are surpressing the err callstack (i.e. it is an **expected** error)
// then print the usage of the specified command.
//
parts.push(function (err, shallow) {
if (err && shallow) {
winston.help('');
winston.help('Usage:'.bold);
resource[action].usage.forEach(function (line) {
winston.help(line);
});
winston.help('');
}
callback(err, shallow);
});
//
// Check the arguments length then execute the command.
//
expected = resource[action].length;
if (parts.length > expected) {
return callback(new Error('Wrong number of arguments: ' + parts.length + ' for ' + expected), true)
}
function runCommand () {
resource[action].apply(resource, parts);
}
//
// If this resource can only be accessed
// after authenticating with Nodejitsu, do so
// then run the specified command on the resource
//
if (requireAuth.indexOf(name) !== -1) {
return jitsu.auth(function (err) {
if (err) {
return callback(err);
}
runCommand();
});
}
runCommand();
};