-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli-utils.js
154 lines (127 loc) · 4.25 KB
/
cli-utils.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
var util = require('util');
var colors = require('colors');
var _ = require('underscore');
var rc = require('rc');
var moment = require('moment');
/**
* Format an ummon task
*
* @param {Task} task
*/
exports.formatTask = function(task) {
var heading = task.id.white;
if (task.enabled === false) { heading = '(disabled) '.red + heading}
var command = task.command;
command += (task.cwd)?' (in '+task.cwd +')':'';
console.log(heading + ': $ '.grey + command);
if (task.description) {
console.log('Description: '+ task.description.white);
}
if (task.trigger) {
if (task.trigger.afterFailed) {
console.log('Trigger after Failed: '+task.trigger.afterFailed);
}
else {
var trigger = task.trigger.time || task.trigger.after || task.trigger;
console.log('Trigger: '+ trigger)
}
}
if (task.lastSuccessfulRun) {
console.log('Last successful run: ' + moment(task.lastSuccessfulRun).calendar())
}
if (task.recentExitCodes.length) {
// Group by exit codes, [0,0,1,1] becomes {'0':[0,0], '1':[1,1]}
var codes = _.groupBy(task.recentExitCodes);
// Count the zeros!
var countSuccess = (codes['0']) ? codes['0'].length : 0;
// No zeros / total exit codes * 100 === pecentage
var percentage = (countSuccess / task.recentExitCodes.length * 100);
var label = "Success rate (last 10): "
// This isn't pretty but it works for now
if (percentage < 20) {
console.log(label + percentage.toString().red + '%'.red);
} else if (percentage == 100) {
console.log(label + percentage.toString().green + '%'.green);
} else {
console.log(label + percentage.toString().yellow + '%'.yellow);
}
}
console.log('');
}
exports.formatJson = function(data) {
console.log(util.inspect(data, { showHidden: true, depth: null }));
}
/**
* A simple helper to print key:value pairs
*/
exports.formatKeyVal = function(key, val) {
console.log(key + ': '+val);
}
exports.errorIfBadTaskID = function(task) {
if (task.indexOf('.') === -1) {
ifError(new Error('('+task+') is not a correct task id. Please use format: <collection>.<name>'));
}
}
/**
* Helper to print errors if there is an error
*
* @param {Error} err [description]
* @return {[type]} [description]
*/
exports.ifError = ifError = function(err) {
if (err) {
console.log('')
console.error('** ERROR **'.red);
if (err.statusCode) { console.error(' ('+err.statusCode+')') }
console.log('');
if (err.message) {
console.error(err.message.red);
} else {
console.error(err.red);
}
process.exit(1);
}
}
/**
* Helper to add all of the default options neccesary for each ummon cli
*
* @param {Object} program The commander object
*/
var addGlobalOptions = exports.addGlobalOptions = function(program) {
// Global Options
program
.option('--env <env>', 'The ummon environment to connect to (Automatically loaded from ~/.ummonrc if present)')
.option('-h, --url <url>', 'The url where the ummon server is running. (Automatically loaded from ~/.ummonrc if present)', 'http://localhost:8888')
.option('-u, --username <username>', 'Your username (Automatically loaded from ~/.ummonrc if present)')
.option('-p, --password <password>', 'Your password (Automatically loaded from ~/.ummonrc if present)')
.option('-r, --retry <count>', 'The amount of times to retry a failed connection', 3)
.option('-t, --timeout <seconds>', 'The amount of time to to wait between retries', 1);
return program;
}
/**
* Load configuration options from users .ummonrc file
*
* @param {Object} program The commander object
* @return {Object} ummon configuration object
*/
var loadLocalConfig = exports.loadLocalConfig = function(program) {
var config = require('rc')('ummon', {}, {});
if (!program.env) {
program.env = config.defaultEnv;
}
// Bring env settings into the root
return _.extend(program, config[program.env])
}
/**
* Helper to quickly setup each task
*
* @param {Object} program The commander object
* @param {Object} argv Process argv
* @return {Object} Fully modified commander object
*/
exports.parse = function(program, argv) {
program = addGlobalOptions(program);
program.parse(argv);
program = loadLocalConfig(program);
return program
}