Skip to content

Commit

Permalink
WIP: core: Export _GO_CMD_NAME and _GO_CMD_ARGV
Browse files Browse the repository at this point in the history
Future commit: add a test to check for variables in Bash command
scripts.
  • Loading branch information
mbland committed Nov 9, 2016
1 parent ecc31e7 commit 2ce6912
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
28 changes: 25 additions & 3 deletions go-core.bash
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ declare -r -x _GO_SCRIPT="$_GO_ROOTDIR/${0##*/}"
# The name of either the ./go script itself or the shell function invoking it.
declare -r -x _GO_CMD="${_GO_CMD:=$0}"

# The array of command line arguments comprising the ./go command name after
# _GO_CMD.
#
# When exported to scripts not written in bash, the array is converted to a
# string with the components delimited by the ASCII NUL character ($'\0').
declare -x _GO_CMD_NAME=

# The array of command line arguments for the ./go command after _GO_CMD_NAME.
#
# When exported to scripts not written in bash, the array is converted to a
# string with the arguments delimited by the ASCII NUL character ($'\0').
declare -x _GO_CMD_ARGV=

# The URL of the framework's original source repository.
declare -r -x _GO_CORE_URL='https://github.com/mbland/go-script-bash'

Expand Down Expand Up @@ -167,6 +180,8 @@ declare -r -x _GO_CORE_URL='https://github.com/mbland/go-script-bash'
if ! _@go.set_command_path_and_argv "$cmd" "$@"; then
return 1
fi

local __go_cmd_name=("$cmd" "${@:1:$(("$#" - "${#__go_argv[@]}"))}")
_@go.run_command_script "$__go_cmd_path" "${__go_argv[@]}"
}

Expand Down Expand Up @@ -197,12 +212,19 @@ _@go.run_command_script() {
interpreter="${interpreter%% *}"

if [[ "$interpreter" == 'bash' || "$interpreter" == 'sh' ]]; then
_GO_CMD_NAME=("${__go_cmd_name[@]}")
_GO_CMD_ARGV=("$@")
. "$cmd_path" "$@"
elif [[ -n "$interpreter" ]]; then
"$interpreter" "$cmd_path" "$@"
else
elif [[ -z "$interpreter" ]]; then
@go.printf "Could not parse interpreter from first line of $cmd_path.\n" >&2
return 1
else
local origIFS="$IFS"
local IFS=$'\0'
_GO_CMD_NAME="${__go_cmd_name[*]}"
_GO_CMD_ARGV="$*"
IFS="$origIFS"
"$interpreter" "$cmd_path" "$@"
fi
}

Expand Down
7 changes: 5 additions & 2 deletions tests/core/run-command-script.bats
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ teardown() {
'}')

local expected=("_GO_CMD: $TEST_GO_SCRIPT"
"_GO_CMD_ARGV: foo"$'\0'"bar"$'\0'"baz quux"$'\0'"xyzzy"
"_GO_CMD_NAME: test-command"$'\0'"test-subcommand"
"_GO_CORE_DIR: $_GO_CORE_DIR"
"_GO_CORE_URL: $_GO_CORE_URL"
"_GO_ROOTDIR: $TEST_GO_ROOTDIR"
"_GO_SCRIPT: $TEST_GO_SCRIPT"
"_GO_SCRIPTS_DIR: $TEST_GO_SCRIPTS_DIR")

create_test_command_script 'test-command' "${script[@]}"
run "$TEST_GO_SCRIPT" test-command
create_test_command_script 'test-command' "# Placeholder parent command"
create_test_command_script 'test-command.d/test-subcommand' "${script[@]}"
run "$TEST_GO_SCRIPT" test-command test-subcommand foo bar 'baz quux' xyzzy
local IFS=$'\n'
assert_success "${expected[*]}"
}
Expand Down

0 comments on commit 2ce6912

Please sign in to comment.