-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightning-cli
executable file
·99 lines (73 loc) · 2.11 KB
/
lightning-cli
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
#!/bin/bash
set -eu
cd "$(dirname "$0")"
HTTP=false
# Check if curl is installed
if ! command -v curl &> /dev/null; then
echo "curl couldn't be found. Please install it to continue."
exit 1
fi
ENV_FILE="$(pwd)/.env"
if [ ! -f "$ENV_FILE" ]; then
echo "WARNING: you don't have a '.env' file specified. We created one for you. Fill it out!"
cat <<EOF > "$ENV_FILE"
RUNE=
REST_ENDPOINT=
EOF
chmod +x "$ENV_FILE"
exit 1
fi
# source the rune/rest endpoint
source "$ENV_FILE"
if [ -z "$RUNE" ]; then
echo "ERROR: You MUST set the RUNE in your .env"
exit 1
fi
if [ -z "$REST_ENDPOINT" ]; then
echo "ERROR: You MUST set the REST_ENDPOINT in your .env"
exit 1
fi
COMMAND_TO_EXECUTE=
IS_PARAMTERIZED=false
COMMAND_TO_EXECUTE="$1"
if [ -z "$COMMAND_TO_EXECUTE" ]; then
echo "ERROR: Please provide a command to execute."
exit 1
fi
if [ -n "${2:-}" ]; then
IS_PARAMTERIZED=true
fi
if [ "$IS_PARAMTERIZED" = true ] && [ -z "${3:-}" ]; then
echo "ERROR: You MUST provide key=value pairs when specifying a paramemtersized command."
exit 1
fi
json_object="{}"
# TODO - ensure everything aft erh -k is in the key=value format
if [ "$IS_PARAMTERIZED" = true ]; then
# Initialize an empty JSON object
json_object="{"
# Loop through each command line argument
for KEY_VALUE_PAIR in "$@"; do
# Split the argument into key and value using '=' as delimiter
IFS='=' read -ra parts <<< "$KEY_VALUE_PAIR"
# Check if the argument is in key=value format
if [ "${#parts[@]}" -eq 2 ]; then
key="${parts[0]}"
value="${parts[1]}"
# Add the key-value pair to the JSON object
json_object+="\"$key\":\"$value\", "
fi
done
json_object="${json_object%,*}}"
fi
PROTOCOL=https
if [ "$HTTP" = true ]; then
PROTOCOL="http"
fi
RESPONSE=$(curl --silent -X 'POST' \
"$PROTOCOL://$REST_ENDPOINT/v1/$COMMAND_TO_EXECUTE" \
-H 'accept: application/json' \
-H "Rune: $RUNE" \
-H 'Content-Type: application/json' \
-d "$json_object")
echo "$RESPONSE"