diff options
author | Andrew Cady <d@jerkface.net> | 2023-08-23 13:19:27 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2023-08-23 13:19:27 -0400 |
commit | f0c21f1ffe947459e133dad71ed2ca20f59a8c6b (patch) | |
tree | b302182dc88e598b13b6454a4d053cee0e5b69b9 | |
parent | 5a952a8365ab5c15b44ff224575f3ebb6fab1988 (diff) |
more WIP
-rw-r--r-- | rpc.bash | 121 |
1 files changed, 98 insertions, 23 deletions
@@ -2,42 +2,117 @@ | |||
2 | set -e | 2 | set -e |
3 | set -o pipefail | 3 | set -o pipefail |
4 | 4 | ||
5 | f() | 5 | max_script_size=$((1024 * 64)) |
6 | |||
7 | main() | ||
8 | { | ||
9 | test -t 0 && NO_TTY= || NO_TTY=y | ||
10 | REMOTE_DEST=localhost | ||
11 | |||
12 | intersection <(extract_words < "$BASH_SOURCE" | sort -u | tee all-extracted-words.txt ) \ | ||
13 | <(difference <( all_commands | sort -u | tee all-commands.txt ) \ | ||
14 | <(remote_run_function all_commands | sort -u | tee all-remote-commands.txt )) | ||
15 | } | ||
16 | |||
17 | all_commands() | ||
18 | { | ||
19 | : main | ||
20 | compgen -A command | ||
21 | } | ||
22 | |||
23 | gudge() | ||
6 | { | 24 | { |
7 | read -p 'Name? ' && | 25 | echo "Hello, $REPLY" |
8 | echo "Hello, $REPLY" | ||
9 | exit | ||
10 | } | 26 | } |
11 | 27 | ||
12 | script=$(declare -f f; echo f) | 28 | fudge() |
29 | { | ||
30 | echo "Don't argue with me about <$*>" | ||
31 | read -p 'Name? ' && gudge "$@" | ||
32 | exit | ||
33 | } | ||
13 | 34 | ||
14 | remote_run_shell_script_stdin() | 35 | remote_run_shell_script_stdin() |
15 | { | 36 | { |
16 | len=$1 | 37 | len=$1 |
17 | [ "$len" -gt 0 ] | 38 | [ "$len" -gt 0 ] |
18 | read -r -N"$len" | 39 | REPLY= |
19 | bash -exc "eval ${REPLY@Q}" | 40 | read -r -N"$len" |
41 | [ "$REPLY" ] | ||
42 | |||
43 | remote_command=(bash -c ${REPLY@Q} "$@") | ||
44 | if [ "$NO_TTY" ] | ||
45 | then | ||
46 | ssh -- "${REMOTE_DEST}" "${remote_command[@]}" | ||
47 | else | ||
48 | ssh -t -- "${REMOTE_DEST}" "${remote_command[@]}" </dev/tty | ||
49 | fi | ||
20 | } | 50 | } |
21 | 51 | ||
22 | remote_run_shell_script_arg1() | 52 | remote_run_shell_script_arg1() |
23 | { | 53 | { |
24 | script=$1 | 54 | script=$1 |
25 | shift | 55 | shift |
26 | (echo -n "$script"; cat) | remote_run_shell_script_stdin "${#script}" "$@" | 56 | (echo -n "$script"; [ ! "$NO_TTY" ] || cat) | remote_run_shell_script_stdin "${#script}" "$@" | tr -d '\015' |
57 | } | ||
58 | |||
59 | intersection() | ||
60 | { | ||
61 | comm -12 "$@" | ||
62 | } | ||
63 | |||
64 | extract_words() | ||
65 | { | ||
66 | grep -o -E '\b\w+\b' | ||
67 | } | ||
68 | |||
69 | immediate_dependencies1() | ||
70 | { | ||
71 | # Thanks Lri from stackoverflow.com/questions/4471364/ | ||
72 | # how-do-i-list-the-functions-defined-in-my-shell/28278090 | ||
73 | intersection \ | ||
74 | <(compgen -A function | sort -u) \ | ||
75 | <(declare -f "$1" | extract_words | sort -u) | ||
76 | } | ||
77 | |||
78 | immediate_dependencies() | ||
79 | { | ||
80 | for f | ||
81 | do | ||
82 | immediate_dependencies1 "$f" | ||
83 | done | sort -u | ||
84 | } | ||
85 | |||
86 | difference() | ||
87 | { | ||
88 | comm -23 "$@" | ||
89 | } | ||
90 | |||
91 | recursive_dependencies() | ||
92 | { | ||
93 | resolved=() | ||
94 | while [ $# -gt 0 ] | ||
95 | do | ||
96 | resolved+=("$@") | ||
97 | set -- $(difference <(immediate_dependencies "$@" | sort -u) \ | ||
98 | <(printf '%s\n' "${resolved[@]}" | sort -u)) | ||
99 | done | ||
100 | printf '%s\n' "${resolved[@]}" | ||
27 | } | 101 | } |
28 | 102 | ||
29 | # Thanks Lri from stackoverflow.com/questions/4471364/ | ||
30 | # how-do-i-list-the-functions-defined-in-my-shell/28278090 | ||
31 | remote_run_function() | 103 | remote_run_function() |
32 | { | 104 | { |
33 | main=$1 | 105 | main=$1 |
34 | shift | 106 | shift |
35 | funcs=$(comm \ | 107 | funcs=$(recursive_dependencies "$main") |
36 | <(declare -f "$main" | grep -o -E '\b\w+\b' | sort -u) \ | 108 | |
37 | <(compgen -A function | sort -u) | 109 | script= |
38 | ) | 110 | read -N${max_script_size} -r script < <( |
39 | script=$(declare -f $funcs; printf '%s "$@";\n' "$main") | 111 | declare -f $funcs |
40 | remote_run_shell_script_arg1 "$script" "$@" | 112 | printf '%s "$@";\n' "$main" |
113 | ) || [ "$script" ] | ||
114 | |||
115 | remote_run_shell_script_arg1 "$script" "$@" | ||
41 | } | 116 | } |
42 | 117 | ||
43 | remote_run_function f | 118 | main "$@" |