this repo has no description
at main 176 lines 3.3 kB view raw
1#!/usr/bin/env bash 2 3set -euo pipefail 4 5msg() { 6 printf "\033[32;1m%s:\033[m %s\n" 'info' "$*" 7} 8 9warn() { 10 >&2 printf "\033[33;1m%s:\033[m %s\n" 'warning' "$*" 11} 12 13die() { 14 >&2 printf "\033[31;1m%s:\033[m %s\n" 'error' "$*" 15 exit 1 16} 17 18confirm() { 19 local msg="$1" 20 echo -n "$msg [y/N] " 21 22 local confirm 23 read -r confirm 24 [[ "${confirm,,}" = 'y' ]] 25} 26 27phelp() { 28 cat <<EOF 29 ebil - ebil.club cli 30 31usage: 32 push | push your site files to ebil.club 33 > ebil push --site robin.ebil.club dist/ 34 pull | pull your site from ebil.club to a local directory 35 > ebil pull --site robin.ebil.club site/ 36 37env: 38 you can set certain options using environment variables. 39 40 EBIL_SITE="robin.ebil.club" | --site robin.ebil.club 41 EBIL_PATH="dist/" | --path dist/ 42 EBIL_HOST="ebil.club" 43 EBIL_REMOTE="/var/ebil.club/\${name}/\${site}" 44 45note: 46 specify options *before* regular arguments. 47EOF 48 exit 0 49} 50 51sourceenv() { 52 # shellcheck disable=SC2046 53 [[ -f .env ]] && export $(grep -v '^#' .env | xargs -0) || return 0 54} 55 56checksite() { 57 local site="$1" 58 local pat='^[a-z]+.ebil.club$' 59 60 [[ -z "$site" ]] && die 'site not specified' 61 [[ "$site" =~ $pat ]] || die 'malformed site url' "(expected form '${pat}'):" "$site" 62} 63 64checkpath() { 65 [[ -d "$1" ]] || die 'path does not exist' 66} 67 68rcopy() { 69 local src="$1" 70 local dst="$2" 71 shift 72 shift 73 74 rsync -rltzq --progress "$@" "$src/" "$dst" 75} 76 77push() { 78 local site="$1" 79 local path="$2" 80 local host="$3" 81 local remote="$4" 82 local name 83 84 checkpath "$path" 85 86 if [[ ! -n "$remote" ]]; then 87 checksite "$site" 88 name="${site%.ebil.club}" 89 msg 'user:' "$name" 90 remote="/var/ebil.club/${name}/${site}" 91 fi 92 93 [[ -f "${path}/index.txt" ]] && msg 'custom curl message configured' "(${path}/index.txt)" 94 [[ -f "${path}/index.html" ]] || warn 'index.html not found' "(${path}/index.html)" 95 96 msg 'pushing to' "${site:-${remote}}" 'from' "$path" 97 rcopy "$path" "${host}:${remote}" --delete --chmod=D755,F644 98} 99 100pull() { 101 local site="$1" 102 local path="$2" 103 local host="$3" 104 local remote="$4" 105 106 if [ ! -d "$path" ]; then 107 msg 'creating destination directory' "$path" 108 mkdir -p "$path" 109 fi 110 111 if [[ ! -n "$(find "$path" -maxdepth 0 -empty)" ]]; then 112 warn 'destination directory is not empty' "(${path})" 113 confirm 'are you sure you want to use this directory?' 114 fi 115 116 if [[ ! -n "$remote" ]]; then 117 checksite "$site" 118 name="${site%.ebil.club}" 119 msg 'user:' "$name" 120 remote="/var/ebil.club/${name}/${site}" 121 fi 122 123 msg 'pulling from' "${site:-${remote}}" 'to' "$path" 124 rcopy "${host}:${remote}" "$path" 125} 126 127main() { 128 sourceenv 129 130 local cmd='' 131 local site="${EBIL_SITE:-}" 132 local path="${EBIL_PATH:-.}" 133 local host="${EBIL_HOST:-ebil.club}" 134 local remote="${EBIL_REMOTE:-}" 135 local path_opt='' 136 137 if [ "$#" -gt 0 ]; then 138 cmd="${1#-}" 139 shift 140 fi 141 142 while [ "$#" -gt 0 ]; do 143 case "$1" in 144 --site) 145 site="$2" 146 shift 147 ;; 148 --path) 149 path_opt="$2" 150 shift 151 ;; 152 --help) 153 phelp 154 ;; 155 *) break ;; 156 esac 157 shift 158 done 159 160 if [[ -n "$path_opt" ]]; then 161 path="$path_opt" 162 else 163 [[ -n "${1:-}" ]] && path="$1" 164 fi 165 166 path="$(realpath "$path")" 167 168 case "$cmd" in 169 push) push "$site" "$path" "$host" "$remote" ;; 170 pull) pull "$site" "$path" "$host" "$remote" ;; 171 '') phelp ;; 172 *) die 'command not found' ;; 173 esac 174} 175 176main "$@"