mirror of https://github.com/dyne/zuper.git
global maps, generic restful client (default mode is still consul), restful fixes
This commit is contained in:
parent
41d24810d3
commit
0690c9881c
21
README.md
21
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
|
||||
|
|
|
|||
72
zuper
72
zuper
|
|
@ -327,49 +327,55 @@ 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
|
||||
|
||||
|
||||
# TODO: work out various parsers, this one works with consul.io
|
||||
|
||||
_fd=$REPLY
|
||||
# func "tcp open on fd $fd"
|
||||
cat <<EOF >& $_fd
|
||||
PUT /v1/kv/$_k HTTP/1.1
|
||||
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
|
||||
|
||||
|
|
@ -377,24 +383,34 @@ EOF
|
|||
ztcp -c $_fd
|
||||
|
||||
[[ "$_res" =~ "true" ]] || {
|
||||
warn "cannot set key/value in consul: $_k = $_v"
|
||||
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 <<EOF >& $_fd
|
||||
GET /v1/kv/$k HTTP/1.1
|
||||
GET ${_path}/$_k HTTP/1.1
|
||||
User-Agent: Zuper/$zuper_version
|
||||
Host: $_host:$_port
|
||||
Accept: */*
|
||||
|
|
|
|||
Loading…
Reference in New Issue