plugin/base: improvements (#1930)

* plugins/base: code style improvements

Quote variables, use $@ and $array[@] instead of $*, typeset some integers, remove unneccesasary binary invocation, use shell features when possible, remove `eval`, &c.

* plugins/base: conditional function definitions

Instead of functions failing when required tools aren't installed, just don't define the function.
Alsö, don't redefine del() if it already exists.

* plugins/base: rewrite `usage()`

Reimplement disk usage function using Bash syntax and simpler layout, without having to invoke an external binary.

* plugins/base: revamp `quiet()`

New implementation that is even quieter.

* plugins/base: `myip()`

* plugins/base: `pickfrom()`

* plugins/base: `passgen()`

Fix `passgen()` to not need `tr`, remove one subshell, and eliminate a useless `echo`.

* plugins/base: `mkcd()`

* plugins/base: `mkiso()`

* plugins/base: remove `banish-cookies()`

Adobe Flash is gone with the wind. Alsö, this would be something someone would do *once* and shouldn't be a function...

* plugins/base: `lsgrep` is SC2010

The `lsgrep()` function is *itself* explicitly forbidden by `shellcheck` rule SC2010.

Alsö, s/`$*`/`$@`

* plugins/base: `mkiso()`

Expressly handle unbound parameters.

* plugins/base: remove `command_exists`

* plugin/base: lint SC2154 && SC2144

Newly undisabled `shellcheck` rules

* plugin/base: import libs for tests

* plugin/base: `shfmt`

Apply `shfmt` using current project settings. My apologies to future `git blame` hunters. ♥
pull/1885/head
John D Pell 2021-09-28 05:13:27 -07:00 committed by GitHub
parent 127cbbd4e3
commit c2c76a380a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 161 additions and 191 deletions

View File

@ -79,6 +79,7 @@ completion/available/wpscan.completion.bash
# #
plugins/available/alias-completion.plugin.bash plugins/available/alias-completion.plugin.bash
plugins/available/autojump.plugin.bash plugins/available/autojump.plugin.bash
plugins/available/base.plugin.bash
plugins/available/basher.plugin.bash plugins/available/basher.plugin.bash
plugins/available/cmd-returned-notify.plugin.bash plugins/available/cmd-returned-notify.plugin.bash
plugins/available/direnv.plugin.bash plugins/available/direnv.plugin.bash

View File

@ -1,216 +1,180 @@
# shellcheck shell=bash
cite about-plugin cite about-plugin
about-plugin 'miscellaneous tools' about-plugin 'miscellaneous tools'
function ips () function ips() {
{ about 'display all ip addresses for this host'
about 'display all ip addresses for this host' group 'base'
group 'base' if _command_exists ifconfig; then
if _command_exists ifconfig ifconfig | awk '/inet /{ gsub(/addr:/, ""); print $2 }'
then elif _command_exists ip; then
ifconfig | awk '/inet /{ gsub(/addr:/, ""); print $2 }' ip addr | grep -oP 'inet \K[\d.]+'
elif _command_exists ip else
then echo "You don't have ifconfig or ip command installed!"
ip addr | grep -oP 'inet \K[\d.]+' fi
else
echo "You don't have ifconfig or ip command installed!"
fi
} }
function down4me () function down4me() {
{ about 'checks whether a website is down for you, or everybody'
about 'checks whether a website is down for you, or everybody' param '1: website url'
param '1: website url' example '$ down4me http://www.google.com'
example '$ down4me http://www.google.com' group 'base'
group 'base' curl -Ls "http://downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g'
curl -Ls "http://downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g'
} }
function myip () function myip() {
{ about 'displays your ip address, as seen by the Internet'
about 'displays your ip address, as seen by the Internet' group 'base'
group 'base' list=("http://myip.dnsomatic.com/" "http://checkip.dyndns.com/" "http://checkip.dyndns.org/")
list=("http://myip.dnsomatic.com/" "http://checkip.dyndns.com/" "http://checkip.dyndns.org/") for url in "${list[@]}"; do
for url in ${list[*]} if res="$(curl -fs "${url}")"; then
do break
res=$(curl -fs "${url}") fi
if [[ $? -eq 0 ]];then done
break; res="$(echo "$res" | grep -Eo '[0-9\.]+')"
fi echo -e "Your public IP is: ${echo_bold_green-} $res ${echo_normal-}"
done
res=$(echo "$res" | grep -Eo '[0-9\.]+')
echo -e "Your public IP is: ${echo_bold_green} $res ${echo_normal}"
} }
function pickfrom () function pickfrom() {
{ about 'picks random line from file'
about 'picks random line from file' param '1: filename'
param '1: filename' example '$ pickfrom /usr/share/dict/words'
example '$ pickfrom /usr/share/dict/words' group 'base'
group 'base' local file=${1:-}
local file=$1 local -i n=0 length
[[ -z "$file" ]] && reference $FUNCNAME && return if [[ ! -r "$file" ]]; then
length=$(cat $file | wc -l) reference "${FUNCNAME[0]}" && return
n=$(expr $RANDOM \* $length \/ 32768 + 1) fi
head -n $n $file | tail -1 length="$(wc -l < "$file")"
n=$((RANDOM * length / 32768 + 1))
head -n "$n" "$file" | tail -1
} }
function passgen () function passgen() {
{ about 'generates random password from dictionary words'
about 'generates random password from dictionary words' param 'optional integer length'
param 'optional integer length' param 'if unset, defaults to 4'
param 'if unset, defaults to 4' example '$ passgen'
example '$ passgen' example '$ passgen 6'
example '$ passgen 6' group 'base'
group 'base' local -i i length=${1:-4}
local i pass length=${1:-4} local pass
pass=$(echo $(for i in $(eval echo "{1..$length}"); do pickfrom /usr/share/dict/words; done)) # shellcheck disable=SC2034
echo "With spaces (easier to memorize): $pass" pass="$(for i in $(eval "echo {1..$length}"); do pickfrom /usr/share/dict/words; done)"
echo "Without (use this as the password): $(echo $pass | tr -d ' ')" echo "With spaces (easier to memorize): ${pass//$'\n'/ }"
echo "Without spaces (easier to brute force): ${pass//$'\n'/}"
} }
# Create alias pass to passgen when pass isn't installed or # Create alias pass to passgen when pass isn't installed or
# BASH_IT_LEGACY_PASS is true. # BASH_IT_LEGACY_PASS is true.
if ! _command_exists pass || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]] if ! _command_exists pass || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]]; then
then alias pass=passgen
alias pass=passgen
fi fi
function pmdown () if _command_exists markdown && _command_exists browser; then
{ function pmdown() {
about 'preview markdown file in a browser' about 'preview markdown file in a browser'
param '1: markdown file' param '1: markdown file'
example '$ pmdown README.md' example '$ pmdown README.md'
group 'base' group 'base'
if _command_exists markdown
then
markdown $1 | browser
else
echo "You don't have a markdown command installed!"
fi
}
function mkcd () markdown "${1?}" | browser
{ }
about 'make one or more directories and cd into the last one'
param 'one or more directories to create'
example '$ mkcd foo'
example '$ mkcd /tmp/img/photos/large'
example '$ mkcd foo foo1 foo2 fooN'
example '$ mkcd /tmp/img/photos/large /tmp/img/photos/self /tmp/img/photos/Beijing'
group 'base'
mkdir -p -- "$@" && eval cd -- "\"\$$#\""
}
function lsgrep ()
{
about 'search through directory contents with grep'
group 'base'
ls | grep "$*"
}
function quiet ()
{
about 'what *does* this do?'
group 'base'
$* &> /dev/null &
}
function banish-cookies ()
{
about 'redirect .adobe and .macromedia files to /dev/null'
group 'base'
rm -r ~/.macromedia ~/.adobe
ln -s /dev/null ~/.adobe
ln -s /dev/null ~/.macromedia
}
function usage ()
{
about 'disk usage per directory, in Mac OS X and Linux'
param '1: directory name'
group 'base'
if [[ "$OSTYPE" == 'darwin'* ]]; then
if [ -n "$1" ]; then
du -hd 1 "$1"
else
du -hd 1
fi
elif [[ "$OSTYPE" = 'linux'* ]]; then
if [[ -n "$1" ]]; then
du -h --max-depth=1 "$1"
else
du -h --max-depth=1
fi
fi
}
if [[ ! -e "${BASH_IT}/plugins/enabled/todo.plugin.bash" ]] && [[ ! -e "${BASH_IT}/plugins/enabled/*${BASH_IT_LOAD_PRIORITY_SEPARATOR}todo.plugin.bash" ]]
then
# if user has installed todo plugin, skip this...
function t ()
{
about 'one thing todo'
param 'if not set, display todo item'
param '1: todo text'
if [[ "$*" == "" ]] ; then
cat ~/.t
else
echo "$*" > ~/.t
fi
}
fi fi
function command_exists () function mkcd() {
{ about 'make one or more directories and cd into the last one'
about 'checks for existence of a command' param 'one or more directories to create'
param '1: command to check' example '$ mkcd foo'
example '$ command_exists ls && echo exists' example '$ mkcd /tmp/img/photos/large'
group 'base' example '$ mkcd foo foo1 foo2 fooN'
type "$1" &> /dev/null ; example '$ mkcd /tmp/img/photos/large /tmp/img/photos/self /tmp/img/photos/Beijing'
group 'base'
mkdir -p -- "$@" && cd -- "${!#}" || return
} }
mkiso () # shellcheck disable=SC2010
{ function lsgrep() {
about 'creates iso from current dir in the parent dir (unless defined)' about 'search through directory contents with grep'
param '1: ISO name' group 'base'
param '2: dest/path' ls | grep "$@"
param '3: src/path'
example 'mkiso'
example 'mkiso ISO-Name dest/path src/path'
group 'base'
if type "mkisofs" > /dev/null; then
[[ -z ${1+x} ]] && local isoname=${PWD##*/} || local isoname=$1
[[ -z ${2+x} ]] && local destpath=../ || local destpath=$2
[[ -z ${3+x} ]] && local srcpath=${PWD} || local srcpath=$3
if [[ ! -f "${destpath}${isoname}.iso" ]]; then
echo "writing ${isoname}.iso to ${destpath} from ${srcpath}"
mkisofs -V ${isoname} -iso-level 3 -r -o "${destpath}${isoname}.iso" "${srcpath}"
else
echo "${destpath}${isoname}.iso already exists"
fi
else
echo "mkisofs cmd does not exist, please install cdrtools"
fi
} }
function quiet() {
about 'what *does* this do?'
group 'base'
nohup "$@" &> /dev/null < /dev/null &
}
function usage() {
about 'disk usage per directory, in Mac OS X and Linux'
param '1: directory name'
group 'base'
case $OSTYPE in
*'darwin'*)
du -hd 1 "$@"
;;
*'linux'*)
du -h --max-depth=1 "$@"
;;
esac
}
# shellcheck disable=SC2144 # the glob matches only one file
if [[ ! -e "${BASH_IT?}/plugins/enabled/todo.plugin.bash" &&
! -e "${BASH_IT?}/plugins/enabled"/*"${BASH_IT_LOAD_PRIORITY_SEPARATOR-}todo.plugin.bash" ]]; then
# if user has installed todo plugin, skip this...
function t() {
about 'one thing todo'
param 'if not set, display todo item'
param '1: todo text'
if [[ "$*" == "" ]]; then
cat ~/.t
else
echo "$*" > ~/.t
fi
}
fi
if _command_exists mkisofs; then
function mkiso() {
about 'creates iso from current dir in the parent dir (unless defined)'
param '1: ISO name'
param '2: dest/path'
param '3: src/path'
example 'mkiso'
example 'mkiso ISO-Name dest/path src/path'
group 'base'
local isoname="${1:-${PWD##*/}}"
local destpath="${2:-../}"
local srcpath="${3:-${PWD}}"
if [[ ! -f "${destpath%/}/${isoname}.iso" ]]; then
echo "writing ${isoname}.iso to ${destpath} from ${srcpath}"
mkisofs -V "${isoname}" -iso-level 3 -r -o "${destpath%/}/${isoname}.iso" "${srcpath}"
else
echo "${destpath%/}/${isoname}.iso already exists"
fi
}
fi
# useful for administrators and configs # useful for administrators and configs
function buf () function buf() {
{ about 'back up file with timestamp'
about 'back up file with timestamp' param 'filename'
param 'filename' group 'base'
group 'base' local filename="${1?}" filetime
local filename=$1 filetime=$(date +%Y%m%d_%H%M%S)
local filetime=$(date +%Y%m%d_%H%M%S) cp -a "${filename}" "${filename}_${filetime}"
cp -a "${filename}" "${filename}_${filetime}"
} }
function del() { if ! _command_exists del; then
about 'move files to hidden folder in tmp, that gets cleared on each reboot' function del() {
param 'file or folder to be deleted' about 'move files to hidden folder in tmp, that gets cleared on each reboot'
example 'del ./file.txt' param 'file or folder to be deleted'
group 'base' example 'del ./file.txt'
mkdir -p /tmp/.trash && mv "$@" /tmp/.trash; group 'base'
} mkdir -p /tmp/.trash && mv "$@" /tmp/.trash
}
fi

