this repo has no description
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
18phelp() {
19 cat <<EOF
20 ebil - ebil.club cli
21
22usage:
23 push | push your site files to ebil.club
24 > ebil push --site robin.ebil.club dist/
25 pull | pull your site from ebil.club to a local directory
26 > ebil pull --site robin.ebil.club site/
27
28env:
29 you can set certain options using environment variables.
30
31 EBIL_SITE="robin.ebil.club" | --site robin.ebil.club
32 EBIL_PATH="dist/" | --path dist/
33
34note:
35 specify options *before* regular arguments.
36EOF
37 exit 0
38}
39
40sourceenv() {
41 # shellcheck disable=SC2046
42 [[ -f .env ]] && export $(grep -v '^#' .env | xargs -0) || return 0
43}
44
45checksite() {
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
53checkpath() {
54 [[ -d "$1" ]] || die 'path does not exist'
55}
56
57push() {
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}"
67
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}
74
75pull() {
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"
85 fi
86
87 name="${site%.ebil.club}"
88
89 msg 'pulling from' "$site" 'to' "$path"
90 rsync -rltzq --progress "${host}:/var/ebil.club/${name}/${site}/" "${path}"
91}
92
93main() {
94 sourceenv
95
96 local cmd=''
97 local site="${EBIL_SITE:-}"
98 local path="${EBIL_PATH:-.}"
99 local host="${EBIL_HOST:-ebil.club}"
100
101 if [ "$#" -gt 0 ]; then
102 cmd="${1#-}"
103 shift
104 fi
105
106 while [ "$#" -gt 0 ]; do
107 case "$1" in
108 --site)
109 site="$2"
110 shift
111 ;;
112 --path)
113 path="$2"
114 shift
115 ;;
116 --help)
117 phelp
118 ;;
119 *) break ;;
120 esac
121 shift
122 done
123
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' ;;
131 esac
132}
133
134main "$@"