Skip to content

Commit

Permalink
Merge pull request #133 from mapbox/cli2
Browse files Browse the repository at this point in the history
Overhaul cli
  • Loading branch information
Ryan Clark committed Jun 13, 2015
2 parents a9da6ed + d95cfd1 commit 3900909
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 112 deletions.
12 changes: 8 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- '0.10'
- '0.12'
sudo: false
script:
- npm test
- npm run-script coverage
- npm test
- npm run-script coverage
env:
global:
- secure: iEgUpSVRZuvHapWT8fdVDyvRnxKkWXFpXRbWrP3z1qzW81fjlzkV48iYW7vRvS93P2hwjS8Bf5JY7deqgEy/FICEjLO46ztBcwD8aMAc3rmwgoMbdXZr1Rd1ftXWlKe8nex6uwV80nVwIFIqwXRofBM0AKY1WpBXEagm20RF59o=
- secure: HUWHLKJNDx/zMDPsEQNg32/VJmzbV3Fv2dFnjLefEbThUJ7jdV50wiozV2p4Fgzm56wDwJHdDP8HaDzu6PEBL1Apszn4KjFyVnR475K0JRPArnJCVNEPawsj1tAXnC4FTB+nyn1uf45DHDBij1kwRyYfUstKEKasFg/6jL8qcv4=
24 changes: 0 additions & 24 deletions CLI.md

This file was deleted.

83 changes: 0 additions & 83 deletions bin/cardboard

This file was deleted.

147 changes: 147 additions & 0 deletions bin/cardboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#!/usr/bin/env node

var Cardboard = require('../');
var parser = require('geojson-stream').parse();
var collector = require('geojson-stream').stringify();
var stream = require('stream');
var args = require('minimist')(process.argv.slice(2));

function configLoader(args, env) {
var config = {};

config.region = args.region || env.CardboardRegion;
if (!config.region) throw new Error('You must provide a region');

config.table = args.table || env.CardboardTable;
if (!config.table) throw new Error('You must provide a table name');

config.bucket = args.bucket || env.CardboardBucket;
if (!config.bucket) throw new Error('You must provide an S3 bucket');

config.prefix = args.prefix || env.CardboardPrefix;
if (!config.prefix) throw new Error('You must provide an S3 prefix');

if (args.endpoint || env.CardboardEndpoint) {
config.endpoint = args.endpoint || env.CardboardEndpoint;
}

return config;
}

var config;
try { config = configLoader(args, process.env); }
catch (err) {
console.error(err.message);
process.exit(1);
}

var command = args._[0];
if (['put', 'get', 'list', 'bbox'].indexOf(command) < 0) {
console.error(command + ' is not a valid command');
process.exit(1);
}

var dataset = args._[1];
if (!dataset) {
console.error('You must provide the name of the dataset to interact with');
process.exit(1);
}

var id = args._[2];
if (command === 'get' && !id) {
console.error('You must provide the id of the feature to get');
process.exit(1);
}

if (command === 'bbox') {
var bbox = process.argv.slice(2).filter(function(arg) {
return arg.split(',').length === 4;
})[0];

if (!bbox) {
console.error('You must provide a bounding box for your query');
process.exit(1);
}

bbox = bbox.split(',').map(function(coord) {
return Number(coord);
});
}

var cardboard = Cardboard(config);

cardboard.createTable(function(err){
if (err) throw err;

if (command === 'get') {
return cardboard.get(id, dataset, function(err, item) {
if (err) throw err;
console.log(JSON.stringify(item));
});
}

if (command === 'list') {
return cardboard.list(dataset)
.on('error', function(err) { throw err; })
.pipe(collector)
.on('error', function(err) { throw err; })
.pipe(process.stdout);
}

if (command === 'bbox') {
return cardboard.bboxQuery(bbox, dataset, function(err, collection) {
if (err) throw err;
console.log(JSON.stringify(collection));
});
}

if (command === 'put') {
var aggregator = new stream.Writable({ objectMode: true, highWaterMark: 75 });

aggregator.features = [];
aggregator.count = 0;
aggregator.pending = 0;

aggregator.collection = function() {
return {
type: 'FeatureCollection',
features: aggregator.features
};
};

aggregator._write = function(feature, enc, callback) {
aggregator.features.push(feature);
if (aggregator.features.length < 25) return callback();

aggregator.pending++;
cardboard.batch.put(aggregator.collection(), dataset, function(err) {
aggregator.pending--;
if (err) return callback(err);
aggregator.count += aggregator.features.length;
aggregator.features = [];
callback();
});
};

aggregator.done = aggregator.end.bind(aggregator);

aggregator.end = function() {
if (aggregator.pending) return setImmediate(aggregator.end);
if (!aggregator.features.length) return aggregator.done();

cardboard.batch.put(aggregator.collection(), dataset, function(err) {
if (err) return aggregator.emit('error', err);
aggregator.done();
});
};

return process.stdin
.pipe(parser)
.on('error', function(err) { throw err; })
.pipe(aggregator)
.on('error', function(err) { throw err; })
.on('finish', function() {
console.log('Inserted %s features', aggregator.count);
});
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"coverage": "istanbul cover test/index.js && coveralls < ./coverage/lcov.info"
},
"bin": {
"cardboard": "./bin/cardboard"
"cardboard": "./bin/cardboard.js"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 3900909

Please sign in to comment.