this repo has no description

refactor: clean up and fix some things (#2)

Reviewed-on: https://codeberg.org/comfysage/ebil/pulls/2
Co-authored-by: june <me@koi.rip>
Co-committed-by: june <me@koi.rip>

authored by koi.rip koi.rip and committed by robinwobin.dev 859df947 6531d501

+59 -57
+59 -57
ebil.sh
··· 2 2 3 3 set -euo pipefail 4 4 5 - # defaults ==================================================================== 6 - 7 - EBIL_SITE="${EBIL_SITE:-""}" 8 - EBIL_PATH="${EBIL_PATH:-"."}" 9 - EBIL_HOST="${EBIL_HOST:-"ebil.club"}" 10 - 11 - # util ======================================================================== 12 - 13 - PROMPT="" 14 - 15 5 msg() { 16 - printf "\033[32;1m%s\033[m %s\n" "$PROMPT" "$*" 6 + printf "\033[32;1m%s:\033[m %s\n" 'info' "$*" 17 7 } 18 8 19 9 warn() { 20 - >&2 printf "\033[33;1m%s \033[mwarning: %s\n" "$PROMPT" "$*" 10 + >&2 printf "\033[33;1m%s:\033[m %s\n" 'warning' "$*" 21 11 } 22 12 23 13 die() { 24 - >&2 printf "\033[31;1m%s \033[merror: %s\n" "$PROMPT" "$*" 14 + >&2 printf "\033[31;1m%s:\033[m %s\n" 'error' "$*" 25 15 exit 1 26 16 } 27 17 ··· 48 38 } 49 39 50 40 sourceenv() { 51 - [ -f .env ] && source .env || return 0 41 + # shellcheck disable=SC2046 42 + [[ -f .env ]] && export $(grep -v '^#' .env | xargs -0) || return 0 43 + } 44 + 45 + checksite() { 46 + local site="$1" 47 + local pat='^[a-z]+.ebil.club$' 48 + 49 + [[ -z "$site" ]] && die 'site not specified' 50 + [[ "$site" =~ $pat ]] || die 'malformed site url' "(expected form '${pat}'):" "$site" 51 + } 52 + 53 + checkpath() { 54 + [[ -d "$1" ]] || die 'path does not exist' 52 55 } 53 56 54 57 push() { 55 - if [ "$#" -gt 0 ]; then 56 - EBIL_PATH="$1" 57 - fi 58 - EBIL_PATH=$(realpath "${EBIL_PATH}") 59 - if [ ! -d "$EBIL_PATH" ]; then 60 - die "EBIL_PATH does not exist:" "${EBIL_PATH}" 61 - fi 62 - if [[ ! "$EBIL_SITE" =~ ^[a-z]+.ebil.club$ ]]; then 63 - die "malformed site url (expected form '^[a-z]+.ebil.club$'):" "$EBIL_SITE" 64 - fi 65 - name="${EBIL_SITE%.ebil.club}" 58 + local site="$1" 59 + local path="$2" 60 + local host="$3" 61 + local name 62 + 63 + checksite "$site" 64 + checkpath "$path" 65 + 66 + name="${site%.ebil.club}" 66 67 67 - msg "user: $name" 68 - msg "pushing to" "${EBIL_SITE}" "from" "${EBIL_PATH}" 69 - if [ -f "${EBIL_PATH}/index.txt" ]; then 70 - msg "" "custom curl message configured" "${EBIL_PATH}/index.txt" 71 - fi 72 - rsync -rltzq --progress --delete --chmod=D755,F644 "${EBIL_PATH}/" "${EBIL_HOST}:/var/ebil.club/${name}/${EBIL_SITE}" 68 + msg 'user:' "$name" 69 + [[ -f "${path}/index.txt" ]] && msg 'custom curl message configured' "(${path}/index.txt)" 70 + 71 + msg 'pushing to' "$site" 'from' "$path" 72 + rsync -rltzq --progress --delete --chmod=D755,F644 "${path}/" "${host}:/var/ebil.club/${name}/${site}" 73 73 } 74 74 75 75 pull() { 76 - if [ "$#" -gt 0 ]; then 77 - EBIL_PATH="$1" 76 + local site="$1" 77 + local path="$2" 78 + local host="$3" 79 + 80 + checksite "$site" 81 + 82 + if [ ! -d "$path" ]; then 83 + msg 'creating destination directory' "$path" 84 + mkdir -p "$path" 78 85 fi 79 - EBIL_PATH=$(realpath "${EBIL_PATH}") 80 - if [ ! -d "$EBIL_PATH" ]; then 81 - msg "creating dst directory" "$EBIL_PATH" 82 - mkdir -p "$EBIL_PATH" 83 - fi 84 - if [[ ! "$EBIL_SITE" =~ ^[a-z]+.ebil.club$ ]]; then 85 - die "malformed site url (expected form '^[a-z]+.ebil.club$'):" "$EBIL_SITE" 86 - fi 87 - name="${EBIL_SITE%.ebil.club}" 86 + 87 + name="${site%.ebil.club}" 88 88 89 - msg "pulling from" "${EBIL_SITE}" "to" "${EBIL_PATH}" 90 - rsync -rltzq --progress "${EBIL_HOST}:/var/ebil.club/${name}/${EBIL_SITE}/" "${EBIL_PATH}" 89 + msg 'pulling from' "$site" 'to' "$path" 90 + rsync -rltzq --progress "${host}:/var/ebil.club/${name}/${site}/" "${path}" 91 91 } 92 92 93 93 main() { 94 94 sourceenv 95 95 96 - flag="" 96 + local cmd='' 97 + local site="${EBIL_SITE:-}" 98 + local path="${EBIL_PATH:-.}" 99 + local host="${EBIL_HOST:-ebil.club}" 100 + 97 101 if [ "$#" -gt 0 ]; then 98 - flag=${1#-} 102 + cmd="${1#-}" 99 103 shift 100 104 fi 101 105 102 106 while [ "$#" -gt 0 ]; do 103 107 case "$1" in 104 108 --site) 105 - EBIL_SITE="$2" 109 + site="$2" 106 110 shift 107 111 ;; 108 112 --path) 109 - EBIL_PATH="$2" 113 + path="$2" 110 114 shift 111 115 ;; 112 116 --help) ··· 117 121 shift 118 122 done 119 123 120 - case $flag in 121 - push) 122 - push "$@" 123 - ;; 124 - pull) 125 - pull "$@" 126 - ;; 127 - "") phelp ;; 128 - *) die "command does not exist." ;; 124 + path="$(realpath "$path")" 125 + 126 + case "$cmd" in 127 + push) push "$site" "$path" "$host" ;; 128 + pull) pull "$site" "$path" "$host" ;; 129 + '') phelp ;; 130 + *) die 'command not found' ;; 129 131 esac 130 132 } 131 133