shell: have, path_(append|prepend|remove)

- have: command -v shorthand
- path_append: appends dir to PATH
- path_prepend: prepends dir to PATH
- path_remove: removes dir from PATH
This commit is contained in:
2023-04-14 00:42:08 +03:00
parent 4492c386b6
commit e5d6cb37fd
15 changed files with 124 additions and 111 deletions

2
.gitignore vendored
View File

@@ -1,6 +1,6 @@
Brewfile.lock.json
*.log
*-secret
*cache
.idea
.vscode

View File

@@ -6,6 +6,8 @@ colors
# Defaults
export DOTFILES="$HOME/.dotfiles"
# shellcheck source=shared.sh
source "$DOTFILES/scripts/shared.sh"
# Run x-load-configs in your terminal to reload the files.
function x-load-configs()
@@ -28,14 +30,14 @@ ANTIGEN_ZSH_PATH="$XDG_BIN_HOME/antigen.zsh"
[[ -f "$ANTIGEN_ZSH_PATH" ]] && source "$ANTIGEN_ZSH_PATH"
# antigen is present
if command -v antigen &> /dev/null; then
have antigen && {
antigen use oh-my-zsh
# config/functions
x-default-antigen-bundles
antigen apply
fi
eval "$(starship init zsh)"
}
# starship is present
have starship && eval "$(starship init zsh)"

View File

@@ -1,9 +1,11 @@
#!/usr/bin/env bash
# shellcheck source="../scripts/shared.sh"
source "$DOTFILES/scripts/shared.sh"
# Get installed php versions from brew and setup aliases
function x-set-php-aliases
{
if command -v brew &> /dev/null; then
have brew && {
local php_versions=()
while IFS="" read -r line; do php_versions+=("$line"); done < <(brew list | grep '^php')
@@ -31,7 +33,7 @@ function x-set-php-aliases
# shellcheck disable=SC2139,SC2140
alias "${php_alias}c"="$php_exec $php_error_reporting $(which composer)"
done
fi
}
}
if [[ $(uname) == 'Darwin' ]]; then
@@ -74,10 +76,8 @@ alias code_scanner='docker run
alias zedit='$EDITOR ~/.dotfiles'
if hash irssi 2> /dev/null; then
# shellcheck disable=2139
have irssi && \
alias irssi="irssi --config='$IRSSI_CONFIG_FILE' --home='$IRSSI_CONFIG_HOME'"
fi
if [[ -f "$HOME/.aliases.local" ]]; then
# shellcheck disable=SC1091

View File

@@ -3,7 +3,8 @@
# vim: filetype=zsh
export DOTFILES="$HOME/.dotfiles"
export PATH="$DOTFILES/local/bin:$PATH"
source "$DOTFILES/scripts/shared.sh"
path_append "$DOTFILES/local/bin"
# Explicitly set XDG folders
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
@@ -23,21 +24,20 @@ export HOMEBREW_SBIN="$HOMEBREW/sbin"
export HOMEBREW_PKG="$HOMEBREW/opt"
export HOMEBREW_NO_ENV_HINTS=1
export PATH="$XDG_BIN_HOME:$HOMEBREW_BIN:$HOMEBREW_SBIN:/usr/local/sbin:$PATH"
path_append "/usr/local/bin"
path_append "$HOMEBREW_SBIN"
path_append "$HOMEBREW_BIN"
path_append "$XDG_BIN_HOME"
# brew, https://brew.sh
if command -v brew &> /dev/null; then
BREW_PYTHON=$(brew --prefix python)/bin
GNUBIN_DIR=$(brew --prefix coreutils)/libexec/gnubin
BREW_RUBY=$(brew --prefix ruby)/bin
BREW_GEMS=$(gem environment gemdir)/bin
export PATH="$BREW_PYTHON:$GNUBIN_DIR:$BREW_GEMS:$BREW_RUBY:$PATH"
fi
have brew && {
path_append "$(brew --prefix python)/bin"
path_append "$(brew --prefix coreutils)/libexec/gnubin"
path_append "$(brew --prefix ruby)/bin"
path_append "$(gem environment gemdir)/bin"
}
source "$DOTFILES/config/exports-shell"
source "$DOTFILES/config/exports-apps"
if command -v nvim &> /dev/null; then
export EDITOR="nvim"
fi
have nvim && export EDITOR="nvim"

View File

