From dbdc26367af3cf566ce8f78592103f7890a15683 Mon Sep 17 00:00:00 2001 From: Andrew Goode Date: Mon, 13 Jun 2016 21:22:52 -0400 Subject: [PATCH] feat: lookup host ip if none given (#14) --- README.md | 41 +++++++++++++++++++++++++++++++++++++++-- bin/which-cloud.js | 11 ++++++----- index.js | 35 ++++++++++++++++++++++++----------- package.json | 1 + test/cli.js | 8 +++++--- 5 files changed, 75 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index eb497bd..bb9dbfe 100644 --- a/README.md +++ b/README.md @@ -7,16 +7,53 @@ given an ip address, return which cloud provider it belongs to (EC2, GCE, etc) ```sh -> which-cloud 104.196.27.39 -> gce +$ which-cloud 104.196.27.39 +gce +``` + +if no ip is given, `which-cloud` will use the public ip of the current host + +```sh +$ which-cloud +AT&T Internet Services (SIS-80) ``` ## Installing +### CLI + ```sh npm i which-cloud -g ``` +```sh +which-cloud +``` + +### Module + +```sh +npm i which-cloud --save +``` + +```js +const whichCloud = require('which-cloud') +``` + +## API + +### `whichCloud([ip,] callback)` + +- `ip`: string, optional + + Determine the cloud provider for this ip + + If no ip is given, the public ip of the current host will be used + +- `callback`: function, required + + Called with an `Error` or the determined cloud provider as a string + ## Supported Clouds * Amazon Web Services (aws). diff --git a/bin/which-cloud.js b/bin/which-cloud.js index c62b856..bcf32f4 100755 --- a/bin/which-cloud.js +++ b/bin/which-cloud.js @@ -2,14 +2,12 @@ const which = require('../') const argv = require('yargs') - .usage('$0 ') + .usage('$0 [ip]') .help() .alias('help', 'h') - .demand(1, 'you must provide an ip to lookup') .argv -const ip = argv._[0] -which(ip, function (err, cloud) { +function cb (err, cloud) { if (err) { console.log(which.default) process.exit(1) @@ -17,4 +15,7 @@ which(ip, function (err, cloud) { console.log(cloud) process.exit(0) } -}) +} + +if (argv._.length) which(argv._[0], cb) +else which(cb) diff --git a/index.js b/index.js index ad54f52..29eaa85 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const providers = [ require('./lib/gce') ] const whois = require('./lib/whois') +const publicIp = require('public-ip') function WhichCloud (ip, done) { var name = WhichCloud.default @@ -25,19 +26,31 @@ function WhichCloud (ip, done) { return checkers } - parallel(checkersGenerator(ip), function (err) { - if (err) return done(err) + function runCheckers (ip, done) { + parallel(checkersGenerator(ip), function (err) { + if (err) return done(err) - if (name === WhichCloud.default) { - whois(ip).org(function (err, _name) { - if (err) return done(err) - if (_name) name = _name + if (name === WhichCloud.default) { + whois(ip).org(function (err, _name) { + if (err) return done(err) + if (_name) name = _name + return done(null, name) + }) + } else { return done(null, name) - }) - } else { - return done(null, name) - } - }) + } + }) + } + + if (typeof ip === 'function') { + done = ip + publicIp.v4(function (err, hostIp) { + if (err) return done(err) + runCheckers(hostIp, done) + }) + } else { + runCheckers(ip, done) + } } WhichCloud.default = 'unknown' diff --git a/package.json b/package.json index be23f5a..cb28ea3 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "gce-ips": "^1.0.2", "ip-range-check": "0.0.1", "lodash.assign": "^4.0.9", + "public-ip": "^1.2.0", "request": "^2.72.0", "whois": "^2.1.6", "xml2js": "^0.4.16", diff --git a/test/cli.js b/test/cli.js index fbb91b1..5468d55 100644 --- a/test/cli.js +++ b/test/cli.js @@ -1,13 +1,15 @@ /* global describe, it */ -require('chai').should() +const should = require('chai').should() const exec = require('child_process').exec describe('cli', function () { - it('displays help if no ip is provided', function (done) { + it('looks up host ip if no ip is provided', function (done) { exec('./bin/which-cloud.js', function (err, stdout, stderr) { - err.message.should.match(/you must provide an ip to lookup/) + should.not.exist(err) + stderr.should.be.empty + stdout.should.be.ok return done() }) })