Skip to content

Commit

Permalink
[api] [bash] Back to using Bash v3 by default
Browse files Browse the repository at this point in the history
  * Most systems use Bash v3 by default
  * Adds configuration option for Bash v4
  * Bash v4 is required for associative arrays
  * Should fix tests on Travis
  • Loading branch information
Marak committed Nov 27, 2017
1 parent 05829b6 commit f4f1079
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
6 changes: 5 additions & 1 deletion bin/binaries/micro-bash
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/bin/sh

# Note: Using Bash version 4 or higher will give access to bash associative arrays
# Bash 3 version is used by default. Updating the configuration file is required for bash 4 functionality
#

# basic support for bash scripts
# TODO: replace with better argument parsing function

# TODO: replace with better argument parsing function
_CODE=$2
_INJECT=$6

Expand Down
3 changes: 3 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var fs = require('fs');

module.exports = {
bash: {
version: 3
},
http: {
port: 3000,
host: "0.0.0.0",
Expand Down
7 changes: 6 additions & 1 deletion examples/services/echo/echo.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# bash 4 is required for Hook_params nested object
for k in "${!Hook_params[@]}"
do
echo "$k=${Hook_params[$k]}"
done
done

# with bash 3 properties are accessed using the syntax:
echo $Hook_params_foo;
# where "foo" is the name of the http request parameter
23 changes: 20 additions & 3 deletions lib/plugins/spawn/generateCommandLineArguments/bash/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var config = require('../../../../../config'); // used to track bash version

function bashEscape (arg) {
if (typeof arg === "undefined") {
return "";
Expand All @@ -24,8 +26,21 @@ module['exports'] = function generateBashArguments (service, env) {
// and generate a bunch of unique keys
var args = [];
var bashInject = "";
bashInject += 'Hook="The Hook object isnt a bash object.";\n'
bashInject += 'declare -A Hook_params;\n';
bashInject += 'Hook="The Hook object isnt a bash object.";\n';

// will default to bash version 4 API by default
var bash3 = false;
// setting bash to version 3 requires configuration parameter
if (config.bash && typeof config.bash.version === 'number') {
if (config.bash.version === 3) {
bash3 = true;
}
}

if (!bash3) {
bashInject += 'declare -A Hook_params;\n';
}

for (var p in env) {
if (typeof env[p] === "object") {
for (var s in env[p]) {
Expand All @@ -35,7 +50,9 @@ module['exports'] = function generateBashArguments (service, env) {
bashInject += 'Hook_' + p + '_' + s + '="' + bashEscape(env[p][s]) + '";\n'
}
if (p === 'params') {
bashInject += 'Hook_params[' + s +']="' + bashEscape(env[p][s]) + '";\n';
if (!bash3) {
bashInject += 'Hook_params[' + s +']="' + bashEscape(env[p][s]) + '";\n';
}
}
}
} else {
Expand Down

0 comments on commit f4f1079

Please sign in to comment.