Refactor variables names

This commit is contained in:
2022-05-25 18:22:35 +02:00
parent 8bd247759b
commit 373cc8e8b6
3 changed files with 46 additions and 45 deletions

View File

@@ -1,66 +1,66 @@
#!/bin/bash #!/bin/bash
# shellcheck disable=SC1090 # dynamically sourced file # shellcheck disable=SC1090 # dynamically sourced file
SEQGITURL="https://winklerfamilie.eu/git/efelon/shell_sequencer.git" sequencer_gitUrl="https://winklerfamilie.eu/git/efelon/shell_sequencer.git"
DEFAULT_DIR="/opt/sequencer" defaultDir="/opt/sequencer"
DEFAULT_USER_SEQS="/opt/seqs" defaultUserSeqs="/opt/seqs"
SEQUENCER_DIR= sequencerDir=
# Get script working directory # Get script working directory
WDIR="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >>/dev/null 2>&1 && pwd)" WDIR="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >>/dev/null 2>&1 && pwd)"
SEQUENCER_DIR="$1" sequencerDir="${1:-}"
# Installation directory was not set by argument -d # Installation directory was not set by argument -d
if [ -z "$SEQUENCER_DIR" ]; then if [ -z "${sequencerDir}" ]; then
[ -w "$(dirname "$DEFAULT_DIR")" ] && SEQUENCER_DIR="$DEFAULT_DIR" [ -w "$(dirname -- "${defaultDir}")" ] && sequencerDir="${defaultDir}"
# Fallback to working directory # Fallback to working directory
[ -z "$SEQUENCER_DIR" ] && SEQUENCER_DIR="${WDIR}/sequencer" [ -z "${sequencerDir}" ] && sequencerDir="${WDIR}/sequencer"
fi fi
# Check if already installed # Check if already installed
if [ -d "$SEQUENCER_DIR" ]; then if [ -d "${sequencerDir}" ]; then
echo " [E] Sequencer seems to be already installed at:" echo " [e] Sequencer seems to be already installed at:"
echo " $SEQUENCER_DIR" echo " ${sequencerDir}"
exit 1 exit 1
fi fi
if [ ! -w "$(dirname "$SEQUENCER_DIR")" ]; then if [ ! -w "$(dirname -- "${sequencerDir}")" ]; then
echo " [E] Your user has no permission to write to $(dirname "$SEQUENCER_DIR")" echo " [e] Your user has no permission to write to $(dirname -- "${sequencerDir}")"
exit 2 exit 2
fi fi
# Install git if neccessary # Install git if neccessary
if ! which git >>/dev/null 2>&1; then if ! which git >>/dev/null 2>&1; then
echo " [W] Git not found and will be installed" echo " [w] Git not found and will be installed"
if ! apt update; then if ! apt update; then
echo " [W] Cannot update apt repositories" echo " [w] Cannot update apt repositories"
fi fi
if ! apt install git -y; then if ! apt install git -y; then
echo " [E] Cannot install git via apt" echo " [e] Cannot install git via apt"
exit 3 exit 3
fi fi
fi fi
# Clone sequncer to target directory # Clone sequncer to target directory
if ! git clone $SEQGITURL "$SEQUENCER_DIR"; then if ! git clone "${sequencer_gitUrl}" "$sequencerDir"; then
echo " [E] Error cloning git repository:" echo " [E] Error cloning git repository:"
echo " $SEQGITURL" echo " ${sequencer_gitUrl}"
exit 4 exit 4
fi fi
# If available use configuration # If available use configuration
. "${SEQUENCER_DIR}/sequencer.cfg" >/dev/null 2>&1 . "${sequencerDir}/sequencer.cfg" >/dev/null 2>&1
# Set to default if not configured # Set to default if not configured
[ -z "$SEQUENCER_USER_SEQS" ] && SEQUENCER_USER_SEQS="$DEFAULT_USER_SEQS" [ -z "${SEQUENCER_USER_SEQS}" ] && SEQUENCER_USER_SEQS="${defaultUserSeqs}"
# Install sequncer script # Install sequncer script
ln -s "${SEQUENCER_DIR}/sequencer/sequencer.sh" "/usr/local/bin" ln -s "${sequencerDir}/sequencer/sequencer.sh" "/usr/local/bin"
if [ "$SEQUENCER_USER_SEQS" != "$SEQUENCER_DIR/seqs" ]; then if [ "${SEQUENCER_USER_SEQS}" != "${sequencerDir}/seqs" ]; then
ln -sT "${SEQUENCER_DIR}/seqs" "${SEQUENCER_USER_SEQS}" ln -sT "${sequencerDir}/seqs" "${SEQUENCER_USER_SEQS}"
fi fi
echo " [I] Successfully installed shell sequencer" echo " [I] Successfully installed shell sequencer"
echo " to: $SEQUENCER_DIR" echo " to: ${sequencerDir}"

