Browse Source

global maps, generic restful client (default mode is still consul), restful fixes

Jaromil 4 years ago
parent
commit
0690c9881c
2 changed files with 69 additions and 40 deletions
  1. 16 5
      README.md
  2. 53 35
      zuper

+ 16 - 5
README.md

@@ -11,7 +11,7 @@
11 11
                            (_)
12 12
 ```
13 13
 
14
-**Z**sh **U**ltimate **P**rogrammer's **E**xtensions **R**efurbished - version 0.1
14
+**Z**sh **U**ltimate **P**rogrammer's **E**xtensions **R**efurbished - version 0.2
15 15
 
16 16
 # Introduction
17 17
 
@@ -40,12 +40,19 @@ program call `endgame` for a clean exit. Example test program:
40 40
 #!/usr/bin/env zsh
41 41
 
42 42
 # switch on debugging output
43
-debug=1
43
+DEBUG=1
44
+
44 45
 # switch on zuper's key/value load/save extension
45 46
 zkv=1
46 47
 # switch off zuper's consul kv get/set extension
47 48
 unset consul
49
+# switch on zuper's helper extensions
50
+helper=1
51
+
52
+# switch logging into test.log
53
+LOG=test.log
48 54
 
55
+# load our zuper library
49 56
 source zuper
50 57
 
51 58
 # declare a custom global variable
@@ -53,6 +60,7 @@ vars+=(myvar)
53 60
 # assign a default value to our global variable
54 61
 myvar=${myvar:-ok}
55 62
 
63
+
56 64
 # declare a custom function to print it out
57 65
 testfun() {
58 66
     # register the function in the debug flow
@@ -71,7 +79,10 @@ testfun() {
71 79
     # but can also be delete earlier here, optionally
72 80
 }
73 81
 
74
-# wrap the init phase
82
+# declare a global associative map
83
+maps+=(mymap)
84
+
85
+# conclude the init phase
75 86
 source zuper.init
76 87
 
77 88
 # register the zdump debug function to be executed on exit
@@ -80,8 +91,6 @@ destruens+=(zdump)
80 91
 # call our custom function
81 92
 testfun
82 93
 
83
-# declare an associative array map
84
-typeset -A mymap
85 94
 # we use words and their md5
86 95
 mymap=(
87 96
     lorem f737a087bca81f69a6048ec744c73e41
@@ -110,6 +119,8 @@ done
110 119
 
111 120
 # Deployment
112 121
 
122
+Here we reference applications where zuper is used succesfully:
123
+
113 124
  - Devuan Simple Development Toolkit https://git.devuan.org/devuan/devuan-sdk#tab-readme
114 125
  - Dowse IoT awareness OS http://dyne.org/software/dowse
115 126
  - Jaro Mail terminal email http://dyne.org/software/jaro-mail

+ 53 - 35
zuper

@@ -327,74 +327,90 @@ EOF
327 327
 
328 328
 }
329 329
 
330
-# optional: define zconsul=1 on source
330
+# optional: define restful=1 on source
331 331
 
332
-[[ "$consul" = "" ]] || {
332
+[[ "$restful" = "" ]] || {
333 333
 
334 334
     ########
335
-    # Consul
335
+    # Restful API client
336 336
     # there is a clear zsh optimization here in get/set kv
337 337
     # using zsh/tcp instead of spawning curl
338 338
     # and perhaps querying with one call using ?recursive
339 339
 
340 340
     zmodload zsh/net/tcp
341 341
 
342
-    function zconsul.set() {
343
-        fn "zconsul.set $*"
342
+    function restful.put() {
343
+        # $1 = hostname[:port][/path] (default port: 8500) (default path /v1/kv)
344
+        # $2 = key
345
+        # $3 = value
344 346
 
345
-        # checks if consul running up to the caller
347
+        fn "restful.put $*"
346 348
 
347
-        _host=$1 # ip address
348
-        _port=${host[(ws@:@)2]:-8500}
349
-        _k=$2 # key name
350
-        _v=$3 # value
349
+        # to check if the http service is running is up to the caller
350
+
351
+        _host=${1} # ip address
352
+        _port=${_host[(ws@:@)2]:-8500}
353
+        _path=${_path[(ws@:@)3]:/v1/kv}
354
+        _host=${_host[(ws@:@)1]}
355
+        _k="$2" # key name
356
+        _v="$3" # value
351 357
 
352
-        req=(_host _port _k _v)
358
+        req=(_host _k _v)
353 359
         ckreq || return $?
354 360
 
355
-        ztcp $_host $_port || {
356
-            zerr
357
-            return 1
358
-        }
361
+        if ztcp $_host $_port; then
359 362
 
360
-        _fd=$REPLY
361
-        #    func "tcp open on fd $fd"
362
-        cat <<EOF >& $_fd
363
-PUT /v1/kv/$_k HTTP/1.1
363
+
364
+            # TODO: work out various parsers, this one works with consul.io
365
+
366
+            _fd=$REPLY
367
+            #    func "tcp open on fd $fd"
368
+            cat <<EOF >& $_fd
369
+PUT ${_path}/$_k HTTP/1.1
364 370
 User-Agent: Zuper/$zuper_version
365
-Host: $_host:$_port
371
+Host: ${_host}:${_port}
366 372
 Accept: */*
367
-Content-Length: ${#v}
373
+Content-Length: ${#_v}
368 374
 Content-Type: application/x-www-form-urlencoded
369 375
 
370 376
 EOF
371 377
 
372
-        print -n "$v" >& $_fd
378
+            print -n "$_v" >& $_fd
373 379
 
374
-        sysread -i $_fd _res
380
+            sysread -i $_fd _res
375 381
 
376
-        # close connection
377
-        ztcp -c $_fd
382
+            # close connection
383
+            ztcp -c $_fd
378 384
 
379
-        [[ "$_res" =~ "true" ]] || {
380
-            warn "cannot set key/value in consul: $_k = $_v"
385
+            [[ "$_res" =~ "true" ]] || {
386
+                warn "failed PUT on restful key/value"
387
+                warn "endpoint: $_host:$_port/$_path"
388
+                warn "resource: $_k = $_v"
389
+                zerr
390
+                return 1
391
+            }
392
+
393
+        else
394
+            error "cannot connect to restful service: $_host:$_port"
381 395
             zerr
382 396
             return 1
383
-        }
397
+        fi
384 398
 
385 399
         return 0
386 400
 
387 401
     }
388 402
 
389
-    zconsul.get() {
390
-        fn "zconsul.get $*"
403
+    restful.get() {
404
+        fn "restful.get $*"
391 405
 
392
-        _host=$1 # ip address
393
-        _port=${host[(ws@:@)2]:-8500}
406
+
407
+        _host=${1} # ip address
408
+        _port=${_host[(ws@:@)2]:-8500}
409
+        _path=${_path[(ws@:@)3]:/v1/kv}
410
+        _host=${_host[(ws@:@)1]}
394 411
         _k=$2 # key name
395
-        _v=$3 # value
396 412
 
397
-        req=(_host _port _k _v)
413
+        req=(_host _k)
398 414
         ckreq || return $?
399 415
 
400 416
         _k=$1
@@ -406,8 +422,10 @@ EOF
406 422
 
407 423
         _fd=$REPLY
408 424
 
425
+        # TODO: work out various parsers, this one works with consul.io
426
+
409 427
         cat <<EOF >& $_fd
410
-GET /v1/kv/$k HTTP/1.1
428
+GET ${_path}/$_k HTTP/1.1
411 429
 User-Agent: Zuper/$zuper_version
412 430
 Host: $_host:$_port
413 431
 Accept: */*