@@ -2,6 +2,7 @@
# shellcheck shell=bash
# shellcheck enable=external-sources
# vim: filetype=zsh
source "$DOTFILES/scripts/shared.sh"
# Antigen configuration
# https://github.com/zsh-users/antigen/wiki/Configuration
@@ -11,61 +12,62 @@ export ANTIGEN_PLUGIN_RECEIPT_F=".local/share/antigen/antigen_plugin_lastupdate"
# Ansible configuration
# https://docs.ansible.com/ansible/latest/reference_appendices/config.html
if hash ansible 2> /dev/null; then
have ansible && {
export ANSIBLE_HOME="$XDG_CONFIG_HOME/ansible"
export ANSIBLE_CONFIG="$XDG_CONFIG_HOME/ansible.cfg"
export ANSIBLE_GALAXY_CACHE_DIR="$XDG_CACHE_HOME/ansible/galaxy_cache"
x-dc "$ANSIBLE_HOME"
x-dc "$ANSIBLE_GALAXY_CACHE_DIR"
fi
}
# composer, https://getcomposer.org/
if command -v composer &> /dev/null; then
have composer && {
export COMPOSER_HOME="$XDG_STATE_HOME/composer"
export COMPOSER_BIN="$COMPOSER_HOME/vendor/bin"
export PATH="$COMPOSER_BIN:$PATH"
fi
}
# docker, https://docs.docker.com/engine/reference/commandline/cli/
if command -v docker &> /dev/null; then
have docker && {
export DOCKER_CONFIG="$XDG_CONFIG_HOME/docker"
x-dc "$DOCKER_CONFIG"
fi
}
# ffmpeg
if hash ffmpeg 2> /dev/null; then
have ffmpeg && {
export FFMPEG_DATADIR="$XDG_CONFIG_HOME/ffmpeg"
x-dc "$FFMPEG_DATADIR"
fi
}
# gcloud
if hash gcloud 2> /dev/null; then
have gcloud && {
GCLOUD_LOC=$(gcloud info --format="value(installation.sdk_root)" --quiet)
[[ -f "$GCLOUD_LOC/path.zsh.inc" ]] && builtin source "$GCLOUD_LOC/path.zsh.inc"
[[ -f "$GCLOUD_LOC/completion.zsh.inc" ]] && builtin source "$GCLOUD_LOC/completion.zsh.inc"
fi
}
# gem, rubygems
if command -v gem &> /dev/null; then
have gem && {
export GEM_HOME="$XDG_STATE_HOME/gem"
export GEM_PATH="$XDG_STATE_HOME/gem"
export PATH="$GEM_HOME/bin:$PATH"
fi
path_append "$GEM_PATH/bin"
}
# If we have go packages, include them to the PATH
if command -v go &> /dev/null; then
have go && {
export GOPATH="$XDG_DATA_HOME/go"
export GOBIN="$XDG_BIN_HOME"
x-dc "$GOPATH"
fi
}
# irssi
if hash irssi 2> /dev/null; then
have irssi && {
# These variables are used in ./alias with --config and --home
export IRSSI_CONFIG_HOME="$XDG_CONFIG_HOME/irssi"
export IRSSI_CONFIG_FILE="$IRSSI_CONFIG_HOME/config"
x-dc "$IRSSI_CONFIG_HOME"
fi
}
# nvm, the node version manager
export NVM_LAZY_LOAD=true
@@ -76,21 +78,21 @@ export NVM_DIR="$XDG_CONFIG_HOME/nvm"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# op (1Password cli) is present
if hash op 2> /dev/null; then
have op && {
export OP_CACHE="$XDG_STATE_HOME/1password"
x-dc "$OP_CACHE"
eval "$(op completion zsh)"
compdef _op op
fi
}
# pyenv, python environments
if command -v pyenv &> /dev/null; then
have pyenv && {
export PYENV_ROOT="$XDG_STATE_HOME/pyenv"
x-dc "$PYENV_ROOT"
export PATH="$PYENV_ROOT/bin:$PATH"
path_append "$PYENV_ROOT/bin"
eval "$(pyenv init -)"
fi
}
# wakatime, https://github.com/wakatime/wakatime-cli
export WAKATIME_HOME="$XDG_STATE_HOME/wakatime"
@@ -104,4 +106,3 @@ export _Z_DATA="$XDG_STATE_HOME/z"
export ANDROID_HOME="$XDG_DATA_HOME/android"
export GNUPGHOME="$XDG_DATA_HOME/gnupg"
export SCREENRC="$XDG_CONFIG_HOME/misc/screenrc"

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash
# shellcheck shell=bash
# vim: filetype=zsh
source "$DOTFILES/scripts/shared.sh"
export LC_ALL=fi_FI.utf8
@@ -39,10 +40,7 @@ hash shopt 2> /dev/null && shopt -s checkwinsize
&& source "$XDG_BIN_HOME/iterm2_shell_integration.zsh"
# Set dircolors based on the file, if it exists
if command -v dircolors &> /dev/null; then
# shellcheck disable=SC2046
eval $(dircolors "$XDG_CONFIG_HOME/dircolors")
fi
have dircolors && eval $(dircolors "$XDG_CONFIG_HOME/dircolors")
# If we are using zsh, color our dir lists and such
if [ "$SHELL" = "$(which zsh)" ]; then
@@ -52,4 +50,3 @@ if [ "$SHELL" = "$(which zsh)" ]; then
zstyle ':completion:*' cache-path "$XDG_CACHE_HOME/zsh/zcompcache"
zstyle ':completion:*' list-colors "$LS_COLORS"
fi