View File

@ -5,11 +5,11 @@ load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load ../../lib/log load ../../lib/log
load ../../lib/utilities load ../../lib/utilities
load ../../lib/search load ../../lib/search
load ../../plugins/available/base.plugin
cite _about _param _example _group _author _version cite _about _param _example _group _author _version
load ../../lib/helpers load ../../lib/helpers
load ../../plugins/available/base.plugin
function local_setup { function local_setup {
setup_test_fixture setup_test_fixture

View File

@ -3,10 +3,11 @@
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load ../../lib/appearance load ../../lib/appearance
load ../../plugins/available/base.plugin
cite _about _param _example _group _author _version cite _about _param _example _group _author _version
load ../../lib/log load ../../lib/log
load ../../lib/helpers
load ../../plugins/available/base.plugin
@test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_ALL" { @test "lib log: basic debug logging with BASH_IT_LOG_LEVEL_ALL" {
BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL BASH_IT_LOG_LEVEL=$BASH_IT_LOG_LEVEL_ALL

View File

@ -2,6 +2,8 @@
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load ../../lib/log
load ../../lib/helpers
load ../../plugins/available/base.plugin load ../../plugins/available/base.plugin
@test 'plugins base: ips()' { @test 'plugins base: ips()' {

2
test/plugins/battery.plugin.bats 100644 → 100755
View File

@ -2,6 +2,8 @@
load ../test_helper load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load "${BASH_IT}/lib/log.bash"
load "${BASH_IT}/lib/helpers.bash"
cite _about _param _example _group _author _version cite _about _param _example _group _author _version