From 859df947152fda9ab1ae6cae6805afc1e0a68a7e Mon Sep 17 00:00:00 2001 From: june Date: Thu, 19 Feb 2026 20:40:04 +0100 Subject: [PATCH] refactor: clean up and fix some things (#2) Reviewed-on: https://codeberg.org/comfysage/ebil/pulls/2 Co-authored-by: june Co-committed-by: june --- ebil.sh | 116 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/ebil.sh b/ebil.sh index afcc00e..0375594 100755 --- a/ebil.sh +++ b/ebil.sh @@ -2,26 +2,16 @@ set -euo pipefail -# defaults ==================================================================== - -EBIL_SITE="${EBIL_SITE:-""}" -EBIL_PATH="${EBIL_PATH:-"."}" -EBIL_HOST="${EBIL_HOST:-"ebil.club"}" - -# util ======================================================================== - -PROMPT="" - msg() { - printf "\033[32;1m%s\033[m %s\n" "$PROMPT" "$*" + printf "\033[32;1m%s:\033[m %s\n" 'info' "$*" } warn() { - >&2 printf "\033[33;1m%s \033[mwarning: %s\n" "$PROMPT" "$*" + >&2 printf "\033[33;1m%s:\033[m %s\n" 'warning' "$*" } die() { - >&2 printf "\033[31;1m%s \033[merror: %s\n" "$PROMPT" "$*" + >&2 printf "\033[31;1m%s:\033[m %s\n" 'error' "$*" exit 1 } @@ -48,65 +38,79 @@ EOF } sourceenv() { - [ -f .env ] && source .env || return 0 + # shellcheck disable=SC2046 + [[ -f .env ]] && export $(grep -v '^#' .env | xargs -0) || return 0 +} + +checksite() { + local site="$1" + local pat='^[a-z]+.ebil.club$' + + [[ -z "$site" ]] && die 'site not specified' + [[ "$site" =~ $pat ]] || die 'malformed site url' "(expected form '${pat}'):" "$site" +} + +checkpath() { + [[ -d "$1" ]] || die 'path does not exist' } push() { - if [ "$#" -gt 0 ]; then - EBIL_PATH="$1" - fi - EBIL_PATH=$(realpath "${EBIL_PATH}") - if [ ! -d "$EBIL_PATH" ]; then - die "EBIL_PATH does not exist:" "${EBIL_PATH}" - fi - if [[ ! "$EBIL_SITE" =~ ^[a-z]+.ebil.club$ ]]; then - die "malformed site url (expected form '^[a-z]+.ebil.club$'):" "$EBIL_SITE" - fi - name="${EBIL_SITE%.ebil.club}" + local site="$1" + local path="$2" + local host="$3" + local name - msg "user: $name" - msg "pushing to" "${EBIL_SITE}" "from" "${EBIL_PATH}" - if [ -f "${EBIL_PATH}/index.txt" ]; then - msg "" "custom curl message configured" "${EBIL_PATH}/index.txt" - fi - rsync -rltzq --progress --delete --chmod=D755,F644 "${EBIL_PATH}/" "${EBIL_HOST}:/var/ebil.club/${name}/${EBIL_SITE}" + checksite "$site" + checkpath "$path" + + name="${site%.ebil.club}" + + msg 'user:' "$name" + [[ -f "${path}/index.txt" ]] && msg 'custom curl message configured' "(${path}/index.txt)" + + msg 'pushing to' "$site" 'from' "$path" + rsync -rltzq --progress --delete --chmod=D755,F644 "${path}/" "${host}:/var/ebil.club/${name}/${site}" } pull() { - if [ "$#" -gt 0 ]; then - EBIL_PATH="$1" - fi - EBIL_PATH=$(realpath "${EBIL_PATH}") - if [ ! -d "$EBIL_PATH" ]; then - msg "creating dst directory" "$EBIL_PATH" - mkdir -p "$EBIL_PATH" - fi - if [[ ! "$EBIL_SITE" =~ ^[a-z]+.ebil.club$ ]]; then - die "malformed site url (expected form '^[a-z]+.ebil.club$'):" "$EBIL_SITE" + local site="$1" + local path="$2" + local host="$3" + + checksite "$site" + + if [ ! -d "$path" ]; then + msg 'creating destination directory' "$path" + mkdir -p "$path" fi - name="${EBIL_SITE%.ebil.club}" - msg "pulling from" "${EBIL_SITE}" "to" "${EBIL_PATH}" - rsync -rltzq --progress "${EBIL_HOST}:/var/ebil.club/${name}/${EBIL_SITE}/" "${EBIL_PATH}" + name="${site%.ebil.club}" + + msg 'pulling from' "$site" 'to' "$path" + rsync -rltzq --progress "${host}:/var/ebil.club/${name}/${site}/" "${path}" } main() { sourceenv - flag="" + local cmd='' + local site="${EBIL_SITE:-}" + local path="${EBIL_PATH:-.}" + local host="${EBIL_HOST:-ebil.club}" + if [ "$#" -gt 0 ]; then - flag=${1#-} + cmd="${1#-}" shift fi while [ "$#" -gt 0 ]; do case "$1" in --site) - EBIL_SITE="$2" + site="$2" shift ;; --path) - EBIL_PATH="$2" + path="$2" shift ;; --help) @@ -117,15 +121,13 @@ main() { shift done - case $flag in - push) - push "$@" - ;; - pull) - pull "$@" - ;; - "") phelp ;; - *) die "command does not exist." ;; + path="$(realpath "$path")" + + case "$cmd" in + push) push "$site" "$path" "$host" ;; + pull) pull "$site" "$path" "$host" ;; + '') phelp ;; + *) die 'command not found' ;; esac } -- 2.51.2