Skip to content

Commit

Permalink
Switch to yargs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Aug 26, 2016
1 parent 2c780b4 commit 42cd92b
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 290 deletions.
27 changes: 27 additions & 0 deletions bigquery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ analytics data warehouse.
* [Samples](#samples)
* [Create A Simple Application With the API](#create-a-simple-application-with-the-api)
* [Datasets](#datasets)
* [Queries](#queries)
* [Tables](#tables)

## Setup
Expand Down Expand Up @@ -72,6 +73,32 @@ For more information, see https://cloud.google.com/bigquery/docs
[datasets_docs]: https://cloud.google.com/bigquery/docs
[datasets_code]: datasets.js

### Queries

View the [documentation][queries_docs] or the [source code][queries_code].

__Usage:__ `node queries --help`

```
Commands:
sync <query> Run a synchronous query.
async <query> Start an asynchronous query.
poll <jobId> Get the status of a job.
Options:
--help Show help [boolean]
Examples:
node queries sync "SELECT * FROM publicdata:samples.natality LIMIT 5;"
node queries async "SELECT * FROM publicdata:samples.natality LIMIT 5;"
node queries poll 12345
For more information, see https://cloud.google.com/bigquery/docs
```

[queries_docs]: https://cloud.google.com/bigquery/docs
[queries_code]: queries.js

### Tables

View the [documentation][tables_docs] or the [source code][tables_code].
Expand Down
66 changes: 28 additions & 38 deletions bigquery/query.js → bigquery/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// [START complete]
// [START all]
/**
* Command-line application to perform an synchronous query in BigQuery.
*
Expand Down Expand Up @@ -127,51 +127,41 @@ function asyncPoll (jobId, callback) {
});
}
// [END async_query]
// [END all]

// [START usage]
function printUsage () {
console.log('Usage:');
console.log('\nCommands:\n');
console.log('\tnode query sync QUERY');
console.log('\tnode query async QUERY');
console.log('\tnode query poll JOB_ID');
console.log('\nExamples:\n');
console.log('\tnode query sync "SELECT * FROM publicdata:samples.natality LIMIT 5;"');
console.log('\tnode query async "SELECT * FROM publicdata:samples.natality LIMIT 5;"');
console.log('\tnode query poll 12345');
}
// [END usage]

// The command-line program:
var program = {
// Print usage instructions.
printUsage: printUsage,
// The command-line program
var cli = require('yargs');
var makeHandler = require('../utils').makeHandler;

// Exports
var program = module.exports = {
asyncQuery: asyncQuery,
asyncPoll: asyncPoll,
syncQuery: syncQuery,
bigquery: bigquery,

// Run the sample.
main: function (args, cb) {
var command = args.shift();
var arg = args.shift();
if (command === 'sync') {
this.syncQuery(arg, cb);
} else if (command === 'async') {
this.asyncQuery(arg, cb);
} else if (command === 'poll') {
this.asyncPoll(arg, cb);
} else {
this.printUsage();
}
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};

cli
.demand(1)
.command('sync <query>', 'Run a synchronous query.', {}, function (options) {
program.syncQuery(options.query, makeHandler());
})
.command('async <query>', 'Start an asynchronous query.', {}, function (options) {
program.asyncQuery(options.query, makeHandler());
})
.command('poll <jobId>', 'Get the status of a job.', {}, function (options) {
program.asyncPoll(options.jobId, makeHandler());
})
.example('node $0 sync "SELECT * FROM publicdata:samples.natality LIMIT 5;"')
.example('node $0 async "SELECT * FROM publicdata:samples.natality LIMIT 5;"')
.example('node $0 poll 12345')
.wrap(80)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/bigquery/docs');

if (module === require.main) {
program.main(process.argv.slice(2), console.log);
program.main(process.argv.slice(2));
}
// [END complete]

module.exports = program;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

'use strict';

var example = require('../query');
var example = require('../queries');

describe('bigquery:query', function () {
describe('sync_query', function () {
Expand Down
85 changes: 29 additions & 56 deletions bigquery/test/query.test.js → bigquery/test/queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function getSample () {
var BigQueryMock = sinon.stub().returns(bigqueryMock);

return {
program: proxyquire('../query', {
program: proxyquire('../queries', {
'@google-cloud/bigquery': BigQueryMock
}),
mocks: {
Expand All @@ -58,49 +58,6 @@ function getSample () {
}

describe('bigquery:query', function () {
describe('main', function () {
it('should show usage based on arguments', function () {
var program = getSample().program;
sinon.stub(program, 'printUsage');

program.main([]);
assert(program.printUsage.calledOnce);

program.main(['-h']);
assert(program.printUsage.calledTwice);

program.main(['--help']);
assert(program.printUsage.calledThrice);
});

it('should run the correct commands', function () {
var program = getSample().program;
sinon.stub(program, 'syncQuery');
sinon.stub(program, 'asyncQuery');
sinon.stub(program, 'asyncPoll');

program.main(['sync']);
assert(program.syncQuery.calledOnce);

program.main(['async']);
assert(program.asyncQuery.calledOnce);

program.main(['poll']);
assert(program.asyncPoll.calledOnce);
});

it('should execute queries', function () {
var example = getSample();
sinon.stub(example.program, 'syncQuery');

example.program.main(['foo'], function (err, data) {
assert.ifError(err);
assert(example.program.syncQuery.calledWith({ query: 'foo' }));
assert.deepEqual(data, example.mocks.natality);
});
});
});

describe('syncQuery', function () {
var query = 'foo';

Expand Down Expand Up @@ -246,19 +203,35 @@ describe('bigquery:query', function () {
});
});

describe('printUsage', function () {
it('should print usage', function () {
describe('main', function () {
var query = 'foo';
var jobId = 'foo';

it('should call syncQuery', function () {
var program = getSample().program;
program.printUsage();
assert(console.log.calledWith('Usage:'));
assert(console.log.calledWith('\nCommands:\n'));
assert(console.log.calledWith('\tnode query sync QUERY'));
assert(console.log.calledWith('\tnode query async QUERY'));
assert(console.log.calledWith('\tnode query poll JOB_ID'));
assert(console.log.calledWith('\nExamples:\n'));
assert(console.log.calledWith('\tnode query sync "SELECT * FROM publicdata:samples.natality LIMIT 5;"'));
assert(console.log.calledWith('\tnode query async "SELECT * FROM publicdata:samples.natality LIMIT 5;"'));
assert(console.log.calledWith('\tnode query poll 12345'));

sinon.stub(program, 'syncQuery');
program.main(['sync', query]);
assert.equal(program.syncQuery.calledOnce, true);
assert.deepEqual(program.syncQuery.firstCall.args.slice(0, -1), [query]);
});

it('should call asyncQuery', function () {
var program = getSample().program;

sinon.stub(program, 'asyncQuery');
program.main(['async', query]);
assert.equal(program.asyncQuery.calledOnce, true);
assert.deepEqual(program.asyncQuery.firstCall.args.slice(0, -1), [query]);
});

it('should call asyncPoll', function () {
var program = getSample().program;

sinon.stub(program, 'asyncPoll');
program.main(['poll', jobId]);
assert.equal(program.asyncPoll.calledOnce, true);
assert.deepEqual(program.asyncPoll.firstCall.args.slice(0, -1), [jobId]);
});
});
});
45 changes: 24 additions & 21 deletions pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,23 @@ View the [documentation][topics_docs] or the [source code][topics_code].
__Usage:__ `node topics --help`

```
Usage: node topics COMMAND [ARGS...]
Commands:
create <name> Create a new topic.
list List topics.
publish <topic> <message> Publish a message to the specified topic.
delete <name> Delete the specified topic.
create TOPIC_NAME
delete TOPIC_NAME
publish TOPIC_NAME MESSAGE
list
Options:
--help Show help [boolean]
Examples:
node topics create my-topic Create a new topic named "my-topic".
node topics list List topics.
node topics publish my-topic Publish a message to "my-topic".
'{"data":"Hello world!"}'
node topics delete my-topic Delete a topic named "my-topic".
node topics create my-topic
node topics list
node topics publish my-topic '{"data":"Hello world!"}'
node topics delete my-topic
For more information, see https://cloud.google.com/pubsub/docs
```

[topics_docs]: https://cloud.google.com/pubsub/publisher
Expand All @@ -61,22 +63,23 @@ View the [documentation][subscriptions_docs] or the [source code][subscriptions_
__Usage:__ `node subscriptions --help`

```
Usage: node subscriptions COMMAND [ARGS...]
Commands:
create <topic> <name> Create a new subscription.
list [topic] List subscriptions.
pull <subscription> Pull messages from the specified subscription.
delete <subscription> Delete the specified dataset.
create TOPIC_NAME SUBSCRIPTION_NAME
delete SUBSCRIPTION_NAME
pull SUBSCRIPTION_NAME
list [TOPIC_NAME]
Options:
--help Show help [boolean]
Examples:
node subscriptions create my-topic my-subscription Create a new subscription.
node subscriptions delete my-subscription Delete a subscription.
node subscriptions pull my-subscription Pull messages from "my-subscription".
node subscriptions list List all subscriptions.
node subscriptions list my-topic List subscriptions to topic "my-topic".
node subscriptions create my-topic my-subscription
node subscriptions delete my-subscription
node subscriptions pull my-subscription
node subscriptions list
node subscriptions list my-topic
For more information, see https://cloud.google.com/pubsub/docs
```

[subscriptions_docs]: https://cloud.google.com/pubsub/subscriber
Expand Down
3 changes: 2 additions & 1 deletion pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ../system-test/_setup.js system-test/*.test.js"
},
"dependencies": {
"@google-cloud/pubsub": "^0.1.1"
"@google-cloud/pubsub": "^0.1.1",
"yargs": "^5.0.0"
},
"devDependencies": {
"mocha": "^3.0.2",
Expand Down
Loading

0 comments on commit 42cd92b

Please sign in to comment.