View File

@@ -2,6 +2,8 @@
#
# shell functions
#
# shellcheck source="../scripts/shared.sh"
source "$DOTFILES/scripts/shared.sh"
# Weather in Tampere, or other city
function weather
@@ -66,29 +68,6 @@ function silent
"$@" >&/dev/null
}
# Remove directory from the PATH variable
# usage: path_remove ~/.local/bin
function path_remove
{
PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$1\"" | sed 's/:$//')
}
# Append directory to the PATH
# usage: path_append ~/.local/bin
function path_append
{
path_remove "$1"
PATH="${PATH:+"$PATH:"}$1"
}
# Prepend directory to the PATH
# usage: path_prepend ~/.local/bin
function path_prepend
{
path_remove "$1"
PATH="$1${PATH:+":$PATH"}"
}
# Defines default antigen bundles
function x-default-antigen-bundles
{
@@ -104,13 +83,13 @@ function x-default-antigen-bundles
antigen bundle zsh-users/zsh-syntax-highlighting
# these should be available if there's need
hash git 2> /dev/null && antigen bundle git
hash brew 2> /dev/null && antigen bundle brew
hash docker 2> /dev/null && antigen bundle docker
hash docker-compose 2> /dev/null && antigen bundle sroze/docker-compose-zsh-plugin
hash jq 2> /dev/null && antigen bundle reegnz/jq-zsh-plugin
hash nvm 2> /dev/null && antigen bundle nvm
hash php 2> /dev/null && antigen bundle php
hash python 2> /dev/null && antigen bundle MichaelAquilina/zsh-autoswitch-virtualenv
hash rvm 2> /dev/null && antigen bundle unixorn/rvm-plugin
have git && antigen bundle git
have brew && antigen bundle brew
have docker && antigen bundle docker
have docker-compose && antigen bundle sroze/docker-compose-zsh-plugin
have jq && antigen bundle reegnz/jq-zsh-plugin
have nvm && antigen bundle nvm
have php && antigen bundle php
have python && antigen bundle MichaelAquilina/zsh-autoswitch-virtualenv
have rvm && antigen bundle unixorn/rvm-plugin
}

View File

@@ -125,9 +125,7 @@ function section_brew
"autoupdate:Setups brew auto-update and runs it immediately"
)
if ! command -v brew &> /dev/null; then
menu_section "$USAGE_PREFIX" "brew not available on this system"
else
have brew && {
case "$1" in
install)
brew bundle install --file="$BREWFILE" && msg_yay "Done!"
@@ -151,7 +149,7 @@ function section_brew
menu_usage "$USAGE_PREFIX" "${MENU[@]}"
;;
esac
fi
} || menu_section "$USAGE_PREFIX" "brew not available on this system"
}
function section_dotfiles
@@ -188,13 +186,13 @@ function section_dotfiles
ln -s $DOTFILES/config/astronvim ~/.config/astronvim
ln -s $DOTFILES/config/nvim ~/.config/nvim
msg_ok "Linked nvim and astronvim"
hash npm 2> /dev/null && $0 install npm
have npm && $0 install npm
msg_ok "Installed packages"
msg_done "nvim reset!"
;;
yamlfmt)
# format yaml files
yamlfmt -conf "$DOTFILES/.yamlfmt"
have yamlfmt && yamlfmt -conf "$DOTFILES/.yamlfmt" || msg_err "yamlfmt not found"
;;
shfmt)
# Format shell scripts according to following rules.

View File

@@ -1,6 +1,10 @@
#!/bin/bash
#!/usr/bin/env bash
# Install PHP Package Manager Composer
#
# shellcheck source="shared.sh"
source "$HOME/.dotfiles/scripts/shared.sh"
if command -v php &> /dev/null; then
have php && {
EXPECTED_CHECKSUM="$(php -r 'copy("https://composer.github.io/installer.sig", "php://stdout");')"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
@@ -16,4 +20,4 @@ if command -v php &> /dev/null; then
rm composer-setup.php
mv composer.phar ~/.local/bin/composer
exit $RESULT
fi
} || msg_err "PHP Not Available, cannot install composer"

View File

@@ -4,9 +4,7 @@
# shellcheck source="shared.sh"
source "$HOME/.dotfiles/scripts/shared.sh"
if ! command -v gh &> /dev/null; then
msg_run "gh (GitHub Client) could not be found, please install it first"
else
have gh && {
extensions=(
# GitHub CLI extension for generating a report on repository dependencies.
andyfeller/gh-dependency-report
@@ -41,4 +39,4 @@ else
done
msg_ok "Done"
fi
} || msg_err "gh (GitHub Client) could not be found, please install it first"

View File

@@ -4,9 +4,7 @@
# shellcheck source=shared.sh
source "$HOME/.dotfiles/scripts/shared.sh"
if ! command -v go &> /dev/null; then
msg "go hasn't been installed yet."
else
have go && {
packages=(
# sysadmin/scripting utilities, distributed as a single binary
github.com/skx/sysbox@latest
@@ -28,5 +26,4 @@ else
done
msg_ok "Done"
fi
} || msg "go hasn't been installed yet."

View File

@@ -4,9 +4,7 @@
# shellcheck source=shared.sh
source "$HOME/.dotfiles/scripts/shared.sh"
if ! command -v npm &> /dev/null; then
msg_err "npm could not be found."
else
have npm && {
packages=(
# This is a tool to check if your files consider your .editorconfig rules.
"editorconfig-checker"
@@ -35,4 +33,4 @@ else
npm install -g --no-fund --no-progress --no-timing "$pkg"
echo ""
done
fi
} || msg_err "npm could not be found."

View File

@@ -1,8 +1,14 @@
#!/bin/bash
#!/usr/bin/env bash
#
# Install ntfy
#
# shellcheck source=shared.sh
source "$HOME/.dotfiles/scripts/shared.sh"
set -e
if ! command -v ntfy &> /dev/null; then
have ntfy && {
msg "ntfy already installed"
} || {
case $(dfm check arch) in
Linux)
NTFY_ARCH="linux_$(arch)"
@@ -28,6 +34,4 @@ if ! command -v ntfy &> /dev/null; then
fi
rm -rf "${NTFY_DEST}" "${NTFY_DEST}.tar.gz"
else
echo "ntfy already installed"
fi
}

View File

@@ -1,4 +1,9 @@
#!/usr/bin/env bash
#
# Install z
#
# shellcheck source=shared.sh
source "$HOME/.dotfiles/scripts/shared.sh"
Z_GIT_PATH="https://github.com/rupa/z.git"
Z_BIN_PATH="$XDG_BIN_HOME/z"
@@ -6,5 +11,5 @@ Z_BIN_PATH="$XDG_BIN_HOME/z"
if [ ! -d "$Z_BIN_PATH" ]; then
git clone "$Z_GIT_PATH" "$Z_BIN_PATH"
else
echo "z ($Z_BIN_PATH/) already installed"
msg_done "z ($Z_BIN_PATH/) already installed"
fi

View File

@@ -171,3 +171,33 @@ function menu_usage()
menu_item "$CMD" "$DESC"
done
}
# shorthand for checking if the system has the bin in path.
# usage: have php && php -v
function have
{
command -v "$1" >&/dev/null;
}
# Remove directory from the PATH variable
# usage: path_remove ~/.local/bin
function path_remove
{
PATH=$(echo -n "$PATH" | awk -v RS=: -v ORS=: "\$0 != \"$1\"" | sed 's/:$//')
}
# Append directory to the PATH
# usage: path_append ~/.local/bin
function path_append
{
path_remove "$1"
PATH="${PATH:+"$PATH:"}$1"
}
# Prepend directory to the PATH
# usage: path_prepend ~/.local/bin
function path_prepend
{
path_remove "$1"
PATH="$1${PATH:+":$PATH"}"
}