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

Plugins: bail early with descriptive messages #2062

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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: 8 additions & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@ plugins/available/basher.plugin.bash
plugins/available/battery.plugin.bash
plugins/available/blesh.plugin.bash
plugins/available/browser.plugin.bash
plugins/available/cht.sh.plugin.bash
plugins/available/cmd-returned-notify.plugin.bash
plugins/available/colors.plugin.bash
plugins/available/direnv.plugin.bash
plugins/available/dirs.plugin.bash
plugins/available/docker-compose.plugin.bash
plugins/available/docker-machine.plugin.bash
plugins/available/fasd.plugin.bash
plugins/available/fzf.plugin.bash
plugins/available/gif.plugin.bash
plugins/available/git-subrepo.plugin.bash
plugins/available/git.plugin.bash
plugins/available/gitstatus.plugin.bash
plugins/available/go.plugin.bash
plugins/available/goenv.plugin.bash
plugins/available/gradle.plugin.bash
Expand All @@ -112,6 +116,7 @@ plugins/available/history.plugin.bash
plugins/available/hub.plugin.bash
plugins/available/java.plugin.bash
plugins/available/jekyll.plugin.bash
plugins/available/jenv.plugin.bash
plugins/available/jgitflow.plugin.bash
plugins/available/jump.plugin.bash
plugins/available/latex.plugin.bash
Expand All @@ -120,17 +125,20 @@ plugins/available/man.plugin.bash
plugins/available/nginx.plugin.bash
plugins/available/node.plugin.bash
plugins/available/nodenv.plugin.bash
plugins/available/nvm.plugin.bash
plugins/available/osx-timemachine.plugin.bash
plugins/available/osx.plugin.bash
plugins/available/percol.plugin.bash
plugins/available/plenv.plugin.bash
plugins/available/powerline.plugin.bash
plugins/available/projects.plugin.bash
plugins/available/proxy.plugin.bash
plugins/available/pyenv.plugin.bash
plugins/available/python.plugin.bash
plugins/available/rbenv.plugin.bash
plugins/available/ruby.plugin.bash
plugins/available/textmate.plugin.bash
plugins/available/thefuck.plugin.bash
plugins/available/todo.plugin.bash
plugins/available/url.plugin.bash
plugins/available/xterm.plugin.bash
Expand Down
5 changes: 2 additions & 3 deletions plugins/available/autojump.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'Autojump configuration, see https://github.com/wting/autojump for more details'

# Only supports the Homebrew variant, Debian and Arch at the moment.
# Only supports the Homebrew variant, Debian, and Arch at the moment.
# Feel free to provide a PR to support other install locations
# shellcheck disable=SC1090
if _bash_it_homebrew_check && [[ -s "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh" ]]; then
if _bash_it_homebrew_check && [[ -s "${BASH_IT_HOMEBREW_PREFIX?}/etc/profile.d/autojump.sh" ]]; then
source "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh"
elif _command_exists dpkg && dpkg -s autojump &> /dev/null; then
source "$(dpkg-query -S autojump.sh | cut -d' ' -f2)"
Expand Down
4 changes: 2 additions & 2 deletions plugins/available/basher.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'initializes basher, the shell package manager'

# https://github.com/basherpm/basher
Expand All @@ -13,4 +12,5 @@ if ! _command_exists basher; then
fi
fi

eval "$(basher init - bash)"
# shellcheck disable=SC1090
source < <(basher init - bash)
38 changes: 21 additions & 17 deletions plugins/available/cht-sh.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
cite about-plugin
# shellcheck shell=bash
about-plugin 'Simplify `curl cht.sh/<query>` to `cht.sh <query>`'

