#!/usr/bin/env bash set -euo pipefail msg() { printf "\033[32;1m%s:\033[m %s\n" 'info' "$*" } warn() { >&2 printf "\033[33;1m%s:\033[m %s\n" 'warning' "$*" } die() { >&2 printf "\033[31;1m%s:\033[m %s\n" 'error' "$*" exit 1 } confirm() { local msg="$1" echo -n "$msg [y/N] " local confirm read -r confirm [[ "${confirm,,}" = 'y' ]] } phelp() { cat < ebil push --user robin dist/ pull | pull your site from ebil.club to a local directory > ebil pull --user robin site/ env: you can set certain options using environment variables. EBIL_USER="robin" | --user robin EBIL_PATH="dist/" | push --from dist/ pull --to dist/ EBIL_PATH_REMOTE="root" | push --to root pull --from root EBIL_HOST="ebil.club" EBIL_REMOTE="/var/ebil.club/\${user}/\${path_remote}" note: specify options *before* regular arguments. EOF exit 0 } sourceenv() { # shellcheck disable=SC2046 [[ -f .env ]] && export $(grep -v '^#' .env | xargs -0) || return 0 } checkfrom() { [[ -d "$1" ]] || die 'from does not exist' } rcopy() { local src="$1" local dst="$2" shift shift rsync -rltzq --progress "$@" "$src/" "$dst" } push() { local user="$1" local from="$2" local to="$3" local host="$4" local remote="$5" from="$(realpath "$from")" checkfrom "$from" if [[ -z "$remote" ]]; then msg 'user:' "$user" remote="/var/ebil.club/${user}/${to}" fi [[ -f "${from}/index.txt" ]] && msg 'custom curl message configured' "(${from}/index.txt)" [[ -f "${from}/index.html" ]] || warn 'index.html not found' "(${from}/index.html)" msg 'pushing to' "${user}@${host}:${to}" 'from' "$from" rcopy "$from" "${host}:${remote}" --delete --chmod=D755,F644 } pull() { local user="$1" local from="$2" local to="$3" local host="$4" local remote="$5" to="$(realpath "$to")" if [ ! -d "$to" ]; then msg 'creating destination directory' "$to" mkdir -p "$to" fi if [[ -z "$(find "$to" -maxdepth 0 -empty)" ]]; then warn 'destination directory is not empty' "(${to})" confirm 'are you sure you want to use this directory?' fi if [[ -z "$remote" ]]; then msg 'user:' "$user" remote="/var/ebil.club/${user}/${from}" fi msg 'pulling from' "${user}@${host}:${from}" 'to' "$to" rcopy "${host}:${remote}" "$to" } main() { sourceenv local user="${EBIL_USER:-}" local host="${EBIL_HOST:-ebil.club}" local remote="${EBIL_REMOTE:-}" local path="${EBIL_PATH:-dist}" local path_remote="${EBIL_PATH_REMOTE:-root}" local cmd if [ "$#" -gt 0 ]; then cmd="${1#-}" shift fi local from local to while [ "$#" -gt 0 ]; do case "$1" in --user) user="$2" shift ;; --from) from="$2" shift ;; --to) to="$2" shift ;; --help) phelp ;; *) break ;; esac shift done if [[ -z "$user" ]]; then die 'no user specified' fi case "$cmd" in push) from="${from:-$path}" from="${1:-$from}" to="${to:-$path_remote}" push "$user" "$from" "$to" "$host" "$remote" ;; pull) from="${from:-$path_remote}" to="${to:-$path}" to="${1:-$to}" pull "$user" "$from" "$to" "$host" "$remote" ;; '') phelp ;; *) die 'command not found' ;; esac } main "$@"