diff --git a/README.md b/README.md index b85b8d3..7b53359 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ (_) ``` -**Z**sh **U**ltimate **P**rogrammer's **E**xtensions **R**efurbished - version 0.1 +**Z**sh **U**ltimate **P**rogrammer's **E**xtensions **R**efurbished - version 0.2 # Introduction @@ -40,12 +40,19 @@ program call `endgame` for a clean exit. Example test program: #!/usr/bin/env zsh # switch on debugging output -debug=1 +DEBUG=1 + # switch on zuper's key/value load/save extension zkv=1 # switch off zuper's consul kv get/set extension unset consul +# switch on zuper's helper extensions +helper=1 +# switch logging into test.log +LOG=test.log + +# load our zuper library source zuper # declare a custom global variable @@ -53,6 +60,7 @@ vars+=(myvar) # assign a default value to our global variable myvar=${myvar:-ok} + # declare a custom function to print it out testfun() { # register the function in the debug flow @@ -71,7 +79,10 @@ testfun() { # but can also be delete earlier here, optionally } -# wrap the init phase +# declare a global associative map +maps+=(mymap) + +# conclude the init phase source zuper.init # register the zdump debug function to be executed on exit @@ -80,8 +91,6 @@ destruens+=(zdump) # call our custom function testfun -# declare an associative array map -typeset -A mymap # we use words and their md5 mymap=( lorem f737a087bca81f69a6048ec744c73e41 @@ -110,6 +119,8 @@ done # Deployment +Here we reference applications where zuper is used succesfully: + - Devuan Simple Development Toolkit https://git.devuan.org/devuan/devuan-sdk#tab-readme - Dowse IoT awareness OS http://dyne.org/software/dowse - Jaro Mail terminal email http://dyne.org/software/jaro-mail diff --git a/zuper b/zuper index 01c8d84..ea2a55c 100644 --- a/zuper +++ b/zuper @@ -327,74 +327,90 @@ EOF } -# optional: define zconsul=1 on source +# optional: define restful=1 on source -[[ "$consul" = "" ]] || { +[[ "$restful" = "" ]] || { ######## - # Consul + # Restful API client # there is a clear zsh optimization here in get/set kv # using zsh/tcp instead of spawning curl # and perhaps querying with one call using ?recursive zmodload zsh/net/tcp - function zconsul.set() { - fn "zconsul.set $*" + function restful.put() { + # $1 = hostname[:port][/path] (default port: 8500) (default path /v1/kv) + # $2 = key + # $3 = value - # checks if consul running up to the caller + fn "restful.put $*" - _host=$1 # ip address - _port=${host[(ws@:@)2]:-8500} - _k=$2 # key name - _v=$3 # value + # to check if the http service is running is up to the caller - req=(_host _port _k _v) + _host=${1} # ip address + _port=${_host[(ws@:@)2]:-8500} + _path=${_path[(ws@:@)3]:/v1/kv} + _host=${_host[(ws@:@)1]} + _k="$2" # key name + _v="$3" # value + + req=(_host _k _v) ckreq || return $? - ztcp $_host $_port || { - zerr - return 1 - } + if ztcp $_host $_port; then - _fd=$REPLY - # func "tcp open on fd $fd" - cat <& $_fd -PUT /v1/kv/$_k HTTP/1.1 + + # TODO: work out various parsers, this one works with consul.io + + _fd=$REPLY + # func "tcp open on fd $fd" + cat <& $_fd +PUT ${_path}/$_k HTTP/1.1 User-Agent: Zuper/$zuper_version -Host: $_host:$_port +Host: ${_host}:${_port} Accept: */* -Content-Length: ${#v} +Content-Length: ${#_v} Content-Type: application/x-www-form-urlencoded EOF - print -n "$v" >& $_fd + print -n "$_v" >& $_fd - sysread -i $_fd _res + sysread -i $_fd _res - # close connection - ztcp -c $_fd + # close connection + ztcp -c $_fd - [[ "$_res" =~ "true" ]] || { - warn "cannot set key/value in consul: $_k = $_v" + [[ "$_res" =~ "true" ]] || { + warn "failed PUT on restful key/value" + warn "endpoint: $_host:$_port/$_path" + warn "resource: $_k = $_v" + zerr + return 1 + } + + else + error "cannot connect to restful service: $_host:$_port" zerr return 1 - } + fi return 0 } - zconsul.get() { - fn "zconsul.get $*" + restful.get() { + fn "restful.get $*" - _host=$1 # ip address - _port=${host[(ws@:@)2]:-8500} + + _host=${1} # ip address + _port=${_host[(ws@:@)2]:-8500} + _path=${_path[(ws@:@)3]:/v1/kv} + _host=${_host[(ws@:@)1]} _k=$2 # key name - _v=$3 # value - req=(_host _port _k _v) + req=(_host _k) ckreq || return $? _k=$1 @@ -406,8 +422,10 @@ EOF _fd=$REPLY + # TODO: work out various parsers, this one works with consul.io + cat <& $_fd -GET /v1/kv/$k HTTP/1.1 +GET ${_path}/$_k HTTP/1.1 User-Agent: Zuper/$zuper_version Host: $_host:$_port Accept: */*