Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: lookup host ip if none given #14

Merged
merged 1 commit into from
Jun 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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