Skip to content

Commit

Permalink
Lower complexity of code (hopefully).
Browse files Browse the repository at this point in the history
Has fewer levels of indirection. Functions are used pretty much only when reuse is a factor, unless it makes good sense to package a piece of functionality as a unit. Uses file descriptors and a logging system. More sensible command line options. TODOs added for what's left. Various renames to lower redundancy.
  • Loading branch information
VersusFacit committed Aug 28, 2021
1 parent fb2a26b commit c3bb0fc
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 256 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
git-tmp/*
*.swp
TODO.md
*.log
runcoms/bashrc
3 changes: 1 addition & 2 deletions lib/exit-handling.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash

declare -r CLONE_DIR='./git-tmp'

err_routine () {
local -r script=${0-unknown}
local -r exit_status=${1-unknown}
Expand All @@ -17,6 +15,7 @@ err_routine () {
exit_routine() {
local -r exit_status="${1}"

cd "${REPO_ROOT}"
if [ -d "${CLONE_DIR}" ]; then
rm -rf "${CLONE_DIR}"
fi
Expand Down
88 changes: 35 additions & 53 deletions lib/logging-helpers.sh
Original file line number Diff line number Diff line change
@@ -1,71 +1,53 @@
#!/bin/bash
#!/bin/bash -e

#
# Basic logging
#
timestamp() {
echo "[$(date +"%y-%m-%d %T")]"
}

log () {
local -r term_width="$(tput cols)"
printf "%s %s\n" "$(timestamp)" "${@}" | fold -sw "${term_width}"
}
# Set colors for STDOUT
if [ -t 1 ]; then
declare -r TTY_STDOUT='true'
else
declare -r TTY_STDOUT='false'
fi

log_stderr() {
1>&2 log "$@"
}
readonly GRN="$([ "$TTY_STDOUT" = 'true' ] && tput setaf 2)"
readonly CLR="$([ "$TTY_STDOUT" = 'true' ] && tput sgr0)"

#
# Script verbosity management
#
# Set colors for STDERR
if [ -t 2 ]; then
declare -r TTY_STDERR='true'
else
declare -r TTY_STDERR='false'
fi

run_cmd_with_verbosity() {
local -r volume_limit="${1}"
if [ "${VERBOSITY:-0}" -ge "${volume_limit}" ]; then
eval "${@:2}"
else
>/dev/null eval "${@:2}"
fi
}
readonly ERR_RED="$([ "$TTY_STDERR" = 'true' ] && tput setaf 1)"
readonly ERR_YLW="$([ "$TTY_STDERR" = 'true' ] && tput setaf 3)"
readonly ERR_CLR="$([ "$TTY_STDERR" = 'true' ] && tput sgr0)"

run_quiet() {
run_cmd_with_verbosity '1' "${@}"
}

run_very_quiet() {
run_cmd_with_verbosity '2' "${@}"
}

log_quiet() {
run_cmd_with_verbosity '1' "echo '${*}'"
}

log_very_quiet() {
run_cmd_with_verbosity '2' "echo '${*}'"
timestamp() {
printf "[%s]" "$(date +"%y-%m-%d %T")"
}

#
# Write colored log messages
#

color_reset() {
tput sgr0
log () {
>&3 printf "%-5s%s\n" 'LOG' "$(timestamp) ${*}" | fold -sw "$(tput cols)"
}

log_info() {
local -r color="$(tput setaf 2)"
printf "%s %s\n%s" "$(timestamp)" "${color}${*}" "$(color_reset)"
tput sgr0
printf "%s%-5s%s\n" "${GRN}" 'INFO' "$(timestamp) ${*}${CLR}" \
| fold -sw "$(tput cols)"
}

log_warn() {
local -r color="$(tput setaf 3)"
>&2 printf "%s %s\n%s" "$(timestamp)" "${color}${*}" "$(color_reset)"
>&2 printf "%s%-5s%s\n" "${ERR_YLW}" 'WARN' \
"$(timestamp) ${*}${ERR_CLR}" | fold -sw "$(tput cols)"
}

log_error() {
local -r color="$(tput setaf 1)"
>&2 printf "%s %s\n%s" "$(timestamp)" "${color}${*}" "$(color_reset)"
>&2 printf "%s%-5s%s\n" "${ERR_RED}" 'ERR' \
"$(timestamp) ${*}${ERR_CLR}" | fold -sw "$(tput cols)"
}

quiet_popd() {
popd >/dev/null
}

quiet_pushd() {
pushd "${*}" >/dev/null
}
50 changes: 14 additions & 36 deletions lib/pkg-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,29 @@
ensure_package_installed() {
local -r pkg_name="${*}"

if run_quiet dpkg -s "${pkg_name}"; then
log_quiet "${pkg_name} is already installed, skipping..."
else
sudo apt-get install "${pkg_name}"
if dpkg -l | grep --quiet "${pkg_name}"; then
sudo apt install "${pkg_name}"
fi
}

ensure_install_all() {
for package_name in "${@}"; do
log_info "Installing package ${package_name}"
ensure_package_installed "${package_name}"
done
}

#
# Git
#

ensure_clean_git_tmp_dir_exists() {
if [ -d "${CLONE_DIR}" ]; then
rm -rf "${CLONE_DIR}"
fi
mkdir "${CLONE_DIR}"
}

clone_github_repo() {
local -r clone_destination="${1:-.}"
local -r repo_author="${2}"
local -r repo_name="${3}"
local -r url="https://github.com/${repo_author}/${repo_name}.git"

local git_option='--quiet'
if [ "${VERBOSITY:-0}" = '1' ]; then
git_option='--'
elif [ "${VERBOSITY:-0}" = '2' ]; then
local git_option='--'
if [ "${VERBOSITY:-normal}" = 'low' ]; then
git_option='--quiet'
elif [ "${VERBOSITY:-normal}" = 'high' ]; then
git_option='--verbose'
fi

git clone "${git_option}" "$url" "$clone_destination"
2>&3 >&3 git clone "${git_option}" "$url" "$clone_destination"
}

# See http://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
Expand All @@ -63,24 +47,18 @@ repo_is_out_of_date() {
fi
}

fast_forward_repo_to_remote() {
if ! grep --quiet "^Already up-to-date.$" <(2>&1 git pull origin master ); then
echo 'Repo now up to date'
fi
}

sync_git_repo() {
local author="${1}"
local name="${2}"
local path="${3:-.}"

if run_quiet 1>&2 pushd "${path}/${name}"; then
run_quiet git fetch
if quiet_pushd "${path}/${name}"; then
>&3 git fetch
if repo_is_out_of_date; then
fast_forward_repo_to_remote
2>&3 git pull origin master
fi
log_info "${name} up to date"
run_quiet popd
log "${name} up to date"
quiet_popd
else
clone_github_repo "${name}" "${author}" "${name}"
log_info "${name} installed"
Expand All @@ -91,6 +69,6 @@ sync_git_repo() {
# Pip
#

install_flake8() {
run_quiet python3 -m pip install flake8
pip_install() {
2>&3 >&3 python3 -m pip install "${*}"
}
17 changes: 11 additions & 6 deletions lib/setup.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#!/bin/bash

usage() {
1>&2 echo "Usage: $0 [-v | -vv | --verbose | --very-verbose]"
1>&2 echo "Usage: $0 [-v | --verbose | -q | --quiet]"
exit 2
}

exec 3> ./build.log

positional_params=''
while [ "$#" -gt 0 ]; do
case "$1" in
-v|--verbose)
if [ -n "${VERBOSITY}" ]; then usage; fi
VERBOSITY='1'
[ -n "${VERBOSITY}" ] && usage
VERBOSITY='high'
exec 3>&1
shift
;;
-vv|--very-verbose)
if [ -n "${VERBOSITY}" ]; then usage; fi
VERBOSITY='2'
-q|--quiet)
[ -n "${VERBOSITY}" ] && usage
VERBOSITY='low'
exec 1> ./build.log
exec 3>&1
shift
;;
-*) # unsupported flags
Expand Down
33 changes: 21 additions & 12 deletions lib/system-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,16 @@
# Interface
#

bell() {
local -r BELL_FILEPATH='/usr/share/sounds/freedesktop/stereo/complete.oga'
2>&1 paplay --volume=41000 "${BELL_FILEPATH}"
}
wait_for_user() {
local -r msg="${*}"

notify-send "$0" "${msg}"
2>&3 >&3 paplay --volume=41000 '/usr/share/sounds/freedesktop/stereo/complete.oga'

wait_for_enter() {
read -n 1 -s -r -p "Press any key to continue"
printf "\r"
}

wait_for_user_intervention() {
local -r msg="${*}"
notify-send "$0" "${msg}"
run_quiet bell
wait_for_enter
}

#
# OS management
#
Expand All @@ -31,3 +24,19 @@ ensure_directory_exists() {
fi
}

safe_copy() {
local -r runcom="${1}"
local -r setup_dir="${2}"

if [ "$(sha256sum < "${HOME}/.${runcom}")" \
= "$(sha256sum < "${RUNCOMS_DIR}/${runcom}")" ]; then
log "repo and system .${runcom} in sync"
elif [ "$(sha256sum < "${HOME}/.${runcom}")" \
= "$(head "${setup_dir}/old_${runcom}_sha.txt")" ]; then
cp -f "${RUNCOMS_DIR}/${runcom}" "${HOME}/.${runcom}"
log_warn "repo .${runcom} copied to system. Be sure to update old_${runcom}_sha.txt."
else
log_error "system .${runcom} has unmerged changes."
exit 2
fi
}
Loading

0 comments on commit c3bb0fc

Please sign in to comment.