Run shfmt on all shell files

This commit is contained in:
Eric Torres 2022-09-30 21:36:24 -07:00
parent 010de1d160
commit 0e47db4015
5 changed files with 142 additions and 142 deletions

View File

@ -8,14 +8,14 @@ trap 'exit 1' SIGINT
LIBDIR="/usr/share/file-scripts/" LIBDIR="/usr/share/file-scripts/"
for f in "$LIBDIR"/*.sh; do for f in "$LIBDIR"/*.sh; do
source "${f}" source "${f}"
done done
DEFAULT_TEMPLATE_DIR="$HOME/Templates" DEFAULT_TEMPLATE_DIR="$HOME/Templates"
# Helper functions # Helper functions
function help() { function help() {
cat << HELPMESSAGE cat <<HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION $(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
Usage: $(basename "$0") [-h] [-d DIR] [-f] dest Usage: $(basename "$0") [-h] [-d DIR] [-f] dest
@ -32,71 +32,71 @@ HELPMESSAGE
} }
while true; do while true; do
case "${1}" in case "${1}" in
'-d'|'--dir') '-d' | '--dir')
DIR="${2}" DIR="${2}"
case "${DIR}" in case "${DIR}" in
"") "")
exit 1 exit 1
;; ;;
-*) -*)
exit 1 exit 1
;; ;;
esac esac
shift 2 shift 2
continue continue
;; ;;
--dir=*) --dir=*)
DIR="${1#*=}" DIR="${1#*=}"
case "${DIR}" in case "${DIR}" in
"") "")
exit 1 exit 1
;; ;;
-*) -*)
exit 1 exit 1
;; ;;
esac esac
shift shift
continue continue
;; ;;
'-f'|'--force') '-f' | '--force')
FORCE_OVERWRITE='--force' FORCE_OVERWRITE='--force'
shift shift
continue continue
;; ;;
'-h'|'--help') '-h' | '--help')
help help
exit exit
;; ;;
--) --)
shift shift
break break
;; ;;
-*) -*)
printf '%s\n' "Unknown option: ${1}" >&2 printf '%s\n' "Unknown option: ${1}" >&2
exit 1 exit 1
;; ;;
*) *)
break break
;; ;;
esac esac
done done
# If directory wasn't overridden # If directory wasn't overridden
if [[ -z "$DIR" ]]; then if [[ -z "$DIR" ]]; then
DIR="$DEFAULT_TEMPLATE_DIR" DIR="$DEFAULT_TEMPLATE_DIR"
fi fi
# If no target specified # If no target specified
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
printf '%s\n' 'Please specify target name' printf '%s\n' 'Please specify target name'
exit 1 exit 1
fi fi
# Check if default template directory exists # Check if default template directory exists
if ! [[ -d "$DIR" ]]; then if ! [[ -d "$DIR" ]]; then
printf '%s\n' "Template directory doesn't exist, exiting." printf '%s\n' "Template directory doesn't exist, exiting."
exit 2 exit 2
fi fi
files="$(find_files "$DIR")" files="$(find_files "$DIR")"
@ -104,10 +104,10 @@ selected_file="$(run_fzf "$files")"
# Check if target exists # Check if target exists
if [[ -f "$1" && -z "$FORCE_OVERWRITE" ]]; then if [[ -f "$1" && -z "$FORCE_OVERWRITE" ]]; then
printf '%s\n' 'File already exists, exiting' printf '%s\n' 'File already exists, exiting'
exit 1 exit 1
elif [[ -f "$1" && -n "$FORCE_OVERWRITE" ]]; then elif [[ -f "$1" && -n "$FORCE_OVERWRITE" ]]; then
cp --verbose --force -- "$selected_file" "$1" cp --verbose --force -- "$selected_file" "$1"
else else
cp --verbose -- "$selected_file" "$1" cp --verbose -- "$selected_file" "$1"
fi fi

View File

@ -8,7 +8,7 @@ trap 'exit 1' SIGINT
LIBDIR="/usr/share/file-scripts/" LIBDIR="/usr/share/file-scripts/"
for f in "$LIBDIR"/*.sh; do for f in "$LIBDIR"/*.sh; do
source "${f}" source "${f}"
done done
BOOT_DIR='/boot' BOOT_DIR='/boot'
@ -16,7 +16,7 @@ ETC_DIR='/etc'
# Helper functions # Helper functions
function help() { function help() {
cat << HELPMESSAGE cat <<HELPMESSAGE
$(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION $(basename "$0") $MAJOR_VERSION.$MINOR_VERSION.$PATCH_VERSION
Usage: $(basename "$0") [-h|--help] [options] [patterns] Usage: $(basename "$0") [-h|--help] [options] [patterns]
@ -34,97 +34,97 @@ HELPMESSAGE
} }
while true; do while true; do
case "${1}" in case "${1}" in
'-b'|'--boot') '-b' | '--boot')
EDIT_BOOT=1 EDIT_BOOT=1
shift shift
continue continue
;; ;;
'-d'|'--dir') '-d' | '--dir')
DIR="${2}" DIR="${2}"
case "${DIR}" in case "${DIR}" in
"") "")
exit 1 exit 1
;; ;;
-*) -*)
exit 1 exit 1
;; ;;
esac esac
shift 2 shift 2
continue continue
;; ;;
--dir=*) --dir=*)
DIR="${1#*=}" DIR="${1#*=}"
case "${DIR}" in case "${DIR}" in
"") "")
exit 1 exit 1
;; ;;
-*) -*)
exit 1 exit 1
;; ;;
esac esac
shift shift
continue continue
;; ;;
'-E'|'--etc') '-E' | '--etc')
EDIT_ETC=1 EDIT_ETC=1
shift shift
continue continue
;; ;;
'-i'|'--no-ignore-vcs') '-i' | '--no-ignore-vcs')
NO_IGNORE_VCS=1 NO_IGNORE_VCS=1
shift shift
continue continue
;; ;;
'-I'|'--no-ignore') '-I' | '--no-ignore')
NO_IGNORE=1 NO_IGNORE=1
shift shift
continue continue
;; ;;
'-h'|'--help') '-h' | '--help')
help help
exit exit
;; ;;
--) --)
shift shift
break break
;; ;;
-*) -*)
printf '%s\n' "Unknown option: ${1}" >&2 printf '%s\n' "Unknown option: ${1}" >&2
exit 1 exit 1
;; ;;
*) *)
break break
;; ;;
esac esac
done done
# Handle -b and -E, they are mutually exclusive # Handle -b and -E, they are mutually exclusive
if [[ -n $EDIT_BOOT && -z $EDIT_ETC ]]; then if [[ -n $EDIT_BOOT && -z $EDIT_ETC ]]; then
DIR="$BOOT_DIR" DIR="$BOOT_DIR"
elif [[ -z $EDIT_BOOT && -n $EDIT_ETC ]]; then elif [[ -z $EDIT_BOOT && -n $EDIT_ETC ]]; then
DIR="$ETC_DIR" DIR="$ETC_DIR"
elif [[ -n $EDIT_BOOT && -n $EDIT_ETC ]]; then elif [[ -n $EDIT_BOOT && -n $EDIT_ETC ]]; then
printf '%s\n' 'Select either --boot or --etc, not both' printf '%s\n' 'Select either --boot or --etc, not both'
exit 1 exit 1
elif [[ -z $DIR ]]; then elif [[ -z $DIR ]]; then
DIR='.' DIR='.'
fi fi
# Handle extra options # Handle extra options
declare -a extra_opts declare -a extra_opts
if [[ -n $NO_IGNORE ]]; then if [[ -n $NO_IGNORE ]]; then
extra_opts+=('--no-ignore') extra_opts+=('--no-ignore')
elif [[ -n $NO_IGNORE_VCS ]]; then elif [[ -n $NO_IGNORE_VCS ]]; then
extra_opts+=('--no-ignore-vcs') extra_opts+=('--no-ignore-vcs')
fi fi
files="$(find_files $DIR "${extra_opts[@]}")" files="$(find_files $DIR "${extra_opts[@]}")"
selected_file="$(run_fzf "$files")" selected_file="$(run_fzf "$files")"
if [[ -w "${selected_file}" ]]; then if [[ -w "${selected_file}" ]]; then
"$EDITOR" "$selected_file" "$EDITOR" "$selected_file"
else else
sudo --edit "$selected_file" sudo --edit "$selected_file"
fi fi

View File

@ -3,5 +3,5 @@
FZF_OPTS=('--select-1' '--exit-0') FZF_OPTS=('--select-1' '--exit-0')
run_fzf() { run_fzf() {
fzf "${FZF_OPTS[@]}" -- <<< "$@" || return 2 fzf "${FZF_OPTS[@]}" -- <<<"$@" || return 2
} }

View File

@ -6,11 +6,11 @@ DEFAULT_FD_OPTS=('--hidden' '--type' 'f' '--type' 'l' '--threads' "$(nproc)")
# $1: directory # $1: directory
# $2-n: extra arguments # $2-n: extra arguments
find_files() { find_files() {
if [[ -d "$1" ]]; then if [[ -d "$1" ]]; then
local directory="$1" local directory="$1"
shift shift
fd "${DEFAULT_FD_OPTS[@]}" "$@" -- . "$directory" fd "${DEFAULT_FD_OPTS[@]}" "$@" -- . "$directory"
else else
fd "${DEFAULT_FD_OPTS[@]}" "$@" fd "${DEFAULT_FD_OPTS[@]}" "$@"
fi fi
} }

View File

@ -5,5 +5,5 @@ MINOR_VERSION=1
PATCH_VERSION=4 PATCH_VERSION=4
if [[ "$1" == '--print' ]]; then if [[ "$1" == '--print' ]]; then
echo "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}" echo "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}"
fi fi