Skip to content

Commit

Permalink
feature: pass query as env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Jul 25, 2017
1 parent 6c836ee commit be1a464
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions fixtures/echo-word.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo before ${word} after
4 changes: 4 additions & 0 deletions fixtures/shellhubrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"cwd": "./",
"cmd": "bash hello-world.sh"
},
"/echo/word": {
"cwd": "./",
"cmd": "bash echo-word.sh"
},
"/with/error": {
"cwd": "./",
"cmd": "echo first line >&2 && echo second line && echo third line >&2"
Expand Down
25 changes: 21 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ var config;

function controller(req, res) {
config.reload();
var pathname = url.parse(req.url).pathname;
var entry = config.scripts[pathname];
var urlObj = url.parse(req.url, true)
var entry = config.scripts[urlObj.pathname];

res.setHeader('content-type', 'text/plain')
if (!entry) {
var msg = 'path ' + pathname + ' not registered';
var msg = 'path ' + urlObj.pathname + ' not registered';
log(msg);
res.statusCode = 404;
res.end(msg);
Expand All @@ -27,14 +27,31 @@ function controller(req, res) {
log(msg);
res.write(msg);

var proc = spawn('bash', ['-c', '( ' + entry.cmd + ' ) 2>&1'], {
var loader = exports.loader(entry.cmd, urlObj.query)
var proc = spawn('bash', ['-c', loader], {
cwd: entry.cwd
});

proc.stdout.pipe(res);
proc.stdout.pipe(process.stdout);
}

exports.loader = function(cmd, query){
var r = /^\w+$/
var vars = Object
.keys(query)
.map(function(key){
var val = query[key];
if (r.test(key) && r.test(val)) {
return 'export ' + key + '=' + val + ';'
} else {
return ''
}
})
.join('')
return vars + '( ' + cmd + ' ) 2>&1'
}

exports.mkServer = function(cfg) {
config = cfg;
return http.createServer(controller);
Expand Down
24 changes: 24 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const request = require('supertest');
const server = require('../src/server.js');
const chai = require('chai')
const expect = chai.expect
const Config = require('../src/config.js');
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -34,6 +36,28 @@ describe('server', function() {
.expect(/first line\nsecond line\nthird line/);
});

it('should pass query as variables into bash', function() {
return request(app)
.get('/echo/word?word=WORD')
.expect(200)
.expect(/before WORD after/);
});

it('should generate loader', function() {
var ret = server.loader('echo', {})
expect(ret).to.equal('( echo ) 2>&1')
});

it('should generate loader according to query', function() {
var ret = server.loader('echo', {foo: 'bar'})
expect(ret).to.equal('export foo=bar;( echo ) 2>&1')
});

it('should skip non work characters in query', function() {
var ret = server.loader('echo', {foo: '-bar', bar: 'baz'})
expect(ret).to.equal('export bar=baz;( echo ) 2>&1')
});

it('should capture child outputs', function() {
return request(app)
.get('/spawn')
Expand Down

0 comments on commit be1a464

Please sign in to comment.