diff --git a/ebil.sh b/ebil.sh index d940b34..62aa231 100755 --- a/ebil.sh +++ b/ebil.sh @@ -38,7 +38,10 @@ env: you can set certain options using environment variables. EBIL_SITE="robin.ebil.club" | --site robin.ebil.club - EBIL_PATH="dist/" | --path dist/ + 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/\${name}/\${site}" @@ -61,8 +64,8 @@ checksite() { [[ "$site" =~ $pat ]] || die 'malformed site url' "(expected form '${pat}'):" "$site" } -checkpath() { - [[ -d "$1" ]] || die 'path does not exist' +checkfrom() { + [[ -d "$1" ]] || die 'from does not exist' } rcopy() { @@ -76,40 +79,45 @@ rcopy() { push() { local site="$1" - local path="$2" - local host="$3" - local remote="$4" + local from="$2" + local to="$3" + local host="$4" + local remote="$5" local name - checkpath "$path" + from="$(realpath "$from")" + checkfrom "$from" if [[ ! -n "$remote" ]]; then checksite "$site" name="${site%.ebil.club}" msg 'user:' "$name" - remote="/var/ebil.club/${name}/${site}" + remote="/var/ebil.club/${name}/${site}/${to}" fi - [[ -f "${path}/index.txt" ]] && msg 'custom curl message configured' "(${path}/index.txt)" - [[ -f "${path}/index.html" ]] || warn 'index.html not found' "(${path}/index.html)" + [[ -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' "${site:-${remote}}" 'from' "$path" - rcopy "$path" "${host}:${remote}" --delete --chmod=D755,F644 + msg 'pushing to' "${site:-${remote}}" 'from' "$from" + rcopy "$from" "${host}:${remote}" --delete --chmod=D755,F644 } pull() { local site="$1" - local path="$2" - local host="$3" - local remote="$4" + local from="$2" + local to="$3" + local host="$4" + local remote="$5" - if [ ! -d "$path" ]; then - msg 'creating destination directory' "$path" - mkdir -p "$path" + to="$(realpath "$to")" + + if [ ! -d "$to" ]; then + msg 'creating destination directory' "$to" + mkdir -p "$to" fi - if [[ ! -n "$(find "$path" -maxdepth 0 -empty)" ]]; then - warn 'destination directory is not empty' "(${path})" + if [[ ! -n "$(find "$to" -maxdepth 0 -empty)" ]]; then + warn 'destination directory is not empty' "(${to})" confirm 'are you sure you want to use this directory?' fi @@ -117,36 +125,42 @@ pull() { checksite "$site" name="${site%.ebil.club}" msg 'user:' "$name" - remote="/var/ebil.club/${name}/${site}" + remote="/var/ebil.club/${name}/${site}/${from}" fi - msg 'pulling from' "${site:-${remote}}" 'to' "$path" - rcopy "${host}:${remote}" "$path" + msg 'pulling from' "${site:-${remote}}" 'to' "$to" + rcopy "${host}:${remote}" "$to" } main() { sourceenv - local cmd='' local site="${EBIL_SITE:-}" - local path="${EBIL_PATH:-.}" local host="${EBIL_HOST:-ebil.club}" local remote="${EBIL_REMOTE:-}" - local path_opt='' + + local cmd='' if [ "$#" -gt 0 ]; then cmd="${1#-}" shift fi + local from_opt='' + local to_opt='' + while [ "$#" -gt 0 ]; do case "$1" in --site) site="$2" shift ;; - --path) - path_opt="$2" + --from) + from_opt="$2" + shift + ;; + --to) + to_opt="$2" shift ;; --help) @@ -157,17 +171,34 @@ main() { shift done - if [[ -n "$path_opt" ]]; then - path="$path_opt" - else - [[ -n "${1:-}" ]] && path="$1" - fi - - path="$(realpath "$path")" + local from + local to case "$cmd" in - push) push "$site" "$path" "$host" "$remote" ;; - pull) pull "$site" "$path" "$host" "$remote" ;; + push) + from="${EBIL_PATH:-dist}" + to="${EBIL_PATH_REMOTE:-root}" + + if [[ -n "$from_opt" ]]; then + from="$from_opt" + else + [[ -n "${1:-}" ]] && from="$1" + fi + + push "$site" "$from" "$to" "$host" "$remote" + ;; + pull) + from="${EBIL_PATH_REMOTE:-root}" + to="${EBIL_PATH:-dst}" + + if [[ -n "$to_opt" ]]; then + to="$to_opt" + else + [[ -n "${1:-}" ]] && to="$1" + fi + + pull "$site" "$from" "$to" "$host" "$remote" + ;; '') phelp ;; *) die 'command not found' ;; esac