-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 4 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -60,7 +57,7 @@ export GCLOUD_PROJECT=<project-id> | |
|
||
7. Run the application! | ||
```sh | ||
npm run tasks | ||
node tasks <command> | ||
``` | ||
*/ | ||
|
||
|
@@ -87,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] | ||
|
@@ -129,7 +127,8 @@ function markDone (taskId, callback) { | |
} | ||
|
||
// The transaction completed successfully. | ||
callback(); | ||
console.log('Task %d updated successfully.', taskId); | ||
return callback(null); | ||
}); | ||
}); | ||
}); | ||
|
@@ -141,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] | ||
|
||
|
@@ -152,101 +158,52 @@ function deleteTask (taskId, callback) { | |
taskId | ||
]); | ||
|
||
datastore.delete(taskKey, callback); | ||
datastore.delete(taskKey, function (err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
return callback(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the following above this line: |
||
}); | ||
} | ||
// [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); | ||
} | ||
|
||
return taskKey + ' : ' + task.data.description + ' (' + status + ')'; | ||
}) | ||
.join('\n'); | ||
} | ||
// [END format_results] | ||
var cli = require('yargs'); | ||
var makeHandler = require('../utils').makeHandler; | ||
|
||
var program = module.exports = { | ||
addEntity: addTask, | ||
updateEntity: markDone, | ||
retrieveEntities: listTasks, | ||
deleteEntity: deleteTask, | ||
main: function (args) { | ||
// Run the command-line program | ||
cli.help().strict().parse(args).argv; | ||
} | ||
}; | ||
|
||
cli | ||
.demand(1) | ||
.command('new <description>', 'Adds a task with a description <description>.', {}, function (options) { | ||
addTask(options.description, makeHandler()); | ||
}) | ||
.command('done <taskId>', 'Marks the specified task as done.', {}, function (options) { | ||
markDone(options.taskId, makeHandler()); | ||
}) | ||
.command('list', 'Lists all tasks ordered by creation time.', {}, function (options) { | ||
listTasks(makeHandler()); | ||
}) | ||
.command('delete <taskId>', 'Deletes a task.', {}, function (options) { | ||
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.') | ||
.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'); | ||
|
||
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); | ||
}); | ||
|
||
break; | ||
} | ||
case 'list': { | ||
listTasks(function (err, tasks) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
console.log(formatTasks(tasks)); | ||
}); | ||
|
||
break; | ||
} | ||
case 'delete': { | ||
taskId = parseInt(input, 10); | ||
|
||
deleteTask(taskId, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
console.log('Task %d deleted successfully.', taskId); | ||
}); | ||
|
||
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')); | ||
} | ||
} | ||
} | ||
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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the docs going to continue to work with this region tag deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should - this tag didn't come up anywhere when I did a code search.