Skip to content

Commit

Permalink
implement transport.prompt()
Browse files Browse the repository at this point in the history
  • Loading branch information
pstadler committed Feb 18, 2014
1 parent cfc482b commit eaa40ad
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down
47 changes: 45 additions & 2 deletions lib/transport/transport.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var util = require('util')
, Fiber = require('fibers')
, prompt = require('prompt')
, commands = require('./commands');

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit eaa40ad

Please sign in to comment.