summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@vps-18a7a2b7.vps.ovh.ca>2023-08-23 09:37:21 -0400
committerroot <root@vps-18a7a2b7.vps.ovh.ca>2023-08-23 09:37:21 -0400
commit5a952a8365ab5c15b44ff224575f3ebb6fab1988 (patch)
tree42012094960c9cbe00befd964949ee3f933dfdc4
parent438cc08d894c02263a57cf73614e395d959fe174 (diff)
rpc WIP
-rw-r--r--rpc.bash43
1 files changed, 43 insertions, 0 deletions
diff --git a/rpc.bash b/rpc.bash
new file mode 100644
index 0000000..a530216
--- /dev/null
+++ b/rpc.bash
@@ -0,0 +1,43 @@
1#!/usr/bin/bash -e
2set -e
3set -o pipefail
4
5f()
6{
7 read -p 'Name? ' &&
8 echo "Hello, $REPLY"
9 exit
10}
11
12script=$(declare -f f; echo f)
13
14remote_run_shell_script_stdin()
15{
16 len=$1
17 [ "$len" -gt 0 ]
18 read -r -N"$len"
19 bash -exc "eval ${REPLY@Q}"
20}
21
22remote_run_shell_script_arg1()
23{
24 script=$1
25 shift
26 (echo -n "$script"; cat) | remote_run_shell_script_stdin "${#script}" "$@"
27}
28
29# Thanks Lri from stackoverflow.com/questions/4471364/
30# how-do-i-list-the-functions-defined-in-my-shell/28278090
31remote_run_function()
32{
33 main=$1
34 shift
35 funcs=$(comm \
36 <(declare -f "$main" | grep -o -E '\b\w+\b' | sort -u) \
37 <(compgen -A function | sort -u)
38 )
39 script=$(declare -f $funcs; printf '%s "$@";\n' "$main")
40 remote_run_shell_script_arg1 "$script" "$@"
41}
42
43remote_run_function f