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

Convert tasks.js to yargs #210

Merged
merged 5 commits into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"dependencies": {
"@google-cloud/datastore": "^0.1.1",
"async": "^2.0.1"
"async": "^2.0.1",
"yargs": "^5.0.0"
},
"devDependencies": {
"mocha": "^3.0.2"
Expand Down
131 changes: 59 additions & 72 deletions datastore/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

'use strict';

var input = process.argv.splice(2);
var command = input.shift();

// [START build_service]
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
Expand Down Expand Up @@ -60,7 +57,7 @@ export GCLOUD_PROJECT=<project-id>

7. Run the application!
```sh
npm run tasks
node tasks <command>
```
*/

Expand Down Expand Up @@ -175,78 +172,68 @@ function formatTasks (tasks) {
}
// [END format_results]

if (module === require.main) {
var taskId;

switch (command) {
case 'new': {
addTask(input, function (err, taskKey) {
if (err) {
throw err;
}

taskId = taskKey.path.pop();

console.log('Task %d created successfully.', taskId);
});

break;
}
case 'done': {
taskId = parseInt(input, 10);

markDone(taskId, function (err) {
if (err) {
throw err;
}

console.log('Task %d updated successfully.', taskId);
});
var cli = require('yargs');
var makeHandler = require('../utils').makeHandler;

var program = module.exports = {
addEntity: addTask,
updateEntity: markDone,
retrieveEntities: listTasks,
deleteEntity: deleteTask,
formatTasks: formatTasks,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};

break;
}
case 'list': {
listTasks(function (err, tasks) {
if (err) {
throw err;
}
cli
.demand(1)
.command('new <description>', 'Adds a task with a description <description>.', {}, function (options) {
addTask(options.description, function (err, taskKey) {
if (err) {
throw err;
}

console.log(formatTasks(tasks));
});
var taskId = taskKey.path.pop();
console.log('Task %d created successfully.', taskId);
});
})
.command('done <taskId>', 'Marks the specified task as done.', {}, function (options) {
markDone(options.taskId, function (err) {
if (err) {
throw err;
}

break;
}
case 'delete': {
taskId = parseInt(input, 10);
console.log('Task %d updated successfully.', options.taskId);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this logic go in the parent function (e.g. markDone) as it did in the BigQuery samples, or in the yargs handler?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should go in the markDone function.

});
})
.command('list', 'Lists all tasks ordered by creation time.', {}, function (options) {
listTasks(function (err, tasks) {
if (err) {
throw err;
}

deleteTask(taskId, function (err) {
if (err) {
throw err;
}
console.log(formatTasks(tasks));
});
})
.command('delete <taskId>', 'Deletes a task.', {}, function (options) {
deleteTask(options.taskId, function (err) {
if (err) {
throw err;
}

console.log('Task %d deleted successfully.', taskId);
});
console.log('Task %d deleted successfully.', options.taskId);
});
})
.example('node $0 new "Buy milk"', 'Adds a task with description "Buy milk".')
.example('node $0 done 12345', 'Marks task 12345 as Done.')
.example('node $0 list', 'Lists all tasks ordered by creation time')
.example('node $0 delete 12345', 'Deletes task 12345.')
.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/datastore/docs')

break;
}
default: {
// Only print usage if this file is being executed directly
if (module === require.main) {
console.log([
'Usage:',
'',
' new <description> Adds a task with a description <description>',
' done <task-id> Marks a task as done',
' list Lists all tasks by creation time',
' delete <task-id> Deletes a task'
].join('\n'));
}
}
}
if (module === require.main) {
program.main(process.argv.slice(2));
}

module.exports.addEntity = addTask;
module.exports.updateEntity = markDone;
module.exports.retrieveEntities = listTasks;
module.exports.deleteEntity = deleteTask;
module.exports.formatTasks = formatTasks;