From 1f581f1198b70015e67b6768a584b198153d85e3 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Sun, 11 Sep 2016 17:34:23 -0700 Subject: [PATCH 1/5] Add yargs to tasks.js --- datastore/package.json | 3 +- datastore/tasks.js | 129 ++++++++++++++++++----------------------- 2 files changed, 60 insertions(+), 72 deletions(-) diff --git a/datastore/package.json b/datastore/package.json index 1503dcf17e..06c325ce0e 100644 --- a/datastore/package.json +++ b/datastore/package.json @@ -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" diff --git a/datastore/tasks.js b/datastore/tasks.js index 27bf5b8da1..8b61ed0c5f 100755 --- a/datastore/tasks.js +++ b/datastore/tasks.js @@ -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 @@ -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 ', 'Adds a task with a 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 ', '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); + }); + }) + .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 ', '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 Adds a task with a description ', - ' done Marks a task as done', - ' list Lists all tasks by creation time', - ' delete 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; From f3d7e9db4f9834e4290c043d3a54f7372cdca018 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Sun, 11 Sep 2016 17:38:04 -0700 Subject: [PATCH 2/5] [Unrelated] Fix usage instructions --- datastore/tasks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datastore/tasks.js b/datastore/tasks.js index 8b61ed0c5f..fe1a525b44 100755 --- a/datastore/tasks.js +++ b/datastore/tasks.js @@ -57,7 +57,7 @@ export GCLOUD_PROJECT= 7. Run the application! ```sh -npm run tasks +node tasks ``` */ From b110bc474adac0c9500f6b8935b8bba0ac90b351 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Sun, 11 Sep 2016 17:47:13 -0700 Subject: [PATCH 3/5] Fix lint --- datastore/tasks.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/datastore/tasks.js b/datastore/tasks.js index fe1a525b44..310585981d 100755 --- a/datastore/tasks.js +++ b/datastore/tasks.js @@ -173,7 +173,6 @@ function formatTasks (tasks) { // [END format_results] var cli = require('yargs'); -var makeHandler = require('../utils').makeHandler; var program = module.exports = { addEntity: addTask, @@ -232,7 +231,7 @@ cli .example('node $0 delete 12345', 'Deletes task 12345.') .wrap(120) .recommendCommands() - .epilogue('For more information, see https://cloud.google.com/datastore/docs') + .epilogue('For more information, see https://cloud.google.com/datastore/docs'); if (module === require.main) { program.main(process.argv.slice(2)); From 29774f93ccdb3ca56d321616b413e163c1f53712 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Mon, 12 Sep 2016 12:32:03 -0700 Subject: [PATCH 4/5] Move logic into parent functions --- datastore/tasks.js | 81 +++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 55 deletions(-) diff --git a/datastore/tasks.js b/datastore/tasks.js index 310585981d..7f0667de9a 100755 --- a/datastore/tasks.js +++ b/datastore/tasks.js @@ -84,11 +84,12 @@ function addTask (description, callback) { ] }, function (err) { if (err) { - callback(err); - return; + return callback(err); } - callback(null, taskKey); + var taskId = taskKey.path.pop(); + console.log('Task %d created successfully.', taskId); + return callback(null, taskKey); }); } // [END add_entity] @@ -126,7 +127,8 @@ function markDone (taskId, callback) { } // The transaction completed successfully. - callback(); + console.log('Task %d updated successfully.', taskId); + return callback(null); }); }); }); @@ -138,7 +140,14 @@ function listTasks (callback) { var query = datastore.createQuery('Task') .order('created'); - datastore.runQuery(query, callback); + datastore.runQuery(query, function (err, tasks) { + if (err) { + return callback(err); + } + + console.log('Found %d task(s)!', tasks.length); + return callback(null, tasks); + }); } // [END retrieve_entities] @@ -149,37 +158,24 @@ function deleteTask (taskId, callback) { taskId ]); - datastore.delete(taskKey, callback); -} -// [END delete_entity] - -// [START format_results] -function formatTasks (tasks) { - return tasks - .map(function (task) { - var taskKey = task.key.path.pop(); - var status; - - if (task.data.done) { - status = 'done'; - } else { - status = 'created ' + new Date(task.data.created); - } + datastore.delete(taskKey, function (err) { + if (err) { + return callback(err); + } - return taskKey + ' : ' + task.data.description + ' (' + status + ')'; - }) - .join('\n'); + return callback(); + }); } -// [END format_results] +// [END delete_entity] 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; @@ -189,41 +185,16 @@ var program = module.exports = { cli .demand(1) .command('new ', 'Adds a task with a description .', {}, function (options) { - addTask(options.description, function (err, taskKey) { - if (err) { - throw err; - } - - var taskId = taskKey.path.pop(); - console.log('Task %d created successfully.', taskId); - }); + addTask(options.description, makeHandler()); }) .command('done ', 'Marks the specified task as done.', {}, function (options) { - markDone(options.taskId, function (err) { - if (err) { - throw err; - } - - console.log('Task %d updated successfully.', options.taskId); - }); + markDone(options.taskId, makeHandler()); }) .command('list', 'Lists all tasks ordered by creation time.', {}, function (options) { - listTasks(function (err, tasks) { - if (err) { - throw err; - } - - console.log(formatTasks(tasks)); - }); + listTasks(makeHandler()); }) .command('delete ', 'Deletes a task.', {}, function (options) { - deleteTask(options.taskId, function (err) { - if (err) { - throw err; - } - - console.log('Task %d deleted successfully.', options.taskId); - }); + deleteTask(options.taskId, makeHandler()); }) .example('node $0 new "Buy milk"', 'Adds a task with description "Buy milk".') .example('node $0 done 12345', 'Marks task 12345 as Done.') From 07d2bdb9278285038139a69b137557a460a498ef Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 13 Sep 2016 09:37:45 -0700 Subject: [PATCH 5/5] Address comments --- datastore/tasks.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/datastore/tasks.js b/datastore/tasks.js index 7f0667de9a..7d8230778f 100755 --- a/datastore/tasks.js +++ b/datastore/tasks.js @@ -163,7 +163,8 @@ function deleteTask (taskId, callback) { return callback(err); } - return callback(); + console.log('Task %d deleted successfully.', taskId); + return callback(null); }); } // [END delete_entity]