diff --git a/go-core.bash b/go-core.bash index 61cf511..25c76f6 100755 --- a/go-core.bash +++ b/go-core.bash @@ -319,7 +319,13 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH" esac if _@go.source_builtin 'aliases' --exists "$cmd"; then - "$cmd" "$@" + if [[ " ${GO_ALIAS_EXPAND_CMDS[*]} " == *" $cmd "* ]]; then + shopt -s expand_aliases + local -a args + eval "$cmd ${@// /\\\ }" + else + "$cmd" "$@" + fi return fi diff --git a/libexec/aliases b/libexec/aliases index ecfbbcd..3fd8795 100755 --- a/libexec/aliases +++ b/libexec/aliases @@ -22,7 +22,17 @@ # Note that "cd" and "pushd" are only available after you've used "{{go}} env" # to set up your shell environment. -declare -r _GO_ALIAS_CMDS=('awk' 'cat' 'cd' 'find' 'grep' 'ls' 'pushd' 'sed') +declare _GO_ALIAS_CMDS=('awk' 'cat' 'cd' 'find' 'grep' 'ls' 'pushd' 'sed') + +if [[ "${GO_ALIAS_CMDS_EXTRA[*]}" != '' ]]; then + _GO_ALIAS_CMDS+=( "${GO_ALIAS_CMDS_EXTRA[@]}" ) +fi + +if [[ "${GO_ALIAS_EXPAND_CMDS[*]}" != '' ]]; then + _GO_ALIAS_CMDS+=( "${GO_ALIAS_EXPAND_CMDS[@]}" ) +fi + +readonly _GO_ALIAS_CMDS _@go.aliases() { local c diff --git a/tests/aliases.bats b/tests/aliases.bats index e8f4fac..7fa3fdc 100644 --- a/tests/aliases.bats +++ b/tests/aliases.bats @@ -2,13 +2,37 @@ load environment -@test "$SUITE: with no arguments, list all aliases" { +create_aliases_test_command_script() { + @go.create_test_go_script \ + 'declare -a GO_ALIAS_CMDS_EXTRA=("nvim")' \ + 'declare -a GO_ALIAS_EXPAND_CMDS=("test_echo")' \ + 'alias test_echo="echo -n test_string_19098e09818fad"' \ + "@go \$@" +} + +@test "$SUITE: with no arguments, list all predefined aliases" { run ./go aliases assert_success assert_line_equals 0 'awk' # first alias assert_line_equals -1 'sed' # last alias } +@test "$SUITE: list custom aliases if defined" { + create_aliases_test_command_script + run "$TEST_GO_SCRIPT" 'aliases' + assert_success + assert_line_equals 0 'awk' # first alias + assert_line_equals -2 'nvim' # second to last alias + assert_line_equals -1 'test_echo' # last alias +} + +@test "$SUITE: run expanded aliases if defined" { + create_aliases_test_command_script 'aliases' + run "$TEST_GO_SCRIPT" 'test_echo' + assert_success + assert_line_equals 0 'test_string_19098e09818fad' +} + @test "$SUITE: tab completions" { run ./go complete 1 aliases '' assert_success '--exists ' @@ -98,7 +122,7 @@ load environment @test "$SUITE: leave help generic for cd, pushd when using env function" { # Setting _GO_CMD will trick the script into thinking the shell function is # running it. - + _GO_CMD='test-go' run ./go aliases --help cd [ "$status" -eq '0' ]