View File

@@ -1,21 +1,21 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Get script working directory # Get script working directory
sq_dir="$(cd "$(dirname -- "$(realpath "${BASH_SOURCE[0]}")")" >>/dev/null 2>&1 && pwd)" sq_dir="$(cd "$(dirname -- "$(realpath -- "${BASH_SOURCE[0]}")")" >>/dev/null 2>&1 && pwd)"
_sequencerCompletion() { _sequencerCompletion() {
local SEQCOMP_LOC="${sq_dir}/sqn-completion.bash" local seqcomp_loc="${sq_dir}/sqn-completion.bash"
local SEQCOMP_LOADER="$HOME/.bashrc" local seqcomp_loader="$HOME/.bashrc"
local SEQCOMP_SOURCE=". \"$SEQCOMP_LOC\"" local seqcomp_source=". \"${seqcomp_loc}\""
if grep "$SEQCOMP_SOURCE" "$SEQCOMP_LOADER" >>/dev/null 2>&1; then if grep "${seqcomp_source}" "${seqcomp_loader}" >>/dev/null 2>&1; then
echo " [I] Completion already installed ($SEQCOMP_LOADER)" echo " [i] Completion already installed (${seqcomp_loader})"
else else
echo "$SEQCOMP_SOURCE" >>"$SEQCOMP_LOADER" echo "${seqcomp_source}" >>"${seqcomp_loader}"
echo " [I] Sequence bash-completion installed" echo " [i] Sequence bash-completion installed"
fi fi
# shellcheck source=sqn-completion.bash # shellcheck source=sqn-completion.bash
. "$SEQCOMP_LOC" . "${seqcomp_loc}"
} }
_sequencerCompletion _sequencerCompletion

View File

@@ -8,7 +8,7 @@ _sqn_completions()
# Extract alias command with buildins same as: # Extract alias command with buildins same as:
# local curCmd="$(alias $1 | sed "s/^alias.*\" \"\(.*\)\"'$/\1/")" # local curCmd="$(alias $1 | sed "s/^alias.*\" \"\(.*\)\"'$/\1/")"
local curCmd= local curCmd=
curCmd="$(alias "$1")" curCmd="$(alias "${1:-}")"
curCmd="${curCmd##*\" \"}" curCmd="${curCmd##*\" \"}"
curCmd="${curCmd%\"\'*}" curCmd="${curCmd%\"\'*}"
COMPREPLY=() COMPREPLY=()
@@ -66,24 +66,25 @@ installCompletion() {
. "${script_dir}/sequencer.cfg" >/dev/null 2>&1 . "${script_dir}/sequencer.cfg" >/dev/null 2>&1
local SEQBASE="${script_dir}/seqs" local seq_base="${script_dir}/seqs"
local SEQPREFIX="sqn_" local seq_prefix="sqn_"
local SEQSHORT=() local seq_short=()
local seq_list=()
# Check for user selected sequences # Check for user selected sequences
[ -e "$SEQUENCER_USER_SEQS" ] && SEQBASE="$SEQUENCER_USER_SEQS" [ -e "$SEQUENCER_USER_SEQS" ] && seq_base="$SEQUENCER_USER_SEQS"
# Create aliases and command (alias) list for "complete" command" # Create aliases and command (alias) list for "complete" command"
IFS=$'\n' read -r -d '' -a SEQLIST < <(ls -1 "$SEQBASE/${cur}"*.sh) IFS=$'\n' read -r -d '' -a seq_list < <(ls -1 "${seq_base}/${cur}"*.sh)
for i in "${!SEQLIST[@]}"; do for i in "${!seq_list[@]}"; do
SEQLIST[$i]="${SEQLIST[$i]##*/}" seq_list[$i]="${seq_list[$i]##*/}"
SEQLIST[$i]="${SEQLIST[$i]%.*}" seq_list[$i]="${seq_list[$i]%.*}"
SEQSHORT[$i]="${SEQPREFIX}${SEQLIST[$i]/[[:blank:]]/_}" seq_short[$i]="${seq_prefix}${seq_list[$i]/[[:blank:]]/_}"
# shellcheck disable=SC2139,SC2086 # shellcheck disable=SC2139,SC2086
# alias should be expanded when defined # alias should be expanded when defined
alias ${SEQSHORT[$i]}="_SQN_ALIAS=\"${SEQSHORT[$i]}\" \"$SEQBASE/${SEQLIST[$i]}.sh\"" alias ${seq_short[$i]}="_SQN_ALIAS=\"${seq_short[$i]}\" \"${seq_base}/${seq_list[$i]}.sh\""
done done
complete -o nosort -o bashdefault -o default -F _sqn_completions "${SEQSHORT[@]}" complete -o nosort -o bashdefault -o default -F _sqn_completions "${seq_short[@]}"
} }
installCompletion installCompletion