Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188#!/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 <<EOF ebil - ebil.club cli
usage: push | push your site files to ebil.club > 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 "$@"