# Play nicely if user already installed cht.sh cli tool
if ! _command_exists cht.sh ; then
function cht.sh () {
about 'Executes a cht.sh curl query using the provided arguments'
param ' [ ( topic [sub-topic] ) | ~keyword ] [ :list | :help | :learn ]'
example '$ cht.sh :help'
example '$ cht.sh :list'
example '$ cht.sh tar'
example '$ cht.sh js "parse json"'
example '$ cht.sh python :learn'
example '$ cht.sh rust :list'
group 'cht-sh'

# Separate arguments with '/', preserving spaces within them
local query=$(IFS=/ ; echo "$*")
curl "cht.sh/${query}"
}
if _binary_exists cht.sh ; then
_log_warning "You have already installed 'cht.sh', so it's safe to disable this plugin."
return 1
fi

function cht.sh () {
about 'Executes a cht.sh curl query using the provided arguments'
param ' [ ( topic [sub-topic] ) | ~keyword ] [ :list | :help | :learn ]'
example '$ cht.sh :help'
example '$ cht.sh :list'
example '$ cht.sh tar'
example '$ cht.sh js "parse json"'
example '$ cht.sh python :learn'
example '$ cht.sh rust :list'
group 'cht-sh'

# Separate arguments with '/', preserving spaces within them
local query
query=$(IFS=/ ; echo "$*")
curl "cht.sh/${query}"
}
9 changes: 6 additions & 3 deletions plugins/available/direnv.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'load direnv, if you are using it: https://direnv.net/'

if _command_exists direnv; then
eval "$(direnv hook bash)"
if ! _binary_exists direnv; then
_log_warning "Could not find 'direnv'."
return 1
fi

# shellcheck disable=SC1090
source < <(direnv hook bash)
10 changes: 7 additions & 3 deletions plugins/available/fasd.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
cite about-plugin
# shellcheck shell=bash
about-plugin 'load fasd, if you are using it'

_command_exists fasd || return
if ! _binary_exists fasd; then
_log_warning "Unable to locage 'fasd'."
return 1
fi

eval "$(fasd --init auto)"
# shellcheck disable=SC1090
source < <(fasd --init auto)
59 changes: 30 additions & 29 deletions plugins/available/fzf.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
# Load after the system completion to make sure that the fzf completions are working
# BASH_IT_LOAD_PRIORITY: 375

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont you want to keep it?

cite about-plugin
# shellcheck shell=bash
about-plugin 'load fzf, if you are using it'

