Skip to content

Commit

Permalink
this will also begin to set things right
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Jul 30, 2024
1 parent 46ee799 commit eae734b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/exec/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.lando
.test
test
1 change: 1 addition & 0 deletions examples/exec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ lando exec web4 -- "nope || echo hellothere" | grep hellothere
lando exec web4 -- "echo -n hello; echo there;" | grep hellothere
lando exec web4 -- "echo -n hello; echo there;" | grep hellothere
lando exec web4 -- "echo \"\$MESSAGE\"" | grep hellothere
lando exec web4 -- echo "\$MESSAGE" | grep hellothere
lando exec web4 -- "mkdir -p /usr/share/nginx/html/test && echo hellothere > /usr/share/nginx/html/test/msg1 && cat /usr/share/nginx/html/test/msg1" | grep hellothere
lando exec web4 -- "mkdir -p /usr/share/nginx/html/test && echo -n hello >> /usr/share/nginx/html/test/msg2 && echo there >> /usr/share/nginx/html/test/msg2 && cat /usr/share/nginx/html/test/msg2" | grep hellothere
lando exec web4 -- "cat < /usr/share/nginx/html/test/msg2" | grep hellothere
Expand Down
1 change: 1 addition & 0 deletions examples/exec/test/msg1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hellothere
1 change: 1 addition & 0 deletions examples/exec/test/msg2
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
hellothere
hellothere
hellothere
37 changes: 24 additions & 13 deletions scripts/exec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
# load in any dynamically set envvars
source /etc/lando/environment

# we need a holder for our expanded args
args=()
# if this is a wrapper script then just execute it
if [[ "$1" == *sh || "$1" == *bash ]]; then
debug "exec '$@'"
exec "$@"

# iterate and expand any variables
# @NOTE: does this have other unintended non-variable consequences?
for arg in "$@"; do
earg=$(eval echo "$arg")
args+=("$earg")
done
# otherwise try to process
else
args=()

# replace "$@" with args
set -- "${args[@]}"
# eval any args with a $
for arg in "$@"; do
if [[ "$arg" == *\$* ]]; then
earg=$(eval echo "$arg")
debug "processed '$arg' into '$earg'"
args+=("$earg")
else
args+=("$arg")
fi
done

# DO IT!
debug "$@ with $# args"
exec "$@"
# replace "$@" with args
set -- "${args[@]}"

# DO IT!
debug "exec '$@'"
exec "$@"
fi
3 changes: 2 additions & 1 deletion tasks/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = (lando, config = lando.appConfig) => ({
// this is useful to handle wrapping more complex commands a la "cmd && cmd"
if (Array.isArray(options.command) && options.command.length === 1) {
if (require('string-argv')(options.command[0]).length > 1) {
options.command = ['sh', '-c', options.command];
options.command = ['sh', '-c', options.command[0]];
}
}

Expand Down Expand Up @@ -135,6 +135,7 @@ module.exports = (lando, config = lando.appConfig) => ({

// try to run it
try {
lando.log.debug('running exec command %o on %o', runner.cmd, runner.id);
await require('../utils/build-docker-exec')(lando, ['inherit', 'pipe', 'pipe'], runner);

// error
Expand Down
3 changes: 3 additions & 0 deletions utils/build-docker-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const getExecOpts = (docker, datum) => {
if (datum.cmd[0] === '/etc/lando/exec.sh' && datum.cmd[datum.cmd.length - 1] === '&') {
datum.cmd.pop();
exec.push('--detach');
} else if (datum.cmd[0] === '/etc/lando/exec.sh' && datum.cmd[datum.cmd.length - 1].endsWith('&')) {
datum.cmd[datum.cmd.length - 1] = datum.cmd[datum.cmd.length - 1].slice(0, -1).trim();
exec.push('--detach');
// Assess the intention to detach for shell wrappers
} else if (datum.cmd[0].endsWith('sh') && datum.cmd[1] === '-c' && datum.cmd[2].endsWith('&')) {
datum.cmd[2] = datum.cmd[2].slice(0, -1).trim();
Expand Down

0 comments on commit eae734b

Please sign in to comment.