From 565bcda2908954ef52b3db6efb4abf2b17552e90 Mon Sep 17 00:00:00 2001 From: Nikolaos Kakouros Date: Sat, 17 Nov 2018 16:10:29 +0100 Subject: [PATCH 1/7] Allows users to declare custom aliases --- libexec/aliases | 8 +++++++- tests/aliases.bats | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/libexec/aliases b/libexec/aliases index ecfbbcd..b28718d 100755 --- a/libexec/aliases +++ b/libexec/aliases @@ -22,7 +22,13 @@ # 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 + +readonly _GO_ALIAS_CMDS _@go.aliases() { local c diff --git a/tests/aliases.bats b/tests/aliases.bats index e8f4fac..2c14a59 100644 --- a/tests/aliases.bats +++ b/tests/aliases.bats @@ -2,6 +2,12 @@ load environment +create_aliases_test_command_script() { + @go.create_test_go_script \ + 'declare -a GO_ALIAS_CMDS_EXTRA=("nvim")' \ + "@go $@" +} + @test "$SUITE: with no arguments, list all aliases" { run ./go aliases assert_success @@ -9,6 +15,14 @@ load environment assert_line_equals -1 'sed' # last alias } +@test "$SUITE: list custom aliases if defined" { + create_aliases_test_command_script 'aliases' + run "$TEST_GO_SCRIPT" aliases + assert_success + assert_line_equals 0 'awk' # first alias + assert_line_equals -1 'nvim' # last alias +} + @test "$SUITE: tab completions" { run ./go complete 1 aliases '' assert_success '--exists ' From c0f6f7ab5ced1a5c06a66f2bb0290061cf91a926 Mon Sep 17 00:00:00 2001 From: tterranigma Date: Sun, 18 Nov 2018 16:23:10 +0100 Subject: [PATCH 2/7] Allows aliases to be expanded --- go-core.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go-core.bash b/go-core.bash index 61cf511..898e3c2 100755 --- a/go-core.bash +++ b/go-core.bash @@ -319,7 +319,7 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH" esac if _@go.source_builtin 'aliases' --exists "$cmd"; then - "$cmd" "$@" + eval "$cmd" "$@" return fi From c88c65e8c285407e3eaf3ef58f58c87d7f247054 Mon Sep 17 00:00:00 2001 From: Nikolaos Kakouros Date: Wed, 21 Nov 2018 00:15:50 +0100 Subject: [PATCH 3/7] Allows users to declare custom aliases --- go-core.bash | 6 +++++- libexec/aliases | 4 ++++ tests/aliases.bats | 23 +++++++++++++++++------ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/go-core.bash b/go-core.bash index 898e3c2..c385911 100755 --- a/go-core.bash +++ b/go-core.bash @@ -319,7 +319,11 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH" esac if _@go.source_builtin 'aliases' --exists "$cmd"; then - eval "$cmd" "$@" + if [[ " ${GO_ALIAS_EXPAND_CMDS[*]} " == *" $cmd "* ]]; then + eval "$cmd" "$@" + else + "$cmd" "$@" + fi return fi diff --git a/libexec/aliases b/libexec/aliases index b28718d..3fd8795 100755 --- a/libexec/aliases +++ b/libexec/aliases @@ -28,6 +28,10 @@ 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() { diff --git a/tests/aliases.bats b/tests/aliases.bats index 2c14a59..0a31dde 100644 --- a/tests/aliases.bats +++ b/tests/aliases.bats @@ -5,10 +5,13 @@ load environment create_aliases_test_command_script() { @go.create_test_go_script \ 'declare -a GO_ALIAS_CMDS_EXTRA=("nvim")' \ - "@go $@" + 'declare -a GO_ALIAS_EXPAND_CMDS=("test_echo")' \ + 'shopt -s expand_aliases' \ + 'alias test_echo="echo -n test_string_19098e09818fad"' \ + "@go \$@" } -@test "$SUITE: with no arguments, list all aliases" { +@test "$SUITE: with no arguments, list all predefined aliases" { run ./go aliases assert_success assert_line_equals 0 'awk' # first alias @@ -16,11 +19,19 @@ create_aliases_test_command_script() { } @test "$SUITE: list custom aliases if defined" { - create_aliases_test_command_script 'aliases' - run "$TEST_GO_SCRIPT" aliases + create_aliases_test_command_script + run "$TEST_GO_SCRIPT" 'aliases' assert_success assert_line_equals 0 'awk' # first alias - assert_line_equals -1 'nvim' # last 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" { @@ -112,7 +123,7 @@ create_aliases_test_command_script() { @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' ] From 3057e2ba24624cea5e03515fa5fba4f6c17984ab Mon Sep 17 00:00:00 2001 From: Nikolaos Kakouros Date: Wed, 21 Nov 2018 01:34:44 +0100 Subject: [PATCH 4/7] Takes care of arguments with spaces --- go-core.bash | 3 +++ 1 file changed, 3 insertions(+) diff --git a/go-core.bash b/go-core.bash index c385911..3f1396b 100755 --- a/go-core.bash +++ b/go-core.bash @@ -320,6 +320,9 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH" if _@go.source_builtin 'aliases' --exists "$cmd"; then if [[ " ${GO_ALIAS_EXPAND_CMDS[*]} " == *" $cmd "* ]]; then + local -a args + args="${@/#/\"}" + args="${@/%/\"}" eval "$cmd" "$@" else "$cmd" "$@" From c4873e88dfd040b6d6909d3bf6d99ceadd00001f Mon Sep 17 00:00:00 2001 From: Nikolaos Kakouros Date: Sat, 16 Feb 2019 15:14:26 +0100 Subject: [PATCH 5/7] Moves "expand_aliases" shell option to go-core --- go-core.bash | 3 +-- tests/aliases.bats | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/go-core.bash b/go-core.bash index 3f1396b..7988a8e 100755 --- a/go-core.bash +++ b/go-core.bash @@ -320,9 +320,8 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH" if _@go.source_builtin 'aliases' --exists "$cmd"; then if [[ " ${GO_ALIAS_EXPAND_CMDS[*]} " == *" $cmd "* ]]; then + shopt -s expand_aliases local -a args - args="${@/#/\"}" - args="${@/%/\"}" eval "$cmd" "$@" else "$cmd" "$@" diff --git a/tests/aliases.bats b/tests/aliases.bats index 0a31dde..7fa3fdc 100644 --- a/tests/aliases.bats +++ b/tests/aliases.bats @@ -6,7 +6,6 @@ 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")' \ - 'shopt -s expand_aliases' \ 'alias test_echo="echo -n test_string_19098e09818fad"' \ "@go \$@" } From a5a980c9e22201ec59a69a46a1aa8cf8d20076b4 Mon Sep 17 00:00:00 2001 From: Nikolaos Kakouros Date: Wed, 3 Apr 2019 15:28:29 +0200 Subject: [PATCH 6/7] Allows spaces in alias arguments --- go-core.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go-core.bash b/go-core.bash index 7988a8e..6881d34 100755 --- a/go-core.bash +++ b/go-core.bash @@ -322,7 +322,7 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH" if [[ " ${GO_ALIAS_EXPAND_CMDS[*]} " == *" $cmd "* ]]; then shopt -s expand_aliases local -a args - eval "$cmd" "$@" + eval "$cmd ${@/ /\\\ }" else "$cmd" "$@" fi From 384915403b04ac790fea32bf6dce8a5c26c2fa2d Mon Sep 17 00:00:00 2001 From: Nikolaos Kakouros Date: Sun, 21 Apr 2019 21:02:52 +0200 Subject: [PATCH 7/7] Escapes all spaces, not only the first one --- go-core.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go-core.bash b/go-core.bash index 6881d34..25c76f6 100755 --- a/go-core.bash +++ b/go-core.bash @@ -322,7 +322,7 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH" if [[ " ${GO_ALIAS_EXPAND_CMDS[*]} " == *" $cmd "* ]]; then shopt -s expand_aliases local -a args - eval "$cmd ${@/ /\\\ }" + eval "$cmd ${@// /\\\ }" else "$cmd" "$@" fi