Created
February 24, 2011 12:54
-
-
Save swdyh/842127 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var rl = require('readline') | |
var KyotoTycoon = require('kyoto-tycoon').KyotoTycoon | |
var commands = [ | |
'report', 'status', 'clear', 'set', 'add', 'replace', 'append', | |
'remove', 'get', | |
] | |
var kt = new KyotoTycoon() | |
var rli = rl.createInterface(process.stdin, process.stdout) | |
rli.setPrompt('kt> ') | |
rli.prompt() | |
rli.on('SIGINT', function() { | |
rli.close() | |
process.exit() | |
}) | |
rli.addListener('line', function(cmd) { | |
var tmp = cmd.replace(/^[ ]+|[ ]+$/g, '').split(/\s+/) | |
var cmd = tmp.shift() | |
if (commands.indexOf(cmd) >= 0) { | |
tmp.push(function(err, data) { | |
if (err) { | |
console.log('error: ' + err.message.replace(/\n$/, '')) | |
} | |
else if (JSON.stringify(data) != '{}') { | |
console.log(data) | |
} | |
rli.prompt() | |
}) | |
kt[cmd].apply(kt, tmp) | |
} | |
else { | |
if (cmd == 'help') { | |
console.log('commands:') | |
console.log(JSON.stringify(commands)) | |
console.log('example:') | |
console.log(' kt > set foo 100') | |
console.log(' kt > get foo') | |
} | |
else if (cmd != '') { | |
console.log('error') | |
} | |
rli.prompt() | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment