Skip to content

Commit

Permalink
Merge d8f6d9e into b8d7140
Browse files Browse the repository at this point in the history
  • Loading branch information
nkakouros committed Jan 13, 2018
2 parents b8d7140 + d8f6d9e commit 1f479c2
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 20 deletions.
19 changes: 13 additions & 6 deletions go-core.bash
Expand Up @@ -65,7 +65,8 @@ fi
declare -r -x _GO_CORE_DIR="$PWD"

# Set _GO_STANDALONE if your script is a standalone program.
#
declare -x _GO_STANDALONE="${_GO_STANDALONE-}"

# See the "Standalone mode" section of README.md for more information.
if [[ -z "$_GO_STANDALONE" ]]; then
cd "$_GO_ROOTDIR" || exit 1
Expand Down Expand Up @@ -120,7 +121,7 @@ declare -r -x _GO_TEST_DIR="${_GO_TEST_DIR:-tests}"
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}"
declare -r -x _GO_CMD="${_GO_CMD:-$0}"

# The array of command line arguments comprising the ./go command name after
# _GO_CMD.
Expand Down Expand Up @@ -151,11 +152,14 @@ declare _GO_SEARCH_PATHS=()
# Should be an absolute path. Use this for stubbing out scripts during testing
# or debugging, or for experimenting with new implementations of existing
# scripts.
declare _GO_INJECT_SEARCH_PATH="$_GO_INJECT_SEARCH_PATH"
declare _GO_INJECT_SEARCH_PATH="${_GO_INJECT_SEARCH_PATH-}"

# Directory to search for module scripts first.
# Similar to _GO_INJECT_SEARCH_PATHS above, but for `. "$_GO_USE_MODULES"`.
declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
declare _GO_INJECT_MODULE_PATH="${_GO_INJECT_MODULE_PATH-}"

# Read from the environment and used in output formatting
COLUMNS="${COLUMNS-}"

