From 1aef05b59c51c2e515b0cee03e8b8c45a9108def Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 30 Jan 2013 14:56:37 +0100 Subject: //util with: initial commit --- util/README.markdown | 27 +++++++++++++++++++++++++++ util/bin/with | 6 ++++++ 2 files changed, 33 insertions(+) create mode 100644 util/README.markdown create mode 100755 util/bin/with (limited to 'util') diff --git a/util/README.markdown b/util/README.markdown new file mode 100644 index 00000000..7d6a9774 --- /dev/null +++ b/util/README.markdown @@ -0,0 +1,27 @@ +# various utils + +## //util/bin/with + + execute a command with an extended/modified environment + +### usage + + with ENV COMMAND + + where `ENV` is the name of the environment and + `COMMAND` your to-be-executed command (-line). + +### environment + + - `env_dir` defines the directory where environment files are searched. + Default: `$HOME/.env.d`. + +### example + + cat > ~/.with/frh-ire < Date: Wed, 30 Jan 2013 15:01:25 +0100 Subject: //util README: fix example --- util/README.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'util') diff --git a/util/README.markdown b/util/README.markdown index 7d6a9774..78b9b81a 100644 --- a/util/README.markdown +++ b/util/README.markdown @@ -13,12 +13,12 @@ ### environment - - `env_dir` defines the directory where environment files are searched. - Default: `$HOME/.env.d`. + `env_dir` defines the directory where environment files are searched + (default: `$HOME/.env.d`). ### example - cat > ~/.with/frh-ire < ~/.env.d/frh-ire < Date: Sat, 2 Feb 2013 00:50:20 +0100 Subject: //util geo: initial commit --- util/lib/geo/Makefile | 15 +++++++++++++++ util/lib/geo/index.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++ util/lib/geo/package.json | 7 +++++++ 3 files changed, 70 insertions(+) create mode 100644 util/lib/geo/Makefile create mode 100644 util/lib/geo/index.js create mode 100644 util/lib/geo/package.json (limited to 'util') diff --git a/util/lib/geo/Makefile b/util/lib/geo/Makefile new file mode 100644 index 00000000..d13cd471 --- /dev/null +++ b/util/lib/geo/Makefile @@ -0,0 +1,15 @@ +all: node_modules GeoLiteCity.dat + +node_modules: package.json + npm install + @touch -r $< $@ + +GeoLiteCity.dat: + @test -e GeoLiteCity.dat && ln -s GeoLiteCity.dat $@ || { \ + echo 'No GeoIP City database found.'; \ + echo 'You can get one from http://dev.maxmind.com/geoip/geolite:'; \ + echo ' wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz'; \ + echo ' gunzip GeoLiteCity.dat.gz'; \ + exit 23; \ + } + diff --git a/util/lib/geo/index.js b/util/lib/geo/index.js new file mode 100644 index 00000000..0763b1a4 --- /dev/null +++ b/util/lib/geo/index.js @@ -0,0 +1,48 @@ +function slurp (stream, callback) { + var data = [] + stream.on('data', function (chunk) { + data.push(chunk) + }) + stream.on('end', function () { + callback(null, Buffer.concat(data)) + }) + stream.resume() +} + + +var path = require('path') +var city_dat = path.join(__dirname, 'GeoLiteCity.dat') + +var geoip = require('geoip') +var city = new geoip.City(city_dat) + +slurp(process.stdin, function (err, data) { + var lines = data.toString().split('\n') + // remove last, empty element (caused by the [mandatory] final \n) + if (lines.length > 1 && lines[lines.length - 1] === '') { + lines.pop() + } + var name = 0, addr = 1 + lines + .map(function (line) { return line.split(' ') }) + .forEach(function (host) { + //city.lookup(host[addr], function (err, loc) { + // if (err) { + // console.error('#', host[name], err.message) + // } else { + // console.log(host[name] + ': ' + loc.longitude + ',' + loc.latitude) + // } + //}) + var loc = city.lookupSync(host[addr]) + //if (!loc) { console.error('#', host[name]) } + if (loc) { + var a = loc.latitude + var b = loc.longitude + //var c = loc.altitude + //var geo = 'geo:' + [a,b,c].join(',') + var geo = 'geo:' + [a,b].join(',') + + console.log(host[name], geo) + } + }) +}) diff --git a/util/lib/geo/package.json b/util/lib/geo/package.json new file mode 100644 index 00000000..ad449a62 --- /dev/null +++ b/util/lib/geo/package.json @@ -0,0 +1,7 @@ +{ + "name": "geo", + "version": "0.0.0", + "dependencies": { + "geoip": "*" + } +} -- cgit v1.2.3