Skip to content

Commit

Permalink
Merge 91dc1c2 into b8d7140
Browse files Browse the repository at this point in the history
  • Loading branch information
nkakouros authored May 9, 2018
2 parents b8d7140 + 91dc1c2 commit fde9d45
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 45 deletions.
27 changes: 19 additions & 8 deletions go-core.bash
Original file line number Diff line number Diff line change
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 @@ -204,7 +208,7 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
# Arguments:
# skip_callers: The number of callers to skip over when printing the stack
@go.print_stack_trace() {
local skip_callers="$1"
local skip_callers="${1-}"
local result=0
local i

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 Expand Up @@ -360,7 +367,11 @@ _@go.run_command_script() {
shift

local interpreter
read -r interpreter < "$cmd_path"
interpreter=''

if [[ -s "$cmd_path" ]]; then
read -r interpreter < "$cmd_path"
fi

if [[ "${interpreter:0:2}" != '#!' ]]; then
@go.printf \
Expand Down
8 changes: 4 additions & 4 deletions lib/bats-main
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,22 @@ fi
# $1: One of the flags defined above, or the first test glob pattern
# ...: Remaining test glob patterns
@go.bats_main() {
if [[ "$1" == '--complete' ]]; then
if [[ "${1:-}" == '--complete' ]]; then
shift
@go.bats_tab_completion "$@"
return
fi
@go.bats_clone

if [[ "$1" == '--coverage' && -z "$__GO_COVERAGE_RUN" ]]; then
if [[ "${1:-}" == '--coverage' && -z "$__GO_COVERAGE_RUN" ]]; then
shift
export __GO_COVERAGE_RUN='true'
@go.bats_coverage "$@"
elif [[ "$1" == '--edit' ]]; then
elif [[ "${1:-}" == '--edit' ]]; then
shift
local tests=($(@go 'glob' "${_GO_BATS_GLOB_ARGS[@]}" "$@"))
@go 'edit' "${tests[@]}"
elif [[ "$1" == '--list' ]]; then
elif [[ "${1:-}" == '--list' ]]; then
shift
@go 'glob' '--trim' "${_GO_BATS_GLOB_ARGS[@]}" "$@"
elif [[ -z "$__GO_COVERAGE_RUN" && -n "$_GO_COLLECT_BATS_COVERAGE" ]]; then
Expand Down
11 changes: 6 additions & 5 deletions lib/complete
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

while IFS= read -r reply_item; do
compreply+=("$reply_item")
done < <(trap 'echo "$?"' EXIT; compgen "$@"; exit "$?")
done < <(trap "" ERR; trap 'echo "$?"' EXIT; compgen "$@"; exit "$?")

compgen_exit_index="$((${#compreply[@]} - 1))"

Expand All @@ -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 Expand Up @@ -69,10 +70,10 @@
local completion_item_reference
local i

for arg in "${!argv_array_reference}"; do
for ((i=0; i != num_completions; ++i)); do
for arg in "${!argv_array_reference-}"; do
for ((i=0; i < num_completions; ++i)); do
completion_item_reference="$completions_reference[$i]"
if [[ "${!completion_item_reference}" == "$arg" ]]; then
if [[ "${!completion_item_reference-}" == "$arg" ]]; then
unset "$completions_reference[$i]"
fi
done
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/path
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 lib/platform
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Sets the `_GO_PLATFORM_*` family of environment variables
#
# This module doesn't export any functions, only the `_GO_PLATFORM_*`
# environment variables if `/etc/os-release` if present, or just
# environment variables if `/etc/os-release` is present, or just
# `_GO_PLATFORM_ID` otherwise, which is set to:
#
# - 'macos' if the Bash variable `$OSTYPE` matches 'darwin'
Expand Down Expand Up @@ -64,7 +64,7 @@ _@go.platform_ostype() {
}

_@go.platform() {
if [[ -n "$_GO_PLATFORM_ID" ]]; then
if [[ -n "${_GO_PLATFORM_ID-}" ]]; then
return
elif ! _@go.platform_os_release; then
_@go.platform_ostype
Expand Down
2 changes: 1 addition & 1 deletion lib/prompt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
# Zero on 'y' or 'yes' (case- and space- insensitive), nonzero otherwise
@go.prompt_for_yes_or_no() {
local prompt="$1"
local default="$2"
local default="${2-}"
local response

case "$default" in
Expand Down
2 changes: 1 addition & 1 deletion libexec/aliases
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ _@go.aliases() {
return
elif [[ "$1" == '--complete' ]]; then
# Tab completions
local word_index="$2"
local word_index="${2-0}"
if [[ "$word_index" -eq 0 ]]; then
echo "--exists"
fi
Expand Down
16 changes: 9 additions & 7 deletions libexec/commands
Original file line number Diff line number Diff line change
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 @@ -105,20 +105,22 @@ _@go.commands_parse_argv() {
}

_@go.commands_tab_completions() {
local word_index="$1"
shift
local word_index="${1-0}"
if [[ "$#" > 1 ]]; then
shift
fi

if [[ "$word_index" -eq '0' ]]; then
echo '--paths' '--summaries'
if [[ "${1:0:1}" == '-' || "$#" -gt 1 ]]; then
if [[ "$#" -gt 0 && ("${1:0:1}" == '-' || "$#" -gt 1) ]]; then
return
fi
fi

if [[ "${1:0:1}" == '-' ]]; then
if [[ "$#" -gt 0 && "${1:0:1}" == '-' ]]; then
shift
((--word_index))
elif [[ "$1" =~ : || -d "$1" ]]; then
elif [[ "${1-}" =~ : || -d "${1-}" ]]; then
return 1
fi

Expand All @@ -132,7 +134,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
11 changes: 7 additions & 4 deletions libexec/complete
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ _@go.complete_command() {
}

_@go.complete_args() {
local word_index="$1"
shift
local word_index="${1-0}"
if [[ "$#" -gt 0 ]]; then
shift
fi
local args=("$@")
local cmd_name="${args[0]}"
local word="${args[$word_index]}"
local cmd_name="${args[0]-}"
local word="${args[$word_index]-}"
local ec=0

. "$_GO_CORE_DIR/lib/internal/complete"

Expand Down
19 changes: 15 additions & 4 deletions libexec/env
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion libexec/get.d/file
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ _@go.get_file() {
local filename
local dl_cmd=()

case "$1" in
case "${1-}" in
--complete)
# Tab completions
_@go.get_file_tab_completions "${@:2}"
Expand Down
8 changes: 3 additions & 5 deletions libexec/help
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion libexec/modules
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ _@go.modules_import_use_modules_functions() {
_@go.modules_import_use_modules_functions

_@go.modules_help() {
local __go_module_name="$1"
local __go_module_name="${1-}"
local __go_module_file

if [[ "$#" -eq '0' ]]; then
Expand Down
3 changes: 3 additions & 0 deletions tests/vars.bats
Original file line number Diff line number Diff line change
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 fde9d45

Please sign in to comment.