dotfiles/Utils/nsh

74 lines
1.5 KiB
Text
Raw Normal View History

2019-08-28 15:59:12 +00:00
#!/bin/bash
#
# This is a nsh script compatible wrapper written uin bash + curl
# Status: Prototype
#
# It is supposed to be fully compatible inside script.
#
# Not working: long option format ex: --clientid
exec_curl() {
local server=$1
local method=$2
shift 2
# -k disable SSL validation h2-master cert seems invalid
# curl: (60) SSL certificate problem: unable to get local issuer certificate
# was a bad certif on h2-master, changed by Rene.
local curlcmd="curl -k -s -H 'Expect:' -F \"apikey=$H2_APIKEY\""
local a v curl_arg
local i=1
local argc=$#
while [[ $i -le $argc ]]
do
# this is index syntax for $@[i]
a=${@:i:1}
# remove dash -a => a
a=${a#-}
v=${@:i+1:1}
if [[ -n $v ]] ; then
curl_arg="-F '$a=$v'"
elif [[ -z $v && $a == 'h' ]] ; then
# help force bool true
# https://gitlab.infomaniak.ch/production/h2-daemon-core/blob/master/nsh/cmdline.c#L62
curl_arg="-F '$a=1'"
else
curl_arg="-F '$a'"
fi
if [[ $v == '-' ]]; then
# file
curl_arg="-F '$a=@-; filename=$a'"
stdin=1
fi
curlcmd+=" $curl_arg"
i=$((i + 2))
done
#local url="https://$server.infomaniak.ch:888/$method"
if [ -z $NSH_PORT ]; then
NSH_PORT=888
fi
if [ -z $NSH_HTTP ]; then
NSH_PROTO=https
else
NSH_PROTO=http
fi
local url="$NSH_PROTO://$server:$NSH_PORT/$method"
curlcmd+=" $url"
if [[ -d ~/tmp/ ]]; then
# debug logging
echo "$(date "+%y%m%d_%H%M%S") $curlcmd" >> ~/tmp/nsh.log
fi
eval "$curlcmd"
}
exec_curl "$@"