mirror of
https://github.com/samsonjs/kwikemon.git
synced 2026-06-29 05:39:29 +00:00
167 lines
No EOL
3.3 KiB
JavaScript
Executable file
167 lines
No EOL
3.3 KiB
JavaScript
Executable file
#!/usr/bin/env node
|
|
|
|
var kwikemon = require('../kwikemon.js');
|
|
|
|
var commands = {};
|
|
function defineCommand(name, handler, args) {
|
|
commands[name] = { handler: handler, args: args };
|
|
}
|
|
defineCommand('clear', clear);
|
|
defineCommand('count', count);
|
|
defineCommand('get', get, 'name');
|
|
defineCommand('help', usage);
|
|
defineCommand('list', list);
|
|
defineCommand('remove', remove, 'name');
|
|
defineCommand('set', set, 'name text');
|
|
defineCommand('sweep', sweep);
|
|
defineCommand('ttl', ttl, 'name [ttl]');
|
|
defineCommand('write', write, 'name');
|
|
|
|
function main() {
|
|
var cmd = process.argv[2]
|
|
, handler = commands[cmd] && commands[cmd].handler
|
|
;
|
|
if (handler) {
|
|
handler.apply(null, process.argv.slice(3));
|
|
}
|
|
else {
|
|
if (cmd) {
|
|
console.log('error: unknown command ' + cmd);
|
|
}
|
|
usage();
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function clear() {
|
|
kwikemon.clear(function(err) {
|
|
process.exit(0);
|
|
});
|
|
}
|
|
|
|
function count() {
|
|
kwikemon.count(function(err, n) {
|
|
console.log(n);
|
|
process.exit(0);
|
|
});
|
|
}
|
|
|
|
function expire(name, ttl) {
|
|
kwikemon.ttl(name, ttl, function(err) {
|
|
if (err && err.message == 'not found') {
|
|
console.log('no monitor named', name);
|
|
process.exit(1);
|
|
}
|
|
else if (err) {
|
|
console.log('error: ' + (err.message || err));
|
|
process.exit(1);
|
|
}
|
|
else {
|
|
process.exit(0);
|
|
}
|
|
});
|
|
}
|
|
|
|
function get(name) {
|
|
if (name) {
|
|
kwikemon.get(name, function(err, mon) {
|
|
if (mon) {
|
|
console.log(mon.text);
|
|
process.exit(0);
|
|
}
|
|
else {
|
|
console.log('no monitor named', name);
|
|
process.exit(1);
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
console.log('get requires a name');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function list() {
|
|
kwikemon.getAll(function(err, monitors) {
|
|
Object.keys(monitors).forEach(function(name) {
|
|
console.log(name + ':', monitors[name].text);
|
|
});
|
|
process.exit(0);
|
|
});
|
|
}
|
|
|
|
function remove(name) {
|
|
if (name) {
|
|
kwikemon.remove(name, function(err) {
|
|
process.exit(0);
|
|
})
|
|
}
|
|
else {
|
|
console.log('remove requires a name');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function set(name, text) {
|
|
if (name && text) {
|
|
kwikemon.set(name, text, function() {
|
|
process.exit(0);
|
|
});
|
|
}
|
|
else {
|
|
console.log('set requires a name and some text')
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function sweep() {
|
|
kwikemon.sweep(function(err) {
|
|
process.exit(0);
|
|
});
|
|
}
|
|
|
|
function ttl(name, ttl) {
|
|
if (ttl) return expire(name, Number(ttl));
|
|
if (name) {
|
|
kwikemon.ttl(name, function(err, ttl) {
|
|
if (typeof ttl == 'number') {
|
|
console.log(ttl);
|
|
process.exit(0);
|
|
}
|
|
else {
|
|
console.log('no monitor named', name);
|
|
process.exit(1);
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
console.log('ttl requires a name');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function write(name) {
|
|
if (name) {
|
|
process.stdin.pipe(kwikemon.writer(name));
|
|
process.stdin.on('end', function() {
|
|
process.exit(0);
|
|
});
|
|
}
|
|
else {
|
|
console.log('write requires a name');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
function usage() {
|
|
console.log('usage: kwikemon <command> [args...]');
|
|
console.log('commands:');
|
|
Object.keys(commands).sort().forEach(function(name) {
|
|
var args = commands[name].args || '';
|
|
console.log(' ' + name + ' ' + args);
|
|
});
|
|
}
|
|
|
|
if (require.main == module) {
|
|
main();
|
|
} |