diff options
author | Andrew Cady <d@jerkface.net> | 2023-08-23 19:03:48 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2023-08-23 19:03:48 -0400 |
commit | 5f8d82c9078b3076d7009bd8aa98e10ea3ac70a3 (patch) | |
tree | 965b20e9a0bc9a3cd2a6c45b7c29d8187c7d1ff8 | |
parent | 4f771eac3b737018c44e630d578683abb868846e (diff) |
split up rpc.bash into source-able library
-rw-r--r-- | src/rpc.bash (renamed from rpc.bash) | 74 | ||||
-rw-r--r-- | src/rpc.main.bash | 56 |
2 files changed, 69 insertions, 61 deletions
@@ -1,63 +1,31 @@ | |||
1 | #!/usr/bin/bash -e | ||
2 | set -e | ||
3 | set -o pipefail | ||
4 | 1 | ||
5 | max_script_size=$((1024 * 64)) | 2 | intersection() |
6 | |||
7 | our_main() | ||
8 | { | ||
9 | REMOTE_DEST=localhost | ||
10 | main2 "$@" | ||
11 | main1 "$@" | ||
12 | } | ||
13 | |||
14 | main1() | ||
15 | { | ||
16 | remote_run_function fudge "$@" | ||
17 | } | ||
18 | |||
19 | main2() | ||
20 | { | ||
21 | funcs=$(find_local_only_functions "$@" </dev/null | xargs) | ||
22 | [ ! "$funcs" ] || printf 'Local-only functions (main2): %s\n' "$funcs" >&2 | ||
23 | } | ||
24 | |||
25 | find_local_only_functions() | ||
26 | { | 3 | { |
27 | intersection <(extract_words < "$BASH_SOURCE") \ | 4 | comm -12 "$@" |
28 | <(difference <(all_commands) \ | ||
29 | <(remote_run_function all_commands)) | ||
30 | } | 5 | } |
31 | 6 | ||
32 | all_commands() | 7 | difference() |
33 | { | 8 | { |
34 | : main2 | 9 | comm -23 "$@" |
35 | compgen -A command | sort -u | ||
36 | } | 10 | } |
37 | 11 | ||
38 | gudge() | 12 | extract_words() |
39 | { | 13 | { |
40 | echo "Hello, ${REPLY@Q}." | 14 | grep -o -E '\b\w+\b' | sort -u |
41 | echo "Don't argue with me about ${*@Q}." | ||
42 | } | 15 | } |
43 | 16 | ||
44 | fudge() | 17 | read_len() |
45 | { | 18 | { |
46 | read -p 'Name? ' && gudge "$@" | 19 | [ "$1" -gt 0 ] |
47 | exit | 20 | REPLY= |
21 | read -r -N"$1" | ||
22 | [ "$REPLY" ] | ||
48 | } | 23 | } |
49 | 24 | ||
50 | |||
51 | |||
52 | |||
53 | read_remote_command() | 25 | read_remote_command() |
54 | { | 26 | { |
55 | len=$1 | 27 | read_len "$1" |
56 | shift | 28 | shift |
57 | [ "$len" -gt 0 ] | ||
58 | REPLY= | ||
59 | read -r -N"$len" | ||
60 | [ "$REPLY" ] | ||
61 | remote_command=(bash -c ${REPLY@Q} bash "${@@Q}") | 29 | remote_command=(bash -c ${REPLY@Q} bash "${@@Q}") |
62 | } | 30 | } |
63 | 31 | ||
@@ -86,16 +54,6 @@ remote_run_shell_script_arg1() | |||
86 | fi | 54 | fi |
87 | } | 55 | } |
88 | 56 | ||
89 | intersection() | ||
90 | { | ||
91 | comm -12 "$@" | ||
92 | } | ||
93 | |||
94 | extract_words() | ||
95 | { | ||
96 | grep -o -E '\b\w+\b' | sort -u | ||
97 | } | ||
98 | |||
99 | immediate_dependencies1() | 57 | immediate_dependencies1() |
100 | { | 58 | { |
101 | # Thanks Lri from stackoverflow.com/questions/4471364/ | 59 | # Thanks Lri from stackoverflow.com/questions/4471364/ |
@@ -113,11 +71,6 @@ immediate_dependencies() | |||
113 | done | sort -u | 71 | done | sort -u |
114 | } | 72 | } |
115 | 73 | ||
116 | difference() | ||
117 | { | ||
118 | comm -23 "$@" | ||
119 | } | ||
120 | |||
121 | recursive_dependencies() | 74 | recursive_dependencies() |
122 | { | 75 | { |
123 | resolved=() | 76 | resolved=() |
@@ -137,7 +90,7 @@ remote_run_function() | |||
137 | funcs=$(recursive_dependencies "$main") | 90 | funcs=$(recursive_dependencies "$main") |
138 | 91 | ||
139 | script= | 92 | script= |
140 | read -N${max_script_size} -r script < <( | 93 | read -N2147483647 -r script < <( |
141 | declare -f $funcs | 94 | declare -f $funcs |
142 | printf '%s "$@";\n' "$main" | 95 | printf '%s "$@";\n' "$main" |
143 | ) || [ "$script" ] | 96 | ) || [ "$script" ] |
@@ -145,4 +98,3 @@ remote_run_function() | |||
145 | remote_run_shell_script_arg1 "$script" "$@" | 98 | remote_run_shell_script_arg1 "$script" "$@" |
146 | } | 99 | } |
147 | 100 | ||
148 | our_main "$@" | ||
diff --git a/src/rpc.main.bash b/src/rpc.main.bash new file mode 100644 index 0000000..b859509 --- /dev/null +++ b/src/rpc.main.bash | |||
@@ -0,0 +1,56 @@ | |||
1 | #!/usr/bin/bash -e | ||
2 | set -e | ||
3 | set -o pipefail | ||
4 | |||
5 | case "$0" in | ||
6 | [^/]* ) PATH=$(realpath -e "$(dirname "$BASH_SOURCE")"):$PATH ;; | ||
7 | esac | ||
8 | |||
9 | . rpc.bash | ||
10 | |||
11 | our_main() | ||
12 | { | ||
13 | REMOTE_DEST=localhost | ||
14 | main2 "$@" | ||
15 | main1 "$@" | ||
16 | } | ||
17 | |||
18 | main1() | ||
19 | { | ||
20 | remote_run_function fudge "$@" | ||
21 | } | ||
22 | |||
23 | main2() | ||
24 | { | ||
25 | funcs=$(find_local_only_functions "$@" </dev/null | xargs) | ||
26 | [ ! "$funcs" ] || printf 'Local-only functions (main2): %s\n' "$funcs" >&2 | ||
27 | } | ||
28 | |||
29 | find_local_only_functions() | ||
30 | { | ||
31 | intersection <(extract_words < "$BASH_SOURCE") \ | ||
32 | <(difference <(all_commands) \ | ||
33 | <(remote_run_function all_commands)) | ||
34 | } | ||
35 | |||
36 | all_commands() | ||
37 | { | ||
38 | : main2 | ||
39 | compgen -A command | sort -u | ||
40 | } | ||
41 | |||
42 | gudge() | ||
43 | { | ||
44 | echo "Hello, ${REPLY@Q}." | ||
45 | topic=${*@Q} | ||
46 | echo "Don't argue with me about ${topic:-punctuation}." | ||
47 | } | ||
48 | |||
49 | fudge() | ||
50 | { | ||
51 | read -p 'Name? ' && gudge "$@" | ||
52 | exit | ||
53 | } | ||
54 | |||
55 | our_main "$@" | ||
56 | |||