# Invokes printf builtin, then folds output to $COLUMNS width
#
Expand Down Expand Up @@ -283,8 +287,11 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
# $1: name of the command to invoke
# $2..$#: arguments to the specified command
@go() {
local cmd="$1"
shift
local cmd="${1-}"

if [[ $# > 0 ]]; then
shift
fi

case "$cmd" in
'')
Expand Down
3 changes: 2 additions & 1 deletion lib/complete
Expand Up @@ -33,14 +33,15 @@
fi
unset "compreply[$compgen_exit_index]"

add_slashes='false'
if [[ "$1" =~ ^-[df]$ ]]; then
add_slashes='true'
fi

for reply_item in "${compreply[@]}"; do
# Since we're not using -o filenames, we must add slashes to dir names.
# Since we're using -o nospace, we must add a space to a single match.
if [[ -n "$add_slashes" && -d "$reply_item" ]]; then
if [[ "$add_slashes" == 'true' && -d "$reply_item" ]]; then
printf -- '%s/\n' "$reply_item"
elif [[ "${#compreply[@]}" -eq '1' && ! $reply_item =~ [\ /]$ ]]; then
printf -- '%s \n' "$reply_item"
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/path
Expand Up @@ -25,7 +25,7 @@ _@go.set_command_path_and_argv() {

local cmd_args=("$@")
local cmd_name="${cmd_args[0]}"
local cmd_path
local cmd_path=''
local try_path

unset 'cmd_args[0]'
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/set-search-paths
Expand Up @@ -26,7 +26,7 @@ _@go.set_search_paths() {
# A plugin's own local plugin paths will appear before inherited ones. If
# there is a version incompatibility issue with other installed plugins, this
# allows a plugin's preferred version to take precedence.
@go.search_plugins '_@go.set_search_paths_add_plugin_paths'
@go.search_plugins '_@go.set_search_paths_add_plugin_paths' || :
_GO_SEARCH_PATHS+=("${_GO_PLUGINS_PATHS[@]}")
}

Expand Down
4 changes: 2 additions & 2 deletions libexec/commands
Expand Up @@ -70,7 +70,7 @@ _@go.commands_parse_search_paths() {
}

_@go.commands_parse_argv() {
case "$1" in
case "${1-}" in
--paths|--summaries)
__go_commands_action="${1#--}"
shift
Expand Down Expand Up @@ -132,7 +132,7 @@ _@go.commands_tab_completions() {

_@go.commands() {
# Tab completions
if [[ "$1" == '--complete' ]]; then
if [[ "${1-}" == '--complete' ]]; then
shift
_@go.commands_tab_completions "$@"
return
Expand Down
19 changes: 15 additions & 4 deletions libexec/env
Expand Up @@ -21,15 +21,15 @@ _@go.has_spaces() {

_@go.env() {
# Tab completions
if [[ "$1" == '--complete' ]]; then
if [[ "${1-}" == '--complete' ]]; then
local word_index="$2"
if [[ "$word_index" -eq '0' ]]; then
echo '-'
fi
return
fi

local go_func="$1"
local go_func="${1-}"
local default_name="${_GO_SCRIPT##*/}"
local shell="${SHELL##*/}"
local shell_impl="$_GO_CORE_DIR/lib/internal/env/$shell"
Expand Down Expand Up @@ -70,12 +70,19 @@ _@go.env() {
return 1
fi

read -r -d '' help_msg <<END_OF_HELP
read -r -d '' help_msg <<END_OF_HELP && exit_code=0 || exit_code=$?
# Define the "$default_name" function by running the following command.
# Replace '-' to use a different name instead of "$default_name".
${eval_cmd//%s/$0}
END_OF_HELP

if [[ "$exit_code" -gt 1 ]]; then
# Due to encountering EOL, 'read' will return with '1'. To support 'set -e'
# we only return the exit code if an actual error has occured ($? -gt 1).
return $exit_code
fi

@go.printf "$help_msg\n"
return
elif [[ "$go_func" == '-' ]]; then
Expand All @@ -87,7 +94,11 @@ END_OF_HELP
return 1
fi

read -r -d '' env_script < "$shell_impl"
read -r -d '' env_script < "$shell_impl" && exit_code=0 || exit_code=$?
if [[ "$exit_code" -gt 1 ]]; then
return $exit_code
fi

env_script="${env_script//_go_func/$go_func}"
env_script="${env_script//\$_GO_ROOTDIR/$_GO_ROOTDIR}"
echo "${env_script//\$_GO_SCRIPT/$_GO_SCRIPT}"
Expand Down
8 changes: 3 additions & 5 deletions libexec/help
Expand Up @@ -129,16 +129,14 @@ _@go.help_message_for_command() {
}

_@go.help() {
if [[ "$1" == '--complete' ]]; then
if [[ "$#" -eq '0' ]]; then
_@go.usage
elif [[ "$1" == '--complete' ]]; then
# Tab completions
shift
. "$_GO_CORE_DIR/lib/internal/complete"
_@go.complete_command_path "$@"
return
fi

if [[ "$#" -eq '0' ]]; then
_@go.usage
else
_@go.help_message_for_command "$@"
fi
Expand Down
3 changes: 3 additions & 0 deletions tests/vars.bats
Expand Up @@ -56,6 +56,7 @@ quotify_expected() {
"declare -rx _GO_SCRIPT=\"$TEST_GO_SCRIPT\""
"declare -- _GO_SCRIPTS_DIR=\"$TEST_GO_SCRIPTS_DIR\""
"declare -a _GO_SEARCH_PATHS=(${search_paths[*]})"
"declare -x _GO_STANDALONE=\"\""
"declare -rx _GO_TEST_DIR=\"$_GO_TEST_DIR\""
"declare -rx _GO_USE_MODULES=\"$_GO_CORE_DIR/lib/internal/use\"")

Expand Down Expand Up @@ -130,6 +131,7 @@ quotify_expected() {
"declare -rx _GO_SCRIPT=\"$TEST_GO_SCRIPT\""
"declare -- _GO_SCRIPTS_DIR=\"$TEST_GO_SCRIPTS_DIR\""
"declare -a _GO_SEARCH_PATHS=(${search_paths[*]})"
"declare -x _GO_STANDALONE=\"$_GO_STANDALONE\""
"declare -rx _GO_TEST_DIR=\"$_GO_TEST_DIR\""
"declare -rx _GO_USE_MODULES=\"$_GO_CORE_DIR/lib/internal/use\"")

Expand Down Expand Up @@ -171,6 +173,7 @@ quotify_expected() {
"_GO_KCOV_DIR: $_GO_KCOV_DIR" \
"_GO_ROOTDIR: $TEST_GO_ROOTDIR" \
"_GO_SCRIPT: $TEST_GO_SCRIPT" \
"_GO_STANDALONE: $_GO_STANDALONE" \
"_GO_TEST_DIR: $_GO_TEST_DIR" \
"_GO_USE_MODULES: $_GO_USE_MODULES"
}

0 comments on commit 1f479c2

Please sign in to comment.