Lint: prepare `lib/utilities` for `shellcheck` (#1933)
* lib/utilities: shellcheck
SC2059
* lib/utilities: fix `_bash-it-get-component-type-from-path()`
Account for plugins with names that contain periods.
* lib/utilities: fix `_bash-it-array-dedup()`
Use fewer subprocesses and newline-delimited not space-delimited.
* lib/utilities: fix `_bash-it-component-list()`
Use fewer subprocesses and return newline-delimited, not space-delimited.
* lib/utilities: fix `_bash-it-component-list-matching()`
Use `sort -u` instead of `sort | uniq`
* lib/utilities: fix `_bash-it-component-list-enabled()`
Use fewer subprocesses, return newline-delimited instead of space-delimited, and use `sort -u` instead of `uniq | sort`
* lib/utilities: fix `_bash-it-component-list-disabled()`
Use fewer subprocesses, return newline-delimited instead of space-delimited, and use `sort -u` instead of `uniq | sort`
* lib/utilities: fix `_bash-it-grep()`
1. Executing `'/usr/bin/grep'` does *not* return the path to grep...
2. use `type -p` instead of external binary `which`.
3. Simplify parameter definition.
4. Why was there a space after `%s`?
* lib/utilities: use `_bash-it-grep`
Alsö, lose a spurious `cat`
* lib/utilities: lint `_bash-it-component-help`
* lib/utilities: lint `_bash-it-component-cache-file()`
* lib/utilities: `shfmt`
My apologies to future `git blame` hunters ♥
* lib/helpers: fix `_bash-it-get-component-name-from-path()`
Use `${BASH_IT_LOAD_PRIORITY_SEPARATOR}`
pull/1885/head
parent
8a1dc96bc0
commit
3eed0f033f
|
|
@ -75,6 +75,9 @@ completion/available/vault.completion.bash
|
||||||
completion/available/vuejs.completion.bash
|
completion/available/vuejs.completion.bash
|
||||||
completion/available/wpscan.completion.bash
|
completion/available/wpscan.completion.bash
|
||||||
|
|
||||||
|
# libraries
|
||||||
|
lib/utilities.bash
|
||||||
|
|
||||||
# plugins
|
# plugins
|
||||||
#
|
#
|
||||||
plugins/available/alias-completion.plugin.bash
|
plugins/available/alias-completion.plugin.bash
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env bash
|
# shellcheck shell=bash
|
||||||
#
|
#
|
||||||
# A collection of reusable functions.
|
# A collection of reusable functions.
|
||||||
|
|
||||||
|
|
@ -6,22 +6,25 @@
|
||||||
# Generic utilies
|
# Generic utilies
|
||||||
###########################################################################
|
###########################################################################
|
||||||
|
|
||||||
_bash-it-get-component-name-from-path() {
|
function _bash-it-get-component-name-from-path() {
|
||||||
|
local filename
|
||||||
# filename without path
|
# filename without path
|
||||||
filename=${1##*/}
|
filename="${1##*/}"
|
||||||
# filename without path or priority
|
# filename without path or priority
|
||||||
filename=${filename##*---}
|
filename="${filename##*${BASH_IT_LOAD_PRIORITY_SEPARATOR?}}"
|
||||||
# filename without path, priority or extension
|
# filename without path, priority or extension
|
||||||
echo ${filename%.*.bash}
|
echo "${filename%.*.bash}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_bash-it-get-component-type-from-path() {
|
function _bash-it-get-component-type-from-path() {
|
||||||
|
local filename
|
||||||
# filename without path
|
# filename without path
|
||||||
filename=${1##*/}
|
filename="${1##*/}"
|
||||||
# filename without path or priority
|
# filename without extension
|
||||||
filename=${filename##*---}
|
filename="${filename%.bash}"
|
||||||
# extension
|
# extension without priority or name
|
||||||
echo ${filename} | cut -d '.' -f 2
|
filename="${filename##*.}"
|
||||||
|
echo "${filename}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# This function searches an array for an exact match against the term passed
|
# This function searches an array for an exact match against the term passed
|
||||||
|
|
@ -43,7 +46,7 @@ _bash-it-get-component-type-from-path() {
|
||||||
# contains pear!
|
# contains pear!
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
_bash-it-array-contains-element() {
|
function _bash-it-array-contains-element() {
|
||||||
local e
|
local e
|
||||||
for e in "${@:2}"; do
|
for e in "${@:2}"; do
|
||||||
[[ "$e" == "$1" ]] && return 0
|
[[ "$e" == "$1" ]] && return 0
|
||||||
|
|
@ -52,87 +55,88 @@ _bash-it-array-contains-element() {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Dedupe a simple array of words without spaces.
|
# Dedupe a simple array of words without spaces.
|
||||||
_bash-it-array-dedup() {
|
function _bash-it-array-dedup() {
|
||||||
echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' '
|
local IFS=$'\n'
|
||||||
|
echo "$@" | tr ' ' '\n' | sort -u
|
||||||
}
|
}
|
||||||
|
|
||||||
# Outputs a full path of the grep found on the filesystem
|
# Outputs a full path of the grep found on the filesystem
|
||||||
_bash-it-grep() {
|
function _bash-it-grep() {
|
||||||
if [[ -z "${BASH_IT_GREP:-}" ]] ; then
|
: "${BASH_IT_GREP:=$(type -p egrep || type -p grep)}"
|
||||||
export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')"
|
printf "%s" "${BASH_IT_GREP:-'/usr/bin/grep'}"
|
||||||
fi
|
|
||||||
printf "%s " "${BASH_IT_GREP}"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# Component-specific functions (component is either an alias, a plugin, or a
|
# Component-specific functions (component is either an alias, a plugin, or a
|
||||||
# completion).
|
# completion).
|
||||||
###########################################################################
|
###########################################################################
|
||||||
|
|
||||||
_bash-it-component-help() {
|
function _bash-it-component-help() {
|
||||||
local component="$(_bash-it-pluralize-component "${1}")"
|
local component file func
|
||||||
local file="$(_bash-it-component-cache-file "${component}")"
|
component="$(_bash-it-pluralize-component "${1}")"
|
||||||
if [[ ! -s "${file}" || -z $(find "${file}" -mmin -300) ]] ; then
|
file="$(_bash-it-component-cache-file "${component}")"
|
||||||
rm -f "${file}" 2>/dev/null
|
if [[ ! -s "${file}" || -z "$(find "${file}" -mmin -300)" ]]; then
|
||||||
local func="_bash-it-${component}"
|
rm -f "${file}" 2> /dev/null
|
||||||
"${func}" | $(_bash-it-grep) -E ' \[' | cat > "${file}"
|
func="_bash-it-${component}"
|
||||||
|
"${func}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E ' \[' > "${file}"
|
||||||
fi
|
fi
|
||||||
cat "${file}"
|
cat "${file}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_bash-it-component-cache-file() {
|
function _bash-it-component-cache-file() {
|
||||||
local component=$(_bash-it-pluralize-component "${1}")
|
local component file
|
||||||
local file="${BASH_IT}/tmp/cache/${component}"
|
component="$(_bash-it-pluralize-component "${1}")"
|
||||||
|
file="${BASH_IT?}/tmp/cache/${component}"
|
||||||
[[ -f "${file}" ]] || mkdir -p "${file%/*}"
|
[[ -f "${file}" ]] || mkdir -p "${file%/*}"
|
||||||
printf "${file}"
|
printf '%s' "${file}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_bash-it-pluralize-component() {
|
function _bash-it-pluralize-component() {
|
||||||
local component="${1}"
|
local component="${1}"
|
||||||
local len=$(( ${#component} - 1 ))
|
local -i len=$((${#component} - 1))
|
||||||
# pluralize component name for consistency
|
# pluralize component name for consistency
|
||||||
[[ ${component:${len}:1} != 's' ]] && component="${component}s"
|
[[ "${component:${len}:1}" != 's' ]] && component="${component}s"
|
||||||
[[ ${component} == "alias" ]] && component="aliases"
|
[[ "${component}" == "alias" ]] && component="aliases"
|
||||||
printf ${component}
|
printf '%s' "${component}"
|
||||||
}
|
}
|
||||||
|
|
||||||
_bash-it-clean-component-cache() {
|
function _bash-it-clean-component-cache() {
|
||||||
local component="$1"
|
local component="$1"
|
||||||
local cache
|
local cache
|
||||||
local -a BASH_IT_COMPONENTS=(aliases plugins completions)
|
local -a BASH_IT_COMPONENTS=(aliases plugins completions)
|
||||||
if [[ -z ${component} ]] ; then
|
if [[ -z "${component}" ]]; then
|
||||||
for component in "${BASH_IT_COMPONENTS[@]}" ; do
|
for component in "${BASH_IT_COMPONENTS[@]}"; do
|
||||||
_bash-it-clean-component-cache "${component}"
|
_bash-it-clean-component-cache "${component}"
|
||||||
done
|
done
|
||||||
else
|
else
|
||||||
cache="$(_bash-it-component-cache-file ${component})"
|
cache="$(_bash-it-component-cache-file "${component}")"
|
||||||
if [[ -f "${cache}" ]] ; then
|
if [[ -f "${cache}" ]]; then
|
||||||
rm -f "${cache}"
|
rm -f "${cache}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Returns an array of items within each compoenent.
|
# Returns an array of items within each compoenent.
|
||||||
_bash-it-component-list() {
|
function _bash-it-component-list() {
|
||||||
local component="$1"
|
local IFS=$'\n' component="$1"
|
||||||
_bash-it-component-help "${component}" | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
_bash-it-component-help "${component}" | awk '{print $1}' | sort -u
|
||||||
}
|
}
|
||||||
|
|
||||||
_bash-it-component-list-matching() {
|
function _bash-it-component-list-matching() {
|
||||||
local component="$1"; shift
|
local component="$1"
|
||||||
|
shift
|
||||||
local term="$1"
|
local term="$1"
|
||||||
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -- "${term}" | awk '{print $1}' | sort | uniq
|
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -- "${term}" | awk '{print $1}' | sort -u
|
||||||
}
|
}
|
||||||
|
|
||||||
_bash-it-component-list-enabled() {
|
function _bash-it-component-list-enabled() {
|
||||||
local component="$1"
|
local IFS=$'\n' component="$1"
|
||||||
_bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E '\[x\]' | awk '{print $1}' | sort -u
|
||||||
}
|
}
|
||||||
|
|
||||||
_bash-it-component-list-disabled() {
|
function _bash-it-component-list-disabled() {
|
||||||
local component="$1"
|
local IFS=$'\n' component="$1"
|
||||||
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -v '\[x\]' | awk '{print $1}' | sort -u
|
||||||
}
|
}
|
||||||
|
|
||||||
# Checks if a given item is enabled for a particular component/file-type.
|
# Checks if a given item is enabled for a particular component/file-type.
|
||||||
|
|
@ -143,10 +147,10 @@ _bash-it-component-list-disabled() {
|
||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# _bash-it-component-item-is-enabled alias git && echo "git alias is enabled"
|
# _bash-it-component-item-is-enabled alias git && echo "git alias is enabled"
|
||||||
_bash-it-component-item-is-enabled() {
|
function _bash-it-component-item-is-enabled() {
|
||||||
local component="$1"
|
local component="$1"
|
||||||
local item="$2"
|
local item="$2"
|
||||||
_bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s"
|
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E '\[x\]' | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -q -- "^${item}\s"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Checks if a given item is disabled for a particular component/file-type.
|
# Checks if a given item is disabled for a particular component/file-type.
|
||||||
|
|
@ -157,8 +161,8 @@ _bash-it-component-item-is-enabled() {
|
||||||
#
|
#
|
||||||
# Examples:
|
# Examples:
|
||||||
# _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled"
|
# _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled"
|
||||||
_bash-it-component-item-is-disabled() {
|
function _bash-it-component-item-is-disabled() {
|
||||||
local component="$1"
|
local component="$1"
|
||||||
local item="$2"
|
local item="$2"
|
||||||
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s"
|
_bash-it-component-help "${component}" | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -v '\[x\]' | ${BASH_IT_GREP:-$(_bash-it-grep)} -E -q -- "^${item}\s"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue