Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows users to declare custom aliases #247

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion go-core.bash
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,13 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
esac

if [email protected]_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

Expand Down
12 changes: 11 additions & 1 deletion libexec/aliases
Original file line number Diff line number Diff line change
Expand Up @@ -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

[email protected]() {
local c
Expand Down
28 changes: 26 additions & 2 deletions tests/aliases.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down Expand Up @@ -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' ]

Expand Down