Skip to content

Commit

Permalink
run/push: support "subargs" for forwarding to remote process. Fixes g…
Browse files Browse the repository at this point in the history
…h-732

Uses the "subarg" syntax:

t2 run index.js [foo bar baz -a 1 -b 2 --named --key=value]

Will result in process.argv on the deployed program:

[ '/usr/bin/node',
  '/tmp/remote-script/index.js',
  'foo',
  'bar',
  'baz',
  '-a',
  '1',
  '-b',
  '2',
  '--named',
  '--key=value' ]

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Jul 20, 2016
1 parent 9fedb90 commit 2c6f1d4
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 16 deletions.
37 changes: 37 additions & 0 deletions bin/tessel-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ makeCommand('run')
// Overridden in tarBundle if options.full is `true`
options.slim = true;

if (parser.subargs) {
options.subargs = parser.subargs;
}

callControllerWith('deploy', options);
})
.option('entryPoint', {
Expand Down Expand Up @@ -241,6 +245,7 @@ makeCommand('push')
options.push = true;
// Overridden in tarBundle if options.full is `true`
options.slim = true;
options.subargs = parser.subargs || [];

callControllerWith('deploy', options);
})
Expand Down Expand Up @@ -516,6 +521,38 @@ makeCommand('root')


module.exports = function(args) {
var sIndexOfSA = -1;
var eIndexOfSA = -1;

// Check to see if there are any subargs...
for (var i = 0; i < args.length; i++) {
var arg = args[i];

if (arg.startsWith('[') && sIndexOfSA === -1) {
// Remove the leading '[', replace existing
args[i] = arg.slice(1, arg.length);
sIndexOfSA = i;
}

if (arg.endsWith(']') && sIndexOfSA !== -1) {
// Remove the trailing ']'
args[i] = arg.slice(0, arg.length - 1);
eIndexOfSA = i;
}
}

// If there are, remove them from the `args`
// that get passed to parser.parse().
//
// If these are not removed, they will be
// treated like they are part of the t2-cli args
// themselves, which is undesirable.
if (sIndexOfSA !== -1 && eIndexOfSA !== -1) {
// Splice the subargs from the args that will be passed to nomnom,
// store on parser so we can get to them later.
parser.subargs = args.splice(sIndexOfSA, eIndexOfSA);
}

// Clear the spec from one call to the next. This is
// only necessary for testing the CLI (each call must be "fresh")
parser.specs = {};
Expand Down
6 changes: 3 additions & 3 deletions lib/tessel/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ module.exports.app = {
*
*/
module.exports.js = {
execute: (filepath, relpath) => ['node', filepath + relpath],
execute: (filepath, relpath, args) => ['node', filepath + relpath].concat(args),
};

module.exports.rs = {
execute: (filepath, relpath) => [filepath + relpath],
execute: (filepath, relpath, args) => [filepath + relpath].concat(args),
};

module.exports.py = {
execute: (filepath, relpath) => ['python', filepath + relpath],
execute: (filepath, relpath, args) => ['python', filepath + relpath].concat(args),
};

module.exports.readFile = function(filepath) {
Expand Down
4 changes: 3 additions & 1 deletion lib/tessel/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Tessel.prototype.deploy = function(opts) {

// Resolve the application's language/runtime
opts.lang = deployment.resolveLanguage(opts.lang || opts.entryPoint);
opts.subargs = opts.subargs || [];

return new Promise((resolve, reject) => {
// Stop running an existing applications
Expand Down Expand Up @@ -373,7 +374,7 @@ exportables.run = function(tessel, options) {

return preRun.then(() => {
tessel.connection.exec(
commands[lang.meta.extname].execute(Tessel.REMOTE_RUN_PATH, options.resolvedEntryPoint), {
commands[lang.meta.extname].execute(Tessel.REMOTE_RUN_PATH, options.resolvedEntryPoint, options.subargs), {
pty: true
}, (error, remoteProcess) => {
if (error) {
Expand Down Expand Up @@ -438,6 +439,7 @@ exportables.createShellScript = function(tessel, opts) {
});
});
});

remoteProcess.stdin.end(new Buffer(opts.lang.meta.shell(opts).trim()));
});
});
Expand Down
4 changes: 2 additions & 2 deletions lib/tessel/deployment/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ var exportables = {
program
};
},
shell: (opts) => {
shell: (options) => {
return tags.stripIndent `
#!/bin/sh
exec node /app/remote-script/${opts.resolvedEntryPoint}
exec node /app/remote-script/${options.resolvedEntryPoint} ${options.subargs.join(' ')}
`;
},
},
Expand Down
4 changes: 2 additions & 2 deletions lib/tessel/deployment/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ var exportables = {
program
};
},
shell: (opts) => {
shell: (options) => {
return tags.stripIndent `
#!/bin/sh
exec python /app/remote-script/${opts.resolvedEntryPoint}
exec python /app/remote-script/${options.resolvedEntryPoint} ${options.subargs.join(' ')}
`;
},
},
Expand Down
4 changes: 2 additions & 2 deletions lib/tessel/deployment/rust.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ var exportables = {
program
};
},
shell: (opts) => {
shell: (options) => {
return tags.stripIndent `
#!/bin/sh
exec /app/remote-script/${opts.resolvedEntryPoint}
exec /app/remote-script/${options.resolvedEntryPoint} ${options.subargs.join(' ')}
`;
},
},
Expand Down
12 changes: 9 additions & 3 deletions test/unit/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ exports['Tessel.prototype.deploy'] = {
test.expect(12);
deployTestCode(this.tessel, {
push: true,
single: false
single: false,
subargs: [],
}, (error) => {
if (error) {
test.ok(false, `deployTestCode failed: ${error.toString()}`);
Expand Down Expand Up @@ -538,8 +539,10 @@ exports['deploy.run'] = {
deploy.run(this.tessel, {
entryPoint: entryPoint,
lang: deployment.js,
subargs: ['--key=value'],
}).then(() => {
test.deepEqual(this.exec.lastCall.args[0], [deployment.js.meta.binary, Tessel.REMOTE_RUN_PATH + entryPoint]);
test.deepEqual(
this.exec.lastCall.args[0], [deployment.js.meta.binary, Tessel.REMOTE_RUN_PATH + entryPoint, '--key=value']);
test.done();
});
},
Expand Down Expand Up @@ -572,6 +575,7 @@ exports['deploy.run'] = {
deploy.run(this.tessel, {
entryPoint: entryPoint,
lang: deployment.rs,
subargs: [],
}).then(() => {
test.done();
});
Expand All @@ -591,6 +595,7 @@ exports['deploy.run'] = {
deploy.run(this.tessel, {
resolvedEntryPoint: 'foo',
lang: deployment.js,
subargs: [],
}).then(() => {
test.equal(this.postRun.callCount, 1);
test.done();
Expand Down Expand Up @@ -640,7 +645,8 @@ exports['deploy.createShellScript'] = {

var opts = {
lang: deployment.js,
resolvedEntryPoint: 'foo'
resolvedEntryPoint: 'foo',
subargs: ['--key=value'],
};

deploy.createShellScript(this.tessel, opts).then(() => {
Expand Down
30 changes: 27 additions & 3 deletions test/unit/deployment/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ exports['Deployment: JavaScript'] = {

var shellScript = tags.stripIndent `
#!/bin/sh
exec node /app/remote-script/index.js
exec node /app/remote-script/index.js --key=value
`;
var opts = {
lang: deployment.js,
resolvedEntryPoint: 'index.js',
subargs: ['--key=value'],
};
this.end.restore();
this.end = sandbox.stub(this.tessel._rps.stdin, 'end', (buffer) => {
Expand All @@ -120,11 +121,12 @@ exports['Deployment: JavaScript'] = {

var shellScript = tags.stripIndent `
#!/bin/sh
exec node /app/remote-script/index.js
exec node /app/remote-script/index.js --key=value
`;
var opts = {
lang: deployment.js,
resolvedEntryPoint: 'index.js',
subargs: ['--key=value'],
};
this.end.restore();
this.end = sandbox.stub(this.tessel._rps.stdin, 'end', (buffer) => {
Expand Down Expand Up @@ -2552,7 +2554,29 @@ exports['deploy.createShellScript'] = {

var opts = {
lang: deployment.js,
resolvedEntryPoint: 'foo'
resolvedEntryPoint: 'foo',
subargs: [],
};

deploy.createShellScript(this.tessel, opts).then(() => {
test.deepEqual(this.exec.firstCall.args[0], ['dd', 'of=/app/start']);
test.deepEqual(this.exec.lastCall.args[0], ['chmod', '+x', '/app/start']);
test.done();
});
},

remoteShellScriptPathIsNotPathNormalizedWithSubargs: function(test) {
test.expect(2);

this.exec = sandbox.stub(this.tessel.connection, 'exec', (command, callback) => {
callback(null, this.tessel._rps);
this.tessel._rps.emit('close');
});

var opts = {
lang: deployment.js,
resolvedEntryPoint: 'foo',
subargs: ['--key=value'],
};

deploy.createShellScript(this.tessel, opts).then(() => {
Expand Down

0 comments on commit 2c6f1d4

Please sign in to comment.