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
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 --user robin dist/
34 pull | pull your site from ebil.club to a local directory
35 > ebil pull --user robin site/
36
37env:
38 you can set certain options using environment variables.
39
40 EBIL_USER="robin" | --user robin
41 EBIL_PATH="dist/" | push --from dist/
42 pull --to dist/
43 EBIL_PATH_REMOTE="root" | push --to root
44 pull --from root
45 EBIL_HOST="ebil.club"
46 EBIL_REMOTE="/var/ebil.club/\${user}/\${path_remote}"
47
48note:
49 specify options *before* regular arguments.
50EOF
51 exit 0
52}
53
54sourceenv() {
55 # shellcheck disable=SC2046
56 [[ -f .env ]] && export $(grep -v '^#' .env | xargs -0) || return 0
57}
58
59checkfrom() {
60 [[ -d "$1" ]] || die 'from does not exist'
61}
62
63rcopy() {
64 local src="$1"
65 local dst="$2"
66 shift
67 shift
68
69 rsync -rltzq --progress "$@" "$src/" "$dst"
70}
71
72push() {
73 local user="$1"
74 local from="$2"
75 local to="$3"
76 local host="$4"
77 local remote="$5"
78
79 from="$(realpath "$from")"
80 checkfrom "$from"
81
82 if [[ -z "$remote" ]]; then
83 msg 'user:' "$user"
84 remote="/var/ebil.club/${user}/${to}"
85 fi
86
87 [[ -f "${from}/index.txt" ]] && msg 'custom curl message configured' "(${from}/index.txt)"
88 [[ -f "${from}/index.html" ]] || warn 'index.html not found' "(${from}/index.html)"
89
90 msg 'pushing to' "${user}@${host}:${to}" 'from' "$from"
91 rcopy "$from" "${host}:${remote}" --delete --chmod=D755,F644
92}
93
94pull() {
95 local user="$1"
96 local from="$2"
97 local to="$3"
98 local host="$4"
99 local remote="$5"
100
101 to="$(realpath "$to")"
102
103 if [ ! -d "$to" ]; then
104 msg 'creating destination directory' "$to"
105 mkdir -p "$to"
106 fi
107
108 if [[ -z "$(find "$to" -maxdepth 0 -empty)" ]]; then
109 warn 'destination directory is not empty' "(${to})"
110 confirm 'are you sure you want to use this directory?'
111 fi
112
113 if [[ -z "$remote" ]]; then
114 msg 'user:' "$user"
115 remote="/var/ebil.club/${user}/${from}"
116 fi
117
118 msg 'pulling from' "${user}@${host}:${from}" 'to' "$to"
119 rcopy "${host}:${remote}" "$to"
120}
121
122main() {
123 sourceenv
124
125 local user="${EBIL_USER:-}"
126 local host="${EBIL_HOST:-ebil.club}"
127 local remote="${EBIL_REMOTE:-}"
128 local path="${EBIL_PATH:-dist}"
129 local path_remote="${EBIL_PATH_REMOTE:-root}"
130
131 local cmd
132
133 if [ "$#" -gt 0 ]; then
134 cmd="${1#-}"
135 shift
136 fi
137
138 local from
139 local to
140
141 while [ "$#" -gt 0 ]; do
142 case "$1" in
143 --user)
144 user="$2"
145 shift
146 ;;
147 --from)
148 from="$2"
149 shift
150 ;;
151 --to)
152 to="$2"
153 shift
154 ;;
155 --help)
156 phelp
157 ;;
158 *) break ;;
159 esac
160 shift
161 done
162
163 if [[ -z "$user" ]]; then
164 die 'no user specified'
165 fi
166
167 case "$cmd" in
168 push)
169 from="${from:-$path}"
170 from="${1:-$from}"
171 to="${to:-$path_remote}"
172
173 push "$user" "$from" "$to" "$host" "$remote"
174 ;;
175 pull)
176 from="${from:-$path_remote}"
177 to="${to:-$path}"
178 to="${1:-$to}"
179
180 pull "$user" "$from" "$to" "$host" "$remote"
181 ;;
182 '') phelp ;;
183 *) die 'command not found' ;;
184 esac
185}
186
187main "$@"