-
Notifications
You must be signed in to change notification settings - Fork 0
/
click
executable file
·236 lines (205 loc) · 5.08 KB
/
click
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
#!/usr/bin/env bash
# ==============================================================================
# click
#
# Thin client for interacting with running Urbit ship via conn.c.
#
# Compiles a Hoon command given as text against an optional list of
# dependencies and then executes it on a running ship.
#
# The click-format helper script handles command formatting, whereas this
# script handles operational control flow. See the documentation in
# click-format for the details of command structure, dependency management,
# etc.
#
# ==============================================================================
# ==========================================================
# CONSTANTS
# ==========================================================
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SCRIPT_NAME=$(basename "$0")
CLICK_CMD="$SCRIPT_DIR/click-format"
# ==========================================================
# VARIABLES
# ==========================================================
MIN_INPUT=0
STATUS=0
EXECUTE=0
FILTER_GOOF=0
JAM_HEX=0
JAM_ONLY=0
KHAN_TED=0
EVAL_CMD=''
INPUT=''
OUTPUT=''
# ==========================================================
# FUNCTIONS
# ==========================================================
show_help() {
cat >&2 <<EOF
Usage:
$SCRIPT_NAME [options] <path-to-pier> <hoon> [<dependencies> ...]
$SCRIPT_NAME [options] -i <path-to-file> <path-to-pier> [<dependencies> ...]
$SCRIPT_NAME [-o|-p] -e -i <path-to-file> <path-to-pier>
Thin client for interacting with running Urbit ship via conn.c
options:
-b <path-to-vere> Specify an undocked vere binary
-e Execute jammed Hoon
-h Show usage info
-i <path-to-file> Read input from file
-j Jam only
-k Execute command using "khan-eval" thread
-o <path-to-file> Output to file
-p Filter failure stack traces from result and pretty-print them to stderr
-x Jam to hex
EOF
}
full() {
if [[ $FILTER_GOOF -ne 0 ]]; then
CUE_OPTS="$CUE_OPTS -k"
fi
$CLICK_CMD "$HOON" $DEPS |
$EVAL_CMD eval --jam $JAM_OPTS |
nc -U -W 1 "$PIER/.urb/conn.sock" |
$EVAL_CMD eval --cue $CUE_OPTS
}
jam() {
if [[ $JAM_HEX -ne 0 ]]; then
JAM_OPTS=""
fi
$CLICK_CMD "$HOON" $DEPS | $EVAL_CMD eval --jam $JAM_OPTS
}
execute() {
if [[ $FILTER_GOOF -ne 0 ]]; then
CUE_OPTS="$CUE_OPTS -k"
fi
nc -U "$PIER/.urb/conn.sock" < $INPUT |
$EVAL_CMD eval --cue $CUE_OPTS
}
# ==========================================================
# MAIN
# ==========================================================
# reset getopts
OPTIND=1
# parse options
while getopts ":b:ehi:jko:px" OPT; do
case "$OPT" in
b)
EVAL_CMD=$OPTARG
;;
e)
EXECUTE=1
;;
i)
INPUT=$OPTARG
;;
j)
JAM_ONLY=1
;;
k)
KHAN_TED=1
;;
o)
OUTPUT=$OPTARG
;;
p)
FILTER_GOOF=1
;;
x)
JAM_HEX=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
STATUS=1
;;
h)
show_help
exit 0
;;
esac
done
# check for opt issues
if [[ $EXECUTE -ne 0 ]]; then
if [[ $JAM_ONLY -ne 0 ]]; then
echo "Invalid option: cannot mix -e and -j" >&2
STATUS=1
fi
if [[ -z $INPUT ]]; then
echo "Invalid option: -e requires -i" >&2
STATUS=1
fi
if [[ $KHAN_TED -ne 0 ]]; then
echo "-k meaningless with -e; ignoring" >&2
fi
fi
if [[ $JAM_ONLY -ne 0 ]]; then
if [[ $FILTER_GOOF -ne 0 ]]; then
echo "-p meaningless with -j; ignoring" >&2
fi
else
if [[ $JAM_HEX -ne 0 ]]; then
echo "Invalid option: -x requires -j" >&2
STATUS=1
fi
fi
# handle errors
if [[ $STATUS -ne 0 ]]; then
show_help
exit "$STATUS"
fi
# skip over opts after parsing
shift $((OPTIND-1))
# check for mandatory input
if [[ -n $INPUT ]]; then
MIN_INPUT=1
else
MIN_INPUT=2
fi
if [[ $# -lt $MIN_INPUT ]]; then
echo "ERROR: missing input" >&2
show_help
exit 1
fi
# read args
if [[ -n $INPUT ]]; then
PIER=$1
shift
if [[ $EXECUTE -eq 0 ]]; then
# read from file, escape 's, replace newlines w/ gaps
HOON="$(cat $INPUT | sed "s;';\\\\';g" | sed ':a;N;s/\n/ /;$!ba')"
fi
else
PIER=$1
HOON=$2
shift 2
fi
DEPS=$*
# setup shared variables
if [[ $KHAN_TED -ne 0 ]]; then
CLICK_CMD="$CLICK_CMD -k"
fi
# use docked binary
if [[ -z $EVAL_CMD ]]; then
EVAL_CMD="$PIER/.run"
fi
JAM_OPTS="-n"
CUE_OPTS="-n"
TMP_OUT=`mktemp -q --tmpdir`
# main logic
{
if [[ $EXECUTE -ne 0 ]]; then
execute
elif [[ $JAM_ONLY -ne 0 ]]; then
jam
else
full
fi
} > $TMP_OUT
# route output
if [[ -n $OUTPUT ]]; then
mv $TMP_OUT $OUTPUT
else
cat $TMP_OUT
fi
# done
exit 0