From c92fc2382f3091c92628d49a2bb605760813489f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Fri, 12 Apr 2024 14:57:54 +0300 Subject: [PATCH] gg.sh: lint --- shared/home/gg.sh | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/shared/home/gg.sh b/shared/home/gg.sh index 11b2e34..5e3e30a 100644 --- a/shared/home/gg.sh +++ b/shared/home/gg.sh @@ -1,31 +1,37 @@ -maxdepth=5 -_gopath=$(git rev-parse --show-toplevel) +# shellcheck shell=bash +_GG_MAXDEPTH=5 gg() { - paths=($(g "$@")) - path_index=0 + local _gopath + _gopath=$(git rev-parse --show-toplevel) + local paths=($(g "$@")) + local path_index=0 if [ ${#paths[@]} -gt 1 ]; then - c=1 + local c=1 for path in "${paths[@]}"; do - echo [$c]: cd ${_gopath}/${path} + echo "[$c]: cd ${_gopath}/${path}" c=$((c+1)) done echo -n "Go to which path: " - read path_index + read -r path_index - path_index=$(($path_index-1)) + path_index=$((path_index-1)) fi - path=${paths[$path_index]} - cd $_gopath/$path + local path=${paths[$path_index]} + cd "$_gopath/$path" || { + >&2 echo "?" + exit 1 + } } # # Print the directories of the specified Go package name. # g() { - local pkg_candidates="$((cd $_gopath && find . -mindepth 1 -maxdepth ${maxdepth} -type d -path "*/$1" -and -not -path '*/vendor/*' -print) | sed 's/^\.\///g')" + local pkg_candidates + pkg_candidates="$( (cd "$_gopath" && find . -mindepth 1 -maxdepth ${_GG_MAXDEPTH} -type d -path "*/$1" -and -not -path '*/vendor/*' -print) | sed 's/^\.\///g')" echo "$pkg_candidates" | awk '{print length, $0 }' | sort -n | awk '{print $2}' } # @@ -35,10 +41,8 @@ _g_complete() { COMPREPLY=() local cur - local prev cur="${COMP_WORDS[COMP_CWORD]}" - prev="${COMP_WORDS[COMP_CWORD-1]}" - COMPREPLY=( $(compgen -W "$(for f in $(find "$_gopath" -mindepth 1 -maxdepth ${maxdepth} -type d -name "${cur}*" ! -name '.*' ! -path '*/.git/*' ! -path '*/test/*' ! -path '*/vendor/*'); do echo "${f##*/}"; done)" -- "$cur") ) + COMPREPLY=( $(compgen -W "$(for f in $(find "$_gopath" -mindepth 1 -maxdepth ${_GG_MAXDEPTH} -type d -name "${cur}*" ! -name '.*' ! -path '*/.git/*' ! -path '*/test/*' ! -path '*/vendor/*'); do echo "${f##*/}"; done)" -- "$cur") ) return 0 } complete -F _g_complete g