if [ -r ~/.fzf.bash ] ; then
source ~/.fzf.bash
elif [ -r "${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf.bash ] ; then
source "${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf.bash
# shellcheck source-path=$HOME source-path=$HOME/.config/fzf disable=SC1090 disable=SC1091
if [[ -r ~/.fzf.bash ]]; then
source ~/.fzf.bash
elif [[ -r "${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf.bash" ]]; then
source "${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf.bash"
fi

# No need to continue if the command is not present
_command_exists fzf || return
if ! _binary_exists fzf; then
_log_warning "unable to initialize without '$_' installed."
return 1
fi

if [ -z ${FZF_DEFAULT_COMMAND+x} ] && _command_exists fd ; then
export FZF_DEFAULT_COMMAND='fd --type f'
if [[ -z ${FZF_DEFAULT_COMMAND+x} ]] && _command_exists fd; then
export FZF_DEFAULT_COMMAND='fd --type f'
fi

fe() {
about "Open the selected file in the default editor"
group "fzf"
param "1: Search term"
example "fe foo"
function fe() {
about "Open the selected file in the default editor"
group "fzf"
param "1: Search term"
example "fe foo"

local IFS=$'\n'
local files
files=($(fzf-tmux --query="$1" --multi --select-1 --exit-0))
[[ -n "$files" ]] && ${EDITOR:-vim} "${files[@]}"
local IFS=$'\n'
local files
read -ra files < <(fzf-tmux --query="$1" --multi --select-1 --exit-0)
[[ -n "${files[*]}" ]] && "${EDITOR:-${ALTERNATE_EDITOR:-nano}}" "${files[@]}"
}

fcd() {
about "cd to the selected directory"
group "fzf"
param "1: Directory to browse, or . if omitted"
example "fcd aliases"
function fcd() {
about "cd to the selected directory"
group "fzf"
param "1: Directory to browse, or . if omitted"
example "fcd aliases"

local dir
dir=$(find ${1:-.} -path '*/\.*' -prune \
-o -type d -print 2> /dev/null | fzf +m) &&
cd "$dir"
local dir
dir=$(find "${1:-.}" -path '*/\.*' -prune \
-o -type d -print 2> /dev/null | fzf +m) \
&& cd "$dir" || return
}
46 changes: 28 additions & 18 deletions plugins/available/gitstatus.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
cite about-plugin
# shellcheck shell=bash
about-plugin 'speeds up your life by using gitstatus for git status calculations. install from https://github.com/romkatv/gitstatus'

: "${SCM_GIT_GITSTATUS_DIR:="$HOME/gitstatus"}"
if [[ ! -d "${SCM_GIT_GITSTATUS_DIR}" ]] && _bash_it_homebrew_check; then
SCM_GIT_GITSTATUS_DIR="${BASH_IT_HOMEBREW_PREFIX?}/opt/gitstatus"
fi

function gitstatus_on_disable() {
about 'Destructor of gitstatus plugin'
group 'gitstatus'
about 'Destructor of gitstatus plugin'
group 'gitstatus'

unset SCM_GIT_USE_GITSTATUS
_command_exists gitstatus_stop && gitstatus_stop
unset SCM_GIT_USE_GITSTATUS
_command_exists gitstatus_stop && gitstatus_stop
}

# No scm-check
[[ $SCM_CHECK == "true" ]] || return
function _bash-it-component-plugin-callback-on-init-gitstatus() {
# No scm-check
[[ ${SCM_CHECK?} == "true" ]] || return

# non-interactive shell
[[ $- == *i* ]] || return
# non-interactive shell
[[ $- == *i* ]] || return

: "${SCM_GIT_GITSTATUS_DIR:="$HOME/gitstatus"}"
if [[ -d ${SCM_GIT_GITSTATUS_DIR} ]]; then
source "${SCM_GIT_GITSTATUS_DIR}/gitstatus.plugin.sh"
# Start the actual gitstatus binary
gitstatus_stop && gitstatus_start -s -1 -u -1 -c -1 -d -1
export SCM_GIT_USE_GITSTATUS=true
else
_log_warning "Could not find gitstatus directory in ${SCM_GIT_GITSTATUS_DIR}. Please specify directory location using SCM_GIT_GITSTATUS_DIR."
fi
if [[ -d "${SCM_GIT_GITSTATUS_DIR?}" ]]; then
# shellcheck source-path=$HOME/gitstatus disable=SC1091
source "${SCM_GIT_GITSTATUS_DIR}/gitstatus.plugin.sh"
# Start the actual gitstatus binary
gitstatus_stop && gitstatus_start -s -1 -u -1 -c -1 -d -1
export SCM_GIT_USE_GITSTATUS=true
else
_log_warning "Could not find gitstatus directory in ${SCM_GIT_GITSTATUS_DIR}. Please specify directory location using SCM_GIT_GITSTATUS_DIR."
return 1
fi
}

_bash-it-component-plugin-callback-on-init-gitstatus
29 changes: 14 additions & 15 deletions plugins/available/go.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'go environment variables & path configuration'

# Load after basher and goenv
# BASH_IT_LOAD_PRIORITY: 270

# Test `go version` because goenv creates shim scripts that will be found in PATH
# but do not always resolve to a working install.
{ _command_exists go && go version &> /dev/null; } || return 0
if ! _binary_exists go || ! go version &> /dev/null; then
_log_warning "Unable to locate a working 'go'."
return 1
fi

export GOROOT="${GOROOT:-$(go env GOROOT)}"
export GOPATH="${GOPATH:-$(go env GOPATH)}"
: "${GOROOT:=$(go env GOROOT)}"
: "${GOPATH:=$(go env GOPATH)}"
export GOROOT GOPATH

# $GOPATH/bin is the default location for binaries. Because GOPATH accepts a list of paths and each
# might be managed differently, we add each path's /bin folder to PATH using pathmunge,
# while preserving ordering.
# e.g. GOPATH=foo:bar -> PATH=foo/bin:bar/bin
_bash-it-gopath-pathmunge() {
function _bash-it-component-plugin-callback-on-init-go() {
_about 'Ensures paths in GOPATH are added to PATH using pathmunge, with /bin appended'
_group 'go'
if [[ -z $GOPATH ]]; then
echo 'GOPATH empty' >&2
if [[ -z "${GOPATH:-}" ]]; then
_log_warning 'GOPATH empty'
return 1
fi
local paths i
local paths apath
IFS=: read -r -a paths <<< "$GOPATH"
i=${#paths[@]}
while [[ $i -gt 0 ]]; do
i=$((i - 1))
if [[ -n "${paths[i]}" ]]; then
pathmunge "${paths[i]}/bin"
fi
for apath in "${paths[@]}"; do
pathmunge "${apath}/bin" || true
done
}
_bash-it-gopath-pathmunge
_bash-it-component-plugin-callback-on-init-go
23 changes: 12 additions & 11 deletions plugins/available/goenv.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'load goenv, if you are using it'

# https://github.com/syndbg/goenv
Expand All @@ -11,29 +10,31 @@ about-plugin 'load goenv, if you are using it'
# - Check if in $PATH already
# - Check if installed manually to $GOENV_ROOT
# - Check if installed manually to $HOME
_command_exists goenv \
|| [[ -n "$GOENV_ROOT" && -x "$GOENV_ROOT/bin/goenv" ]] \
|| [[ -x "$HOME/.goenv/bin/goenv" ]] \
|| return 0
if ! _binary_exists goenv || ! [[ -n "${GOENV_ROOT:-}" && -x "$GOENV_ROOT/bin/goenv" ]] || ! [[ -x "$HOME/.goenv/bin/goenv" ]]; then
_log_warning "Unable to locate 'goenv'."
return 1
fi

# Set GOENV_ROOT, if not already set
export GOENV_ROOT="${GOENV_ROOT:-$HOME/.goenv}"
: "${GOENV_ROOT:=$HOME/.goenv}"
export GOENV_ROOT

# Add GOENV_ROOT/bin to PATH, if that's where it's installed
if ! _command_exists goenv && [[ -x "$GOENV_ROOT/bin/goenv" ]]; then
pathmunge "$GOENV_ROOT/bin"
fi

# Initialize goenv
eval "$(goenv init - bash)"
# shellcheck disable=SC1090 # Initialize goenv
source < <(goenv init - bash)

# If moving to a directory with a goenv version set, reload the shell
# to ensure the shell environment matches expectations.
_bash-it-goenv-preexec() {
function _bash-it-goenv-preexec() {
GOENV_OLD_VERSION="$(goenv version-name)"
}
_bash-it-goenv-precmd() {
if [[ -n $GOENV_OLD_VERSION ]] && [[ "$GOENV_OLD_VERSION" != "$(goenv version-name)" ]]; then

function _bash-it-goenv-precmd() {
if [[ -n "${GOENV_OLD_VERSION:-}" ]] && [[ "$GOENV_OLD_VERSION" != "$(goenv version-name)" ]]; then
exec env -u PATH -u GOROOT -u GOPATH -u GOENV_OLD_VERSION "${0/-/}" --login
fi
unset GOENV_OLD_VERSION
Expand Down
8 changes: 5 additions & 3 deletions plugins/available/hub.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# shellcheck shell=bash
cite about-plugin
about-plugin 'load hub, if you are using it'

if _command_exists hub; then
eval "$(hub alias -s)"
if ! _binary_exists hub; then
_log_warning "Unable to locate 'hub'."
return 1
fi

source < <(hub alias -s)
Loading
Loading