diff --git a/bin/dogapi b/bin/dogapi new file mode 100755 index 0000000..d872d89 --- /dev/null +++ b/bin/dogapi @@ -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 " +]; +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); +} diff --git a/lib/api/infrastructure.js b/lib/api/infrastructure.js index 4ed7d29..73e1deb 100644 --- a/lib/api/infrastructure.js +++ b/lib/api/infrastructure.js @@ -32,5 +32,14 @@ function search(query, callback){ } module.exports = { - search: search + search: search, + getUsage: function(){ + return [ + "${command} infrastructure search " + ] + }, + handleCli: function(subcommand, args, callback){ + var query = args._[4]; + search(query, callback); + } }; diff --git a/lib/api/metric.js b/lib/api/metric.js index 472ae88..bf46145 100644 --- a/lib/api/metric.js +++ b/lib/api/metric.js @@ -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 [--tags ] [--host ] [--type ]", + "${command} metric query " + ] + }, + getHelp: function(){ + return [ + "Metric Options:", + " --tags a comma separated list of \"tag:value\"'s", + " --host the hostname that should be associated with this metric", + " --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[""], args[""], 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); + } + } }; diff --git a/package.json b/package.json index fc139a3..2c65bbd 100644 --- a/package.json +++ b/package.json @@ -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",