From 21c3b66a77ea3ffd02aec15917493ee33815074e Mon Sep 17 00:00:00 2001 From: Shawn Wallis Date: Fri, 9 Mar 2018 12:55:41 -0500 Subject: [PATCH 01/19] Adds a command to expand an alias to its value --- goto.bash | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/goto.bash b/goto.bash index 2a5c4ba..c601d0a 100644 --- a/goto.bash +++ b/goto.bash @@ -48,6 +48,9 @@ function goto() -l|--list) _goto_list_aliases ;; + -x|--expand) # Expand an alias + _goto_expand_alias "$@" + ;; -h|--help) _goto_usage ;; @@ -75,6 +78,8 @@ OPTIONS: goto -u|--unregister -l, --list: lists aliases goto -l|--list + -x, --expand: expands an alias + goto -x|--expand -c, --cleanup: cleans up non existent directory aliases goto -c|--cleanup -h, --help: prints this help @@ -108,7 +113,24 @@ function _goto_list_aliases() fi } -# Registers and alias. +# Expands a registered alias. +function _goto_expand_alias() +{ + if [ "$#" -ne "1" ]; then + _goto_error "usage: goto -x|--expand " + return + fi + + local resolved=$(_goto_find_alias_directory $1) + if [ -z "$resolved" ]; then + _goto_error "alias '$1' does not exist" + return + fi + + echo "$resolved" +} + +# Registers an alias. function _goto_register_alias() { if [ "$#" -ne "2" ]; then @@ -235,7 +257,7 @@ function _complete_goto_commands() local IFS=$' \t\n' # shellcheck disable=SC2207 - COMPREPLY=($(compgen -W "-r --register -u --unregister -l --list -c --cleanup -v --version" -- "$1")) + COMPREPLY=($(compgen -W "-r --register -u --unregister -l --list -x --expand -c --cleanup -v --version" -- "$1")) } # Completes the goto function with the available aliases From b1747d35ec19df15c0c45273ecf02f3feb705b8c Mon Sep 17 00:00:00 2001 From: James Short Date: Thu, 8 Mar 2018 11:49:26 -0800 Subject: [PATCH 02/19] Add support for zsh completion. I also made the documentation and name of file more generic as it works for both zsh and bash. I removed the shebang since this file is simply sourced by the calling shell and it doesn't have executable permissions anyway. The various functions were updated to return non-zero on errors in case anyone uses goto in other scripts. The logic for parsing ~/.goto to determine which aliases can be cleaned was simplified into an awk script that removed the array manipulation logic that was not zsh compatible. --- README.md | 13 +++--- goto.bash => goto.sh | 100 ++++++++++++++++++++++++++++--------------- 2 files changed, 71 insertions(+), 42 deletions(-) rename goto.bash => goto.sh (79%) diff --git a/README.md b/README.md index b5c8737..9f35cb3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # goto -`goto` is a bash utility allowing users to change faster to aliased directories supporting auto-completion :feet: +`goto` is a shell utility allowing users to change faster to aliased directories supporting auto-completion :feet: ## How does it work? @@ -17,7 +17,7 @@ goto dev ## goto completion -`goto` comes with a nice auto-completion script so that whenever you press the `tab` key after the `goto` command, bash prompts with suggestions of the available aliases: +`goto` comes with a nice auto-completion script so that whenever you press the `tab` key after the `goto` command, bash or zsh prompts with suggestions of the available aliases: ```bash $ goto @@ -28,12 +28,12 @@ rubies /home/iridakos/.rvm/rubies ## Installation -Copy the file `goto.bash` somewhere in your filesystem and add a line in your `.bashrc` to source it. +Copy the file `goto.sh` somewhere in your filesystem and add a line in your `.zshrc` or `.bashrc` to source it. -For example, if you placed the file in your home folder, all you have to do is add the following line to your `.bashrc` file: +For example, if you placed the file in your home folder, all you have to do is add the following line to your `.zshrc` or `.bashrc` file: ```bash -source ~/goto.bash +source ~/goto.sh ``` ## Usage @@ -75,7 +75,7 @@ goto --register blog /mnt/external/projects/html/blog goto -r last_release . ``` and it will automatically be aliased to the whole path. -* Pressing the `tab` key after the alias name, you have the default directory suggestions by bash. +* Pressing the `tab` key after the alias name, you have the default directory suggestions by the shell. ### Unregister an alias @@ -148,7 +148,6 @@ goto --version ## TODO * ~~Test on macOS~~ extensively -* Fix `zsh` issues [[#7](https://github.com/iridakos/goto/issues/7), ...] * Write [tests](https://github.com/iridakos/goto/issues/2) ## Contributing diff --git a/goto.bash b/goto.sh similarity index 79% rename from goto.bash rename to goto.sh index 2a5c4ba..5ad141c 100644 --- a/goto.bash +++ b/goto.sh @@ -1,4 +1,3 @@ -#!/usr/bin/env bash # MIT License # # Copyright (c) 2018 Lazarus Lazaridis @@ -58,6 +57,7 @@ function goto() _goto_directory "$subcommand" ;; esac + return $? } function _goto_usage() @@ -113,12 +113,12 @@ function _goto_register_alias() { if [ "$#" -ne "2" ]; then _goto_error "usage: goto -r|--register " - return + return 1 fi if ! [[ $1 =~ ^[[:alnum:]]+[a-zA-Z0-9_-]*$ ]]; then _goto_error "invalid alias - can start with letters or digits followed by letters, digits, hyphens or underscores" - return + return 1 fi local resolved @@ -126,14 +126,14 @@ function _goto_register_alias() if [ -n "$resolved" ]; then _goto_error "alias '$1' exists" - return + return 1 fi local directory directory=$(_goto_expand_directory "$2") if [ -z "$directory" ]; then _goto_error "failed to register '$1' to '$2' - can't cd to directory" - return + return 1 fi # Append entry to file. @@ -146,14 +146,14 @@ function _goto_unregister_alias { if [ "$#" -ne "1" ]; then _goto_error "usage: goto -u|--unregister " - return + return 1 fi local resolved resolved=$(_goto_find_alias_directory "$1") if [ -z "$resolved" ]; then _goto_error "alias '$1' does not exist" - return + return 1 fi # Delete entry from file. @@ -164,26 +164,15 @@ function _goto_unregister_alias # Unregisters aliases whose directories no longer exist. function _goto_cleanup() { - if ! [ -f ~/.goto ]; then + if [ ! -f ~/.goto ]; then return fi - local IFS=$'\n' match matches al dir - - read -d '' -r -a matches < ~/.goto - - IFS=' ' - for i in "${!matches[@]}"; do - read -r -a match <<< "${matches[$i]}" - - al="${match[0]}" - dir="${match[*]:1}" - - if [ -n "$al" ] && [ ! -d "$dir" ]; then - echo "Cleaning up: $al - $dir" - _goto_unregister_alias "$al" - fi - done + while IFS= read -r i && [ -n "$i" ]; do + echo "Cleaning up: $i" + _goto_unregister_alias "$i" + done <<< "$(awk '{al=$1; $1=""; dir=substr($0,2); + system("[ ! -d \"" dir "\" ] && echo " al)}' ~/.goto)" } # Changes to the given alias' directory @@ -191,11 +180,10 @@ function _goto_directory() { local target - target=$(_goto_resolve_alias "$1") + target=$(_goto_resolve_alias "$1") || return 1 - if [ -n "$target" ]; then - cd "$target" || _goto_error "Failed to goto '$target'" - fi + cd "$target" 2> /dev/null || \ + { _goto_error "Failed to goto '$target'" && return 1; } } # Fetches the alias directory. @@ -223,7 +211,7 @@ function _goto_resolve_alias() if [ -z "$resolved" ]; then _goto_error "unregistered alias $1" - echo "" + return 1 else echo "${resolved}" fi @@ -241,7 +229,7 @@ function _complete_goto_commands() # Completes the goto function with the available aliases function _complete_goto_aliases() { - local IFS=$'\n' matches al + local IFS=$'\n' matches # shellcheck disable=SC2207 matches=($(sed -n "/^$1/p" ~/.goto 2>/dev/null)) @@ -269,7 +257,7 @@ function _complete_goto_aliases() } # Bash programmable completion for the goto function -function _complete_goto() +function _complete_goto_bash() { local cur="${COMP_WORDS[$COMP_CWORD]}" prev @@ -304,9 +292,51 @@ function _complete_goto() fi } -# Register the goto compspec -if ! [[ $(uname -s) =~ Darwin* ]]; then - complete -o filenames -F _complete_goto goto +# Zsh programmable completion for the goto function +function _complete_goto_zsh() +{ + local all_aliases=() + while IFS= read -r line; do + all_aliases+=("$line") + done <<< "$(sed -e 's/ /:/g' ~/.goto 2>/dev/null)" + + local state + local -a options=( + '(1)'{-r,--register}'[registers an alias]:register:->register' + '(- 1 2)'{-u,--unregister}'[unregisters an alias]:unregister:->unregister' + '(: -)'{-l,--list}'[lists aliases]' + '(*)'{-c,--cleanup}'[cleans up non existent directory aliases]' + '(: -)'{-h,--help}'[prints this help]' + '(* -)'{-v,--version}'[displays the version of the goto script]' + ) + + _arguments -C \ + "${options[@]}" \ + '1:alias:->aliases' \ + '2:dir:_files' \ + && ret=0 + + case ${state} in + (aliases) + _describe -t aliases 'goto aliases:' all_aliases && ret=0 + ;; + (unregister) + _describe -t aliases 'unregister alias:' all_aliases && ret=0 + ;; + esac + return $ret +} + +# Register the goto completions. +if [ -n "${BASH_VERSION}" ]; then + if ! [[ $(uname -s) =~ Darwin* ]]; then + complete -o filenames -F _complete_goto_bash goto + else + complete -F _complete_goto_bash goto + fi +elif [ -n "${ZSH_VERSION}" ]; then + compdef _complete_goto_zsh goto else - complete -F _complete_goto goto + echo "Unsupported shell." + exit 1 fi From df46ea166befa84eaeb4ea7414bb743a505c62d6 Mon Sep 17 00:00:00 2001 From: Shawn Wallis Date: Fri, 9 Mar 2018 13:48:45 -0500 Subject: [PATCH 03/19] Adds auto-completion and documentation for expand command --- README.md | 20 ++++++++++++++++++++ goto.bash | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5c8737..86d9d29 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,26 @@ or goto --list ``` +### Expand an alias + +To expand an alias to its value, use: +```bash +goto -x +``` +or +```bash +goto --expand +``` + +#### Example +```bash +goto -x last_release +``` +or +```bash +goto --expand last_release +``` + ### Cleanup To cleanup the aliases from directories that are no longer accessible in your filesystem, use: diff --git a/goto.bash b/goto.bash index c601d0a..46b8147 100644 --- a/goto.bash +++ b/goto.bash @@ -309,7 +309,10 @@ function _complete_goto() prev="${COMP_WORDS[1]}" if [[ $prev = "-u" ]] || [[ $prev = "--unregister" ]]; then - # prompt with aliases only if user tries to unregister one + # prompt with aliases if user tries to unregister one + _complete_goto_aliases "$cur" + elif [[ $prev = "-x" ]] || [[ $prev = "--expand" ]]; then + # prompt with aliases if user tries to expand one _complete_goto_aliases "$cur" fi elif [ "$COMP_CWORD" -eq "3" ]; then From bdf17eb7052199751437690f40ff9d1ed955861d Mon Sep 17 00:00:00 2001 From: Omri Date: Sun, 11 Mar 2018 12:00:20 +0200 Subject: [PATCH 04/19] Added install script --- install | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 install diff --git a/install b/install new file mode 100755 index 0000000..902a72a --- /dev/null +++ b/install @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +function _goto_install_error() +{ + echo "$@" >&2 +} + + # sudo-check + if [ $EUID -ne 0 ]; then + _goto_install_error 'Please run this script in sudo mode or as root user' + exit + fi + + GOTO_FILE_LOCATION='/usr/local/share/goto.bash' + RC="" + if [ -f ~/.bashrc ]; then + RC="$HOME/.bashrc" + elif [ -f ~/.cshrc ]; then + RC="$HOME/.cshrc" + elif [ -f ~/.kshrc ]; then + RC="$HOME/.kshrc" + elif [ -f ~/.zshrc ]; then + RC="$HOME/.zshrc" + fi + + cp ./goto.bash "$GOTO_FILE_LOCATION" + + if [ -n "$RC" ]; then + # Append source to RC startup file if not already there + if [ `grep -c "source $GOTO_FILE_LOCATION" "$RC"` == "0" ]; then + # Append source to RC file + echo -e "\n#Source goto\nsource $GOTO_FILE_LOCATION\n" >> "$RC" + fi + source "$GOTO_FILE_LOCATION" + else + _goto_install_error "Error sourcing goto in your startup file.." + _goto_install_error "E.g ~/.bashrc ~/.cshrc etc.." + fi + From 6cfe42eda324aa4597478a1491dae7c03e291151 Mon Sep 17 00:00:00 2001 From: Omri Date: Sun, 11 Mar 2018 12:03:48 +0200 Subject: [PATCH 05/19] Added GOTO_DB variable --- goto.bash | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/goto.bash b/goto.bash index 2a5c4ba..400deaf 100644 --- a/goto.bash +++ b/goto.bash @@ -26,6 +26,7 @@ function goto() { local target + readonly GOTO_DB="$HOME/.goto" if [ -z "$1" ]; then # display usage and exit when no args @@ -101,8 +102,8 @@ function _goto_expand_directory() function _goto_list_aliases() { local IFS=$'\n' - if [ -f ~/.goto ]; then - sed '/^\s*$/d' ~/.goto 2>/dev/null + if [ -f "$GOTO_DB" ]; then + sed '/^\s*$/d' "$GOTO_DB" 2>/dev/null else echo "You haven't configured any directory aliases yet." fi @@ -137,7 +138,7 @@ function _goto_register_alias() fi # Append entry to file. - echo "$1 $directory" >> ~/.goto + echo "$1 $directory" >> "$GOTO_DB" echo "Alias '$1' registered successfully." } @@ -155,22 +156,23 @@ function _goto_unregister_alias _goto_error "alias '$1' does not exist" return fi - + + local readonly GOTO_DB_TMP="$HOME/.goto_" # Delete entry from file. - sed "/^$1 /d" ~/.goto > ~/.goto_ && mv ~/.goto_ ~/.goto + sed "/^$1 /d" "$GOTO_DB" > "$GOTO_DB_TMP" && mv "$GOTO_DB_TMP" "$GOTO_DB" echo "Alias '$1' unregistered successfully." } # Unregisters aliases whose directories no longer exist. function _goto_cleanup() { - if ! [ -f ~/.goto ]; then + if ! [ -f "$GOTO_DB" ]; then return fi local IFS=$'\n' match matches al dir - read -d '' -r -a matches < ~/.goto + read -d '' -r -a matches < "$GOTO_DB" IFS=' ' for i in "${!matches[@]}"; do @@ -203,7 +205,7 @@ function _goto_find_alias_directory() { local resolved - resolved=$(sed -n "s/^$1 \\(.*\\)/\\1/p" ~/.goto 2>/dev/null) + resolved=$(sed -n "s/^$1 \\(.*\\)/\\1/p" "$GOTO_DB" 2>/dev/null) echo "$resolved" } @@ -214,6 +216,17 @@ function _goto_error() (>&2 echo "goto error: $1") } +function _goto_print_similar() +{ + local similar + + similar=$(sed -n "/^$1[^ ]* .*/p" "$GOTO_DB" 2>/dev/null) + if [ -n "$similar" ]; then + (>&2 echo "Did you mean:") + (>&2 echo "$similar" | column -t) + fi +} + # Fetches alias directory, errors if it doesn't exist. function _goto_resolve_alias() { @@ -223,6 +236,7 @@ function _goto_resolve_alias() if [ -z "$resolved" ]; then _goto_error "unregistered alias $1" + _goto_print_similar "$1" echo "" else echo "${resolved}" @@ -244,7 +258,7 @@ function _complete_goto_aliases() local IFS=$'\n' matches al # shellcheck disable=SC2207 - matches=($(sed -n "/^$1/p" ~/.goto 2>/dev/null)) + matches=($(sed -n "/^$1/p" "$GOTO_DB" 2>/dev/null)) if [ "${#matches[@]}" -eq "1" ]; then # remove the filenames attribute from the completion method From 3bb1dc79de00edc72208664f9f0e3d22977406ec Mon Sep 17 00:00:00 2001 From: Omri Date: Sun, 11 Mar 2018 12:49:33 +0200 Subject: [PATCH 06/19] Added duplicate note --- goto.bash | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/goto.bash b/goto.bash index 400deaf..ae64e21 100644 --- a/goto.bash +++ b/goto.bash @@ -26,7 +26,7 @@ function goto() { local target - readonly GOTO_DB="$HOME/.goto" + GOTO_DB="$HOME/.goto" if [ -z "$1" ]; then # display usage and exit when no args @@ -109,6 +109,12 @@ function _goto_list_aliases() fi } +function _goto_find_duplicate() +{ + local duplicates=$(sed -n 's:[^ ]* '"$1"'$:&:p' "$GOTO_DB" 2>/dev/null) + echo "$duplicates" +} + # Registers and alias. function _goto_register_alias() { @@ -137,9 +143,16 @@ function _goto_register_alias() return fi + local duplicate=$(_goto_find_duplicate "$directory") + # Append entry to file. echo "$1 $directory" >> "$GOTO_DB" echo "Alias '$1' registered successfully." + + if [ -n "$duplicate" ]; then + echo -e "note: duplicate alias found:\n$duplicate" + return + fi } # Unregisters the given alias. From fe1ca689501cbc9f432c00f615ec85bee47e831e Mon Sep 17 00:00:00 2001 From: Omri Date: Sun, 11 Mar 2018 13:02:24 +0200 Subject: [PATCH 07/19] Updated readme's install --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b5c8737..5f6d8a0 100644 --- a/README.md +++ b/README.md @@ -27,13 +27,11 @@ rubies /home/iridakos/.rvm/rubies ``` ## Installation - -Copy the file `goto.bash` somewhere in your filesystem and add a line in your `.bashrc` to source it. - -For example, if you placed the file in your home folder, all you have to do is add the following line to your `.bashrc` file: - +Clone the repository and run the install script as super user or root ```bash -source ~/goto.bash +git clone https://github.com/iridakos/goto.git +cd goto +sudo ./install ``` ## Usage From a1c2b8a78cad3e5a326c3861ef00ef4fedb4ffff Mon Sep 17 00:00:00 2001 From: Lazarus Lazaridis Date: Mon, 12 Mar 2018 06:40:47 +0200 Subject: [PATCH 08/19] Fix shellcheck errors and warnings. --- goto.bash | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/goto.bash b/goto.bash index ae64e21..78eb872 100644 --- a/goto.bash +++ b/goto.bash @@ -111,7 +111,9 @@ function _goto_list_aliases() function _goto_find_duplicate() { - local duplicates=$(sed -n 's:[^ ]* '"$1"'$:&:p' "$GOTO_DB" 2>/dev/null) + local duplicates= + + duplicates=$(sed -n 's:[^ ]* '"$1"'$:&:p' "$GOTO_DB" 2>/dev/null) echo "$duplicates" } @@ -143,7 +145,8 @@ function _goto_register_alias() return fi - local duplicate=$(_goto_find_duplicate "$directory") + local duplicate + duplicate=$(_goto_find_duplicate "$directory") # Append entry to file. echo "$1 $directory" >> "$GOTO_DB" @@ -169,7 +172,8 @@ function _goto_unregister_alias _goto_error "alias '$1' does not exist" return fi - + + # shellcheck disable=SC2034 local readonly GOTO_DB_TMP="$HOME/.goto_" # Delete entry from file. sed "/^$1 /d" "$GOTO_DB" > "$GOTO_DB_TMP" && mv "$GOTO_DB_TMP" "$GOTO_DB" From a9fed9b549c3e13a2ec5c5b103a6a9b0b4a57d36 Mon Sep 17 00:00:00 2001 From: Lazarus Lazaridis Date: Mon, 12 Mar 2018 06:42:25 +0200 Subject: [PATCH 09/19] Enable interpretation of backslash escapes on error. --- goto.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/goto.bash b/goto.bash index 78eb872..cc4ddc0 100644 --- a/goto.bash +++ b/goto.bash @@ -230,7 +230,7 @@ function _goto_find_alias_directory() # Used for common error output. function _goto_error() { - (>&2 echo "goto error: $1") + (>&2 echo -e "goto error: $1") } function _goto_print_similar() From 109f0eb62f180b25d67c331c98d4822ce7c0e9cc Mon Sep 17 00:00:00 2001 From: Lazarus Lazaridis Date: Mon, 12 Mar 2018 06:43:55 +0200 Subject: [PATCH 10/19] Add function to display warnings ala errors. --- goto.bash | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/goto.bash b/goto.bash index cc4ddc0..19d1ba5 100644 --- a/goto.bash +++ b/goto.bash @@ -233,6 +233,13 @@ function _goto_error() (>&2 echo -e "goto error: $1") } +# Displays the given warning. +# Used for common warning output. +function _goto_warning() +{ + (>&2 echo -e "goto warning: $1") +} + function _goto_print_similar() { local similar From 51f8c88eba11b1f05e2286332c9e75ab5feae8c8 Mon Sep 17 00:00:00 2001 From: Lazarus Lazaridis Date: Mon, 12 Mar 2018 06:44:29 +0200 Subject: [PATCH 11/19] [MINOR] Fix indentation and comments --- goto.bash | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/goto.bash b/goto.bash index 19d1ba5..a3a28e5 100644 --- a/goto.bash +++ b/goto.bash @@ -109,6 +109,7 @@ function _goto_list_aliases() fi } +# Lists duplicate directory aliases function _goto_find_duplicate() { local duplicates= @@ -240,10 +241,11 @@ function _goto_warning() (>&2 echo -e "goto warning: $1") } +# Displays entries with aliases starting as the given one. function _goto_print_similar() { local similar - + similar=$(sed -n "/^$1[^ ]* .*/p" "$GOTO_DB" 2>/dev/null) if [ -n "$similar" ]; then (>&2 echo "Did you mean:") From 12336713a08799d359b7358f84d48571f4dde90b Mon Sep 17 00:00:00 2001 From: Lazarus Lazaridis Date: Mon, 12 Mar 2018 06:45:19 +0200 Subject: [PATCH 12/19] Move duplicate warning before registration message. --- goto.bash | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/goto.bash b/goto.bash index a3a28e5..8b08d44 100644 --- a/goto.bash +++ b/goto.bash @@ -148,15 +148,13 @@ function _goto_register_alias() local duplicate duplicate=$(_goto_find_duplicate "$directory") + if [ -n "$duplicate" ]; then + _goto_warning "duplicate alias(es) found: \\n$duplicate" + fi # Append entry to file. echo "$1 $directory" >> "$GOTO_DB" echo "Alias '$1' registered successfully." - - if [ -n "$duplicate" ]; then - echo -e "note: duplicate alias found:\n$duplicate" - return - fi } # Unregisters the given alias. From 19393468fcbb349c9346807b79bc13207b5b1a07 Mon Sep 17 00:00:00 2001 From: Lazarus Lazaridis Date: Mon, 12 Mar 2018 07:37:52 +0200 Subject: [PATCH 13/19] Resolve goto db upon completion. Fixes #20. --- goto.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/goto.sh b/goto.sh index 4af91e0..2f02f73 100644 --- a/goto.sh +++ b/goto.sh @@ -25,7 +25,7 @@ function goto() { local target - GOTO_DB="$HOME/.goto" + _goto_resolve_db if [ -z "$1" ]; then # display usage and exit when no args @@ -61,6 +61,11 @@ function goto() return $? } +function _goto_resolve_db() +{ + GOTO_DB="$HOME/.goto" +} + function _goto_usage() { cat <<\USAGE @@ -268,6 +273,7 @@ function _complete_goto_commands() function _complete_goto_aliases() { local IFS=$'\n' matches + _goto_resolve_db # shellcheck disable=SC2207 matches=($(sed -n "/^$1/p" "$GOTO_DB" 2>/dev/null)) From 37a960d000d9d6e0dd9cf6ee3aeff296ea843870 Mon Sep 17 00:00:00 2001 From: Lazarus Lazaridis Date: Mon, 12 Mar 2018 07:54:31 +0200 Subject: [PATCH 14/19] Fix shellcheck warnings. --- goto.sh | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/goto.sh b/goto.sh index 2f02f73..93c9e90 100644 --- a/goto.sh +++ b/goto.sh @@ -1,3 +1,5 @@ +#!/usr/bin/env sh +# shellcheck disable=SC2039 # MIT License # # Copyright (c) 2018 Lazarus Lazaridis @@ -22,7 +24,7 @@ # Changes to the given alias directory # or executes a command based on the arguments. -function goto() +goto() { local target _goto_resolve_db @@ -61,12 +63,12 @@ function goto() return $? } -function _goto_resolve_db() +_goto_resolve_db() { GOTO_DB="$HOME/.goto" } -function _goto_usage() +_goto_usage() { cat <<\USAGE usage: goto [