Skip to content

Commit

Permalink
feat: lookup host ip if none given (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexdrew authored and bcoe committed Jun 14, 2016
1 parent ea9375b commit dbdc263
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 21 deletions.
41 changes: 39 additions & 2 deletions README.md
Expand Up @@ -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).
Expand Down
11 changes: 6 additions & 5 deletions bin/which-cloud.js
Expand Up @@ -2,19 +2,20 @@

const which = require('../')
const argv = require('yargs')
.usage('$0 <ip>')
.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)
} else {
console.log(cloud)
process.exit(0)
}
})
}

if (argv._.length) which(argv._[0], cb)
else which(cb)
35 changes: 24 additions & 11 deletions index.js
Expand Up @@ -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
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions 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()
})
})
Expand Down

0 comments on commit dbdc263

Please sign in to comment.