Browse Source

add start to cli tool

pull/14/head
Brett Langdon 11 years ago
parent
commit
17da0c0f6b
4 changed files with 124 additions and 3 deletions
  1. +70
    -0
      bin/dogapi
  2. +10
    -1
      lib/api/infrastructure.js
  3. +38
    -1
      lib/api/metric.js
  4. +6
    -1
      package.json

+ 70
- 0
bin/dogapi View File

@ -0,0 +1,70 @@
#!/usr/bin/env node
var dogapi = require("../");
var minimist = require("minimist");
var rc = require("rc");
var config = rc("dogapi", {
api_key: null,
app_key: null
});
dogapi.initialize(config);
var usage = [
"Usage:",
"${command} --help",
"${command} --version",
"${command} now",
"${command} then <seconds-ago>"
];
var help = [];
for(var key in dogapi){
if(!dogapi.hasOwnProperty(key)){
continue;
} else if(!dogapi[key].hasOwnProperty("getUsage") || typeof dogapi[key].getUsage !== "function"){
continue;
} else if(!dogapi[key].hasOwnProperty("handleCli") || typeof dogapi[key].handleCli !== "function"){
continue;
}
usage = usage.concat(dogapi[key].getUsage());
if(dogapi[key].hasOwnProperty("getHelp") && typeof dogapi[key].getHelp === "function"){
help = help.concat(["\r\n"], dogapi[key].getHelp());
}
}
usage = usage.concat(help);
usage = usage.join("\r\n").replace(/\$\{command\}/g, " dogapi");
var args = minimist(process.argv);
var command = args._[2];
var subcommand = args._[3];
if(command === "now"){
console.log(dogapi.now());
} else if(command === "then" && args._.length > 3){
console.log(dogapi.now() - parseInt(args._[args._.length - 1]));
} else if(dogapi.hasOwnProperty(command)){
if(subcommand && dogapi[command].hasOwnProperty(subcomand)){
dogapi[command].handleCli(subcommand, args, function(err, res){
if(err){
console.error(err);
process.exit(1);
} else {
if(res === ""){
res = "success";
}
console.log(res);
}
});
} else {
var commandUsage = ["Usage:"].concat(dogapi[command].getUsage());
if(dogapi[command].hasOwnProperty("getHelp") && typeof dogapi[command].getHelp === "function"){
commandUsage = commandUsage.concat(["\r\n"], dogapi[command].getHelp());
}
console.log(commandUsage.join("\r\n").replace(/\$\{command\}/g, " dogapi"));
}
} else if(args.version){
console.log(require("../package.json").version);
} else {
console.log(usage);
}

+ 10
- 1
lib/api/infrastructure.js View File

@ -32,5 +32,14 @@ function search(query, callback){
}
module.exports = {
search: search
search: search,
getUsage: function(){
return [
"${command} infrastructure search <query>"
]
},
handleCli: function(subcommand, args, callback){
var query = args._[4];
search(query, callback);
}
};

+ 38
- 1
lib/api/metric.js View File

@ -139,5 +139,42 @@ function query(from, to, query, callback){
module.exports = {
send: send,
send_all: send_all
send_all: send_all,
query: query,
getUsage: function(){
return [
"${command} metric send <metric> <point> [--tags <tags>] [--host <host>] [--type <type>]",
"${command} metric query <from> <to> <query>"
]
},
getHelp: function(){
return [
"Metric Options:",
" --tags <tags> a comma separated list of \"tag:value\"'s",
" --host <host> the hostname that should be associated with this metric",
" --type <type> the type of metric \"gauge\" or \"counter\""
]
},
handleCli: function(subcommand, args, callback){
if(subcommand === "send"){
var extra = {};
if(args["--tags"]){
extra.tags = args["--tags"].split(",");
}
if(args["--host"]){
extra.host = args["--host"];
}
if(args["--type"]){
extra.metric_type = args["--type"];
}
send(args["<metric>"], args["<point>"], extra, callback);
} else if(subcommand === "query" && args._.length > 6){
var from = parseInt(args._[4]);
var to = parseInt(args._[5]);
var q = args._[6];
query(from, to, q, callback);
} else {
callback("unknown subcommand or arguments try `dogapi --help` for help", false);
}
}
};

+ 6
- 1
package.json View File

@ -7,6 +7,9 @@
"test": "echo \"Error: no test specified\" && exit 1",
"docs": "node ./docs/create.js > index.html"
},
"bin": {
"dogapi": "bin/dogapi"
},
"repository": {
"type": "git",
"url": "git://github.com/brettlangdon/node-dogapi"
@ -28,7 +31,9 @@
"readmeFilename": "README.md",
"gitHead": "f388635a5ab4f4da25702dc0999385d437bdf2bc",
"dependencies": {
"extend": "^2.0.0"
"extend": "^2.0.0",
"minimist": "^1.1.1",
"rc": "^1.0.0"
},
"devDependencies": {
"docast": "^0.1.1",


Loading…
Cancel
Save