1
0
Fork 0
mirror of https://github.com/ACINQ/eclair.git synced 2025-02-24 22:58:23 +01:00
eclair/eclair-core/eclair-cli

114 lines
3.6 KiB
Bash
Executable file

#!/bin/bash
# default script values, can be overriden for convenience.
api_url='http://localhost:8080'
# uncomment the line below if you don't want to provide a password each time you call eclair-cli
# api_password='your_api_password'
colors=false
# prints help message
usage() {
echo -e "==============================
Command line client for eclair
==============================
This tool requires the eclair node's API to be enabled and listening
on <$api_url>.
Usage
-----
\e[93meclair-cli\e[39m [\e[93mOPTIONS\e[39m]... [\e[93mCOMMAND\e[39m] [--command-param command-value]...
where OPTIONS can be:
-p <password> API's password
-a <address> Override the API URL with <address>
-c Outputs colored JSON
-h Show available commands
and COMMAND is one of:
getinfo, connect, open, close, forceclose, updaterelayfee,
peers, channels, channel, allnodes, allchannels, allupdates,
receive, parseinvoice, findroute, findroutetonode,
send, sendtonode, checkpayment,
audit, networkfees, channelstats
Examples
--------
eclair-cli help display available commands
eclair-cli -a localhost:1234 peers list the peers of a node hosted on localhost:1234
eclair-cli close --channelId 006fb... closes the channel with id 006fb...
Full documentation here: <https://acinq.github.io/apidoc>" 1>&2;
exit 1;
}
# -- script's logic begins here
# Check if jq is installed. If not, display instructions and abort program
command -v jq >/dev/null 2>&1 || { echo -e "This tool requires jq.\nFor installation instructions, visit https://stedolan.github.io/jq/download/.\n\nAborting..."; exit 1; }
# curl installed? If not, give a hint
command -v curl >/dev/null 2>&1 || { echo -e "This tool requires curl.\n\nAborting..."; exit 1; }
# extract script options
while getopts ':cu:p:a:hu:' flag; do
case "${flag}" in
p) api_password="${OPTARG}" ;;
a) api_url="${OPTARG}" ;;
c) colors=true ;;
h) usage ;;
*) ;;
esac
done
shift $(($OPTIND - 1))
# extract api's endpoint (e.g. sendpayment, connect, ...) from params
api_endpoint=${1}
shift 1
# display a usage method if no method given or help requested
if [ -z $api_endpoint ] || [ "$api_endpoint" == "help" ]; then
usage;
fi
# transform long options into a HTTP encoded url body.
api_payload=""
index=1
for arg in "${@}"; do
transformed_arg=""
case ${arg} in
"--"*) # if arg begins with two dashes, it is the name of a parameter. Dashes must be removed, and arg must be followed by an equal sign
# also, it must be prefixed by an '&' sign, if it is not the first argument
if [ $index -eq 1 ]; then
transformed_arg="$transformed_arg${arg:2}="
else
transformed_arg="&$transformed_arg${arg:2}="
fi
;;
*) transformed_arg=$arg
;;
esac
api_payload="$api_payload$transformed_arg";
let "index++"
done;
# jq parses response body for error message
jq_filter='if type=="object" and .error != null then .error else . end'
# jq options
if [ "$colors" = true ]; then
jq_opts="--color-output";
else
jq_opts="--monochrome-output"
fi
# if no password is provided, auth should only contain user login so that curl prompts for the api password
if [ -z $api_password ]; then
auth="eclair-cli";
else
auth="eclair-cli:$api_password";
fi
# we're now ready to execute the API call
eval curl "--user $auth --silent --show-error -X POST -H \"Content-Type: application/x-www-form-urlencoded\" -d '$api_payload' $api_url/$api_endpoint" | jq -r "$jq_opts" "$jq_filter"