scope launch script: Attempt to quote args in "scope command"

As indicated by the TODO, any args passed into the command do not get escaped
when output, so for example:
	scope command "foo bar"
would output results like:
	foo bar
instead of
	"foo bar"
or
	foo\ bar

The "right" way to do this seems to be printf %q, which prints a quoted version of the string.
However this format specifier is not available in POSIX sh (though it does work in many
implementations of it, such as the ones provided by bash which make up the likely majority of
real-world usage).
This code is a compromise that uses the added functionality where available,
while still falling back to the old behaviour when it isn't.
This commit is contained in:
Mike Lang
2016-12-09 05:57:28 -08:00
parent 21431bb4b0
commit b1a1c7d427

14
scope
View File

@@ -172,8 +172,16 @@ check_docker_version
case "$COMMAND" in
command)
# TODO: properly escape/quote the output of "$@"
echo "$(launch_command)" "$@"
# Most systems should have printf, but the %q specifier isn't mandated by posix
# and can't be guaranteed. Since this is mainly a cosmetic output and the alternative
# is not making any attempt to do escaping at all, we might as well try.
quoted=$(printf '%q ' "$@" 2>/dev/null || true)
# printf %q behaves oddly with zero args (it acts as though it recieved one empty arg)
# so we ignore that case.
if [ -z "$quoted" ] || [ $# -eq 0 ]; then
quoted="$*"
fi
echo "$(launch_command) $quoted"
;;
version)
@@ -245,7 +253,7 @@ EOF
fi
if check_docker_for_mac ; then
if docker inspect "$SCOPE_APP_CONTAINER_NAME" >/dev/null 2>&1 ; then
docker stop "$SCOPE_APP_CONTAINER_NAME" >/dev/null
docker stop "$SCOPE_APP_CONTAINER_NAME" >/dev/null
fi
fi
;;