diff --git a/README.md b/README.md index 1290122..cf47975 100644 --- a/README.md +++ b/README.md @@ -378,6 +378,18 @@ is formatted correctly within the current context. transport.log('Copying files to remote hosts'); ``` +Prompt for user input. + +```javascript +var input = local.prompt('Are you sure you want to continue? [yes]'); +if(input.indexOf('yes') === -1) { + local.abort('user canceled flight'); +} + +// prompt for password (hide input UNIX style) +var password = local.prompt('Enter your password:', { hidden: true }); +``` + ### transport.silent() When calling `silent()` all subsequent commands are executed without @@ -432,8 +444,9 @@ remote.ls('foo'); // ls: foo: No such file or directory ### transport.debug(message) -Print a debug message to stdout. Flightplan takes care that the message -is formatted correctly within the current context. +Print a debug message to stdout if debug mode is enabled. Flightplan +takes care that the message is formatted correctly within the current +context. ```javascript remote.debug('Copying files to remote hosts'); diff --git a/lib/transport/transport.js b/lib/transport/transport.js index 7a3e822..8ef63d8 100644 --- a/lib/transport/transport.js +++ b/lib/transport/transport.js @@ -1,4 +1,6 @@ var util = require('util') + , Fiber = require('fibers') + , prompt = require('prompt') , commands = require('./commands'); /** @@ -176,6 +178,46 @@ Transport.prototype = { this.logger.log.apply(this.logger, arguments); }, + /** + * Prompt for user input. + * + * ```javascript + * var input = local.prompt('Are you sure you want to continue? [yes]'); + * if(input.indexOf('yes') === -1) { + * local.abort('user canceled flight'); + * } + * + * // prompt for password (hide input UNIX style) + * var password = local.prompt('Enter your password:', { hidden: true }); + * ``` + * + * @method(query[, options]) + * @return input + */ + prompt: function(query, options) { + options = options || {}; + var fiber = Fiber.current; + + prompt.delimiter = ''; + prompt.message = 'prompt'.grey; + prompt.start(); + + prompt.get([{ + name: 'input', + description: ' ' + query.white, + hidden: options.hidden || false, + required: options.required || false + }], function(err, result) { + if(err) { + this.logger.space(); + fiber.throwInto(new Error('user canceled prompt')); + } + fiber.run(result ? result.input : null); + }.bind(this)); + + return Fiber.yield(); + }, + /** * When calling `silent()` all subsequent commands are executed without * printing their output to stdout until `verbose()` is called. @@ -249,8 +291,9 @@ Transport.prototype = { }, /** - * Print a debug message to stdout. Flightplan takes care that the message - * is formatted correctly within the current context. + * Print a debug message to stdout if debug mode is enabled. Flightplan + * takes care that the message is formatted correctly within the current + * context. * * ```javascript * remote.debug('Copying files to remote hosts'); diff --git a/package.json b/package.json index 4a0e587..b67dc0f 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "fibers": "~1.0.1", "ssh2": "~0.2.17", "colors": "~0.6.2", - "commander": "~2.1.0" + "commander": "~2.1.0", + "prompt": "~0.2.12" }, "devDependencies": { "gulp": "~3.5.2",