From d38eb32e7694406bac9ae90e9318c89aca0ae41e Mon Sep 17 00:00:00 2001 From: Patrick Stadler Date: Thu, 6 Mar 2014 08:59:23 +0100 Subject: [PATCH] implement Transport#with(). close #4 --- README.md | 18 ++++++++++++++++++ lib/transport/shell.js | 2 +- lib/transport/ssh.js | 2 +- lib/transport/transport.js | 37 +++++++++++++++++++++++++++++++++++++ package.json | 2 +- 5 files changed, 58 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b604896..5e53882 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/transport/shell.js b/lib/transport/shell.js index 2e8e8b3..7980d50 100644 --- a/lib/transport/shell.js +++ b/lib/transport/shell.js @@ -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); diff --git a/lib/transport/ssh.js b/lib/transport/ssh.js index 7dde1aa..3ade018 100644 --- a/lib/transport/ssh.js +++ b/lib/transport/ssh.js @@ -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 || {}; diff --git a/lib/transport/transport.js b/lib/transport/transport.js index e5c2d3f..da89e4a 100644 --- a/lib/transport/transport.js +++ b/lib/transport/transport.js @@ -52,6 +52,7 @@ function Transport(flight) { failsafe: false }; + this._execWith = ''; commands.forEach(function(cmd) { this[cmd] = function(args, opts) { @@ -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. diff --git a/package.json b/package.json index 8deb6a9..a4d8030 100644 --- a/package.json +++ b/package.json @@ -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 ", "keywords": [ "deploy",