From d4d64f654eaa5e7b00a7573c7be59d9d9430bc9c Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Sat, 23 May 2020 19:16:06 +0100 Subject: [PATCH] Add command-exists shell helper --- zsh/aliases.zsh | 4 ++-- zsh/docker.zsh | 2 +- zsh/kubernetes.zsh | 2 +- zsh/less.zsh | 2 +- zsh/python.zsh | 4 ++-- zsh/ruby.zsh | 2 +- zshrc | 8 ++++++++ 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/zsh/aliases.zsh b/zsh/aliases.zsh index e5697b8..25055e2 100644 --- a/zsh/aliases.zsh +++ b/zsh/aliases.zsh @@ -27,7 +27,7 @@ alias di="colordiff" alias devnullsmtp="java -jar $DOTBIN/DevNullSmtp.jar" # Homebrew -if (( $+commands[brew] )); then +if command-exists brew; then alias br="brew" alias ca="brew cask" alias cask="brew cask" @@ -36,7 +36,7 @@ if (( $+commands[brew] )); then fi # Flutter -if (( $+commands[flutter] )); then +if command-exists flutter; then alias fl="flutter" fi diff --git a/zsh/docker.zsh b/zsh/docker.zsh index 86e385d..855ef7d 100644 --- a/zsh/docker.zsh +++ b/zsh/docker.zsh @@ -10,7 +10,7 @@ docker_remove_exited () { docker rm "$(docker ps -f='status=exited' -q)" } -if (( $+commands[docker] )); then +if command-exists docker; then zinit ice from'gh-r' as'program' mv'ctop-* -> ctop' zinit light bcicen/ctop fi diff --git a/zsh/kubernetes.zsh b/zsh/kubernetes.zsh index e6990ee..04580b9 100644 --- a/zsh/kubernetes.zsh +++ b/zsh/kubernetes.zsh @@ -6,7 +6,7 @@ alias kc="kubectl" alias hl="helm" alias mk="minikube" -if (( $+commands[kubectl] )); then +if command-exists kubectl; then # lazy-load kubectl setup _kubectl() { unset -f _kubectl diff --git a/zsh/less.zsh b/zsh/less.zsh index e6960cb..77498f5 100644 --- a/zsh/less.zsh +++ b/zsh/less.zsh @@ -5,7 +5,7 @@ alias le="less" # Enable syntax highlighting via source-highlight -if (( $+commands[src-hilite-lesspipe.sh] )); then +if command-exists "src-hilite-lesspipe.sh"; then export LESSOPEN="| src-hilite-lesspipe.sh %s" export LESS=" -R " elif [ -f "/usr/share/source-highlight/src-hilite-lesspipe.sh" ]; then diff --git a/zsh/python.zsh b/zsh/python.zsh index 6414118..523705f 100644 --- a/zsh/python.zsh +++ b/zsh/python.zsh @@ -10,12 +10,12 @@ install_python_global_packages () { 'yamllint' ) - if [[ "$OSTYPE" == "darwin"* ]] && (( $+commands[brew] )); then + if [[ "$OSTYPE" == "darwin"* ]] && command-exists brew; then brew list python >/dev/null || brew install python fi local pipcmd="pip" - if (( $+commands[pip3] )); then; pipcmd="pip3"; fi + if command-exists pip3; then pipcmd="pip3"; fi "$pipcmd" install --upgrade setuptools "$pipcmd" install --upgrade "${packages[@]}" diff --git a/zsh/ruby.zsh b/zsh/ruby.zsh index b891d08..620ddb6 100755 --- a/zsh/ruby.zsh +++ b/zsh/ruby.zsh @@ -50,7 +50,7 @@ alias bo="bundle open" alias bp="bundle package" alias bu="bundle update" -if (( $+commands[rbenv] )); then +if command-exists rbenv; then # lazy-load rbenv rbenv() { load-rbenv diff --git a/zshrc b/zshrc index fe0d833..23f03fb 100644 --- a/zshrc +++ b/zshrc @@ -9,6 +9,14 @@ if [[ "$OSTYPE" == "darwin"* ]] && [ -f "/etc/zshrc" ]; then source "/etc/zshrc" fi +# ============================================================================== +# Helpers +# ============================================================================== + +command-exists() { + command -v "$1" &> /dev/null + return $? +} # ============================================================================== # Zinit