-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpglet.sh
364 lines (298 loc) · 8.74 KB
/
pglet.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# shellcheck shell=bash
# Constants
PGLET_VER="0.7.0" # Pglet version required by this script
PGLET_DEFAULT_INSTALL_DIR="$HOME/.pglet/bin" # Default installation directory
# Installation variables
PGLET_INSTALL_DIR=${PGLET_INSTALL_DIR:-""} # Custom installation directory
# Default session variables:
PGLET_EXE="" # full path to Pglet executable
PGLET_CONNECTION_ID="" # the last page connection ID.
PGLET_PAGE_URL="" # the last page URL.
PGLET_EVENT_TARGET="" # the last received event target (control ID).
PGLET_EVENT_NAME="" # the last received event name.
PGLET_EVENT_DATA="" # the last received event data.
# Parameters:
# $1 - page name
# Variables:
# PGLET_WEB - makes the page available as public at pglet.io service or a self-hosted Pglet server
# PGLET_PRIVATE - makes the page available as private at pglet.io service or a self-hosted Pglet server
# PGLET_SERVER - connects to the page on a self-hosted Pglet server
# PGLET_TOKEN - authentication token for pglet.io service or a self-hosted Pglet server
function pglet_page() {
local pargs=(page)
if [[ $# -ne 0 && "$1" != "" ]]; then
pargs+=("$1")
fi
if [[ -n "${PGLET_WEB-}" && "$PGLET_WEB" == "true" ]]; then
pargs+=(--web)
fi
if [[ -n "${PGLET_SERVER-}" && "$PGLET_SERVER" != "" ]]; then
pargs+=(--server "$PGLET_SERVER")
fi
if [[ -n "${PGLET_TOKEN-}" && "$PGLET_TOKEN" != "" ]]; then
pargs+=(--token "$PGLET_TOKEN")
fi
if [[ -n "${PGLET_NO_WINDOW-}" && "$PGLET_NO_WINDOW" != "" ]]; then
pargs+=(--no-window)
fi
if [[ -n "${PGLET_TICKER-}" && "$PGLET_TICKER" != "" ]]; then
pargs+=(--ticker "$PGLET_TICKER")
fi
# execute pglet and get page connection ID
local page_results
page_results=$($PGLET_EXE "${pargs[@]}")
IFS=' ' read -r PGLET_CONNECTION_ID PGLET_PAGE_URL <<< "$page_results"
echo "Page URL: $PGLET_PAGE_URL"
}
function __pglet_start_session() {
echo "Started session: $1"
PGLET_CONNECTION_ID=$1
local fn=$2
eval "$fn"
}
function pglet_app() {
local pargs=(app)
if [[ $# -eq 1 ]]; then
# only hander function specified
local fn=$1
elif [[ $# -eq 2 ]]; then
# page name and hander function specified
pargs+=("$1")
local fn=$2
else
echo "Error: wrong number of arguments"
exit 1
fi
if [[ -n "${PGLET_WEB-}" && "$PGLET_WEB" == "true" ]]; then
pargs+=(--web)
fi
if [[ -n "${PGLET_SERVER-}" && "$PGLET_SERVER" != "" ]]; then
pargs+=(--server "$PGLET_SERVER")
fi
if [[ -n "${PGLET_TOKEN-}" && "$PGLET_TOKEN" != "" ]]; then
pargs+=(--token "$PGLET_TOKEN")
fi
if [[ -n "${PGLET_NO_WINDOW-}" && "$PGLET_NO_WINDOW" != "" ]]; then
pargs+=(--no-window)
fi
if [[ -n "${PGLET_TICKER-}" && "$PGLET_TICKER" != "" ]]; then
pargs+=(--ticker "$PGLET_TICKER")
fi
# reset vars
PGLET_PAGE_URL=""
# execute pglet
$PGLET_EXE "${pargs[@]}" |
{
while read -r session_id
do
if [[ "$PGLET_PAGE_URL" == "" ]]; then
PGLET_PAGE_URL="$session_id"
echo "Page URL: $PGLET_PAGE_URL"
else
__pglet_start_session "$session_id" "$fn" &
fi
done
}
}
function pglet_send() {
if [[ $# -eq 1 ]]; then
local conn_id=$PGLET_CONNECTION_ID
local cmd=$1
elif [[ $# -eq 2 ]]; then
local conn_id=$1
local cmd=$2
else
echo "Error: wrong number of arguments"
exit 1
fi
# send command
echo "$cmd" > "$conn_id"
# take result if command doesn't end with "f" (fire-and-forget)
if [[ "$cmd" =~ ^[[:space:]]*[A-Za-z]+f ]]; then
return
fi
# read result
local firstLine="true"
local result_value=""
IFS=''
while read -r line; do
if [[ $firstLine == "true" ]]; then
IFS=' ' read -r result_status result_value <<< "$line"
firstLine="false"
if [[ "$result_status" == "error" ]]; then
echo "Error: $result_value"
exit 2
fi
else
result_value="$line"
fi
echo "$result_value"
done <"$conn_id"
}
function pglet_add() {
pglet_send "add $1"
}
function pglet_addf() {
pglet_send "addf $1"
}
function pglet_set() {
pglet_send "set $1"
}
function pglet_setf() {
pglet_send "setf $1"
}
function pglet_set_value() {
pglet_send "set $1 value=\"${2//\"/\\\"}\""
}
function pglet_set_valuef() {
pglet_send "setf $1 value=\"${2//\"/\\\"}\""
}
function pglet_get_value() {
pglet_send "get $1 value"
}
function pglet_show() {
pglet_send "set $1 visible=true"
}
function pglet_hide() {
pglet_send "set $1 visible=false"
}
function pglet_enable() {
pglet_send "set $1 enabled=true"
}
function pglet_disable() {
pglet_send "set $1 enabled=false"
}
function pglet_clean() {
pglet_send "clean $1"
}
function pglet_remove() {
pglet_send "remove $1"
}
# shellcheck disable=SC2120
function pglet_wait_event() {
if [[ $# -ne 0 && "$1" != "" ]]; then
local conn_id=$1
else
local conn_id=$PGLET_CONNECTION_ID
fi
# shellcheck disable=SC2034
IFS=' ' read -r PGLET_EVENT_TARGET PGLET_EVENT_NAME PGLET_EVENT_DATA < "$conn_id.events"
}
function pglet_dispatch_events() {
# https://askubuntu.com/questions/992439/bash-pass-both-array-and-non-array-parameter-to-function
#echo "count: $#"
arr=("$@")
IFS=' '
while true
do
pglet_wait_event
for evt in "${arr[@]}";
do
IFS=' ' read -r et en fn <<< "$evt"
if [[ "$PGLET_EVENT_TARGET" == "$et" && "$PGLET_EVENT_NAME" == "$en" ]]; then
eval "$fn"
return
fi
#echo "$et - $en - $fn"
done
done
}
# escape new lines and single quotes
function escape_sq_str() {
local CR="
"
local r1="${1//${CR}/\\\n}"
echo "${r1//\'/\\\'}"
}
# escape new lines and double quotes
function escape_dq_str() {
local CR="
"
local r1="${1//${CR}/\\\n}"
echo "${r1//\"/\\\"}" # escape double quotes
}
# execute command and escape new lines and single quotes
function escape_sq_cmd() {
local CR="
"
local result
result=$("$@")
local r1="${result//${CR}/\\\n}"
echo "${r1//\'/\\\'}" # escape single quotes
}
# execute command and escape new lines and double quotes
function escape_dq_cmd() {
local CR="
"
local result
result=$("$@")
local r1="${result//${CR}/\\\n}"
echo "${r1//\"/\\\"}" # escape double quotes
}
function __pglet_install() {
if [[ -n "${OS-}" && "$OS" = "Windows_NT" ]]; then
echo "Error: Bash for Windows is not supported." 1>&2
exit 1
fi
platform=$(uname -s)
if [[ $platform == Darwin ]]; then
platform="darwin"
elif [[ $platform == Linux ]]; then
platform="linux"
else
echo "Error: Unsupported platform $platform." 1>&2
exit 1
fi
arch=$(uname -m)
if [[ $arch == x86_64 ]] || [[ $arch == amd64 ]]; then
arch="amd64"
elif [[ $arch == arm64 ]] || [[ $arch == aarch64 ]]; then
arch="arm64"
elif [[ $arch == arm* ]]; then
arch="arm"
else
echo "Error: Unsupported architecture $arch." 1>&2
exit 1
fi
# check if pglet.exe is in PATH already
local current_pglet_dir=""
if command -v pglet &> /dev/null
then
PGLET_EXE=$(which pglet)
current_pglet_dir="$(dirname "${PGLET_EXE}")"
fi
# pglet installation dir
local pglet_dir="$PGLET_DEFAULT_INSTALL_DIR"
if [[ "$PGLET_INSTALL_DIR" != "" ]]; then
pglet_dir="$PGLET_INSTALL_DIR"
elif [[ "$current_pglet_dir" != "" ]]; then
pglet_dir="$current_pglet_dir"
fi
# check if there is Pglet aready installed
PGLET_EXE="$pglet_dir/pglet"
local ver="$PGLET_VER"
local installed_ver=""
if [ -f "$PGLET_EXE" ]; then
installed_ver=$($PGLET_EXE --version)
fi
#echo "Installed version: $installed_ver"
if [[ "$installed_ver" != "unknown" && "$installed_ver" != "$ver" ]]; then
pkill pglet
printf "Installing Pglet v%s to %s..." "$ver" "$pglet_dir"
if [ ! -d "$pglet_dir" ]; then
mkdir -p "$pglet_dir"
fi
local pglet_url="https://github.com/pglet/pglet/releases/download/v${ver}/pglet-${ver}-${platform}-${arch}.tar.gz"
local tempTar="/tmp/pglet.tar.gz"
{
curl -fsSL $pglet_url -o $tempTar &&
tar -zxf $tempTar -C "$pglet_dir" pglet
} || {
echo "Error downloading and extracting pglet executable." 1>&2
exit 1
}
rm $tempTar
printf "OK\n"
fi
}
__pglet_install