Skip to content

Commit

Permalink
implement Transport#with(). close #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pstadler committed Mar 6, 2014
1 parent ce21c79 commit d38eb32
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,24 @@ var result = transport.waitFor(function(done) {
console.log(result); // 'sent!'
```

### transport.with(cmd|options[, options], fn)

Execute commands with a certain context.

```javascript
transport.with('cd /tmp', function() {
transport.ls('-al'); // 'cd /tmp && ls -al'
});

transport.with({silent: true, failsafe: true}, function() {
transport.ls('-al'); // output suppressed, fail safely
});

transport.with('cd /tmp', {silent: true}, function() {
transport.ls('-al'); // 'cd /tmp && ls -al', output suppressed
});
```

### transport.silent()

When calling `silent()` all subsequent commands are executed without
Expand Down
2 changes: 1 addition & 1 deletion lib/transport/shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ShellTransport.prototype.__exec = function(cmd, args, options) {
stderr: null
};

cmd = cmd + (args ? ' ' + args : '');
cmd = this._execWith + cmd + (args ? ' ' + args : '');
this.logger.command(cmd);
proc = exec(cmd);

Expand Down
2 changes: 1 addition & 1 deletion lib/transport/ssh.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SSHTransport.prototype.__exec = function(cmd, args, options) {
stdout: null,
stderr: null
};
cmd = cmd + (args ? ' ' + args : '');
cmd = this._execWith + cmd + (args ? ' ' + args : '');

this.logger.command(cmd);
var execOpts = options.exec || this.target.exec || {};
Expand Down
37 changes: 37 additions & 0 deletions lib/transport/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function Transport(flight) {
failsafe: false
};

this._execWith = '';

commands.forEach(function(cmd) {
this[cmd] = function(args, opts) {
Expand Down Expand Up @@ -293,6 +294,42 @@ Transport.prototype = {
return Fiber.yield();
},

/**
* Execute commands with a certain context.
*
* ```javascript
* transport.with('cd /tmp', function() {
* transport.ls('-al'); // 'cd /tmp && ls -al'
* });
*
* transport.with({silent: true, failsafe: true}, function() {
* transport.ls('-al'); // output suppressed, fail safely
* });
*
* transport.with('cd /tmp', {silent: true}, function() {
* transport.ls('-al'); // 'cd /tmp && ls -al', output suppressed
* });
* ```
*
* @method with(cmd|options[, options], fn)
*/
with: function() {
var previousOptions = util._extend({}, this.options); // clone
var args = Array.prototype.slice.call(arguments, 0);

for(var i in args) {
if(typeof args[i] === 'string') {
this._execWith = args[i] + ' && ';
} else if(typeof args[i] === 'object') {
this.options = util._extend(this.options, args[i]);
} else if(typeof args[i] === 'function') {
args[i]();
}
}
this._execWith = '';
this.options = previousOptions;
},

/**
* When calling `silent()` all subsequent commands are executed without
* printing their output to stdout until `verbose()` is called.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "flightplan",
"description": "Library for streamlining application deployment or systems administration tasks",
"version": "0.1.9",
"version": "0.1.10",
"author": "Patrick Stadler <[email protected]>",
"keywords": [
"deploy",
Expand Down

0 comments on commit d38eb32

Please sign in to comment.