Browse Source

more string helpers, uppercased some flags and wrapped exit-on-error via throw/catch

Jaromil 5 years ago
parent
commit
704f6fdd6f
1 changed files with 105 additions and 18 deletions
  1. 105 18
      zuper

+ 105 - 18
zuper

@@ -24,11 +24,15 @@
24 24
 ##########################
25 25
 typeset -aU vars
26 26
 typeset -aU arrs
27
-vars=(debug quiet ztmpfile)
27
+vars=(DEBUG QUIET ztmpfile)
28 28
 arrs=(req freq)
29 29
 
30
-debug=${debug:-0}
31
-quiet=${quiet:-0}
30
+# reset list of destructors
31
+destruens=()
32
+
33
+# global execution flags
34
+DEBUG=${DEBUG:-0}
35
+QUIET=${QUIET:-0}
32 36
 
33 37
 vars+=(zuper_version)
34 38
 zuper_version=0.1
@@ -93,33 +97,33 @@ function _msg() {
93 97
 function _message say act() {
94 98
     local notice="message"
95 99
     [[ "$1" = "-n" ]] && shift && notice="inline"
96
-    [[ $quiet = 1 ]] || _msg "$notice" $@
100
+    [[ $QUIET = 1 ]] || _msg "$notice" $@
97 101
     return 0
98 102
 }
99 103
 
100 104
 function _verbose xxx func() {
101
-    [[ $debug = 1 ]] && _msg verbose $@
105
+    [[ $DEBUG = 1 ]] && _msg verbose $@
102 106
     return 0
103 107
 }
104 108
 
105 109
 function _success yes notice() {
106
-    [[ $quiet = 1 ]] || _msg success $@
110
+    [[ $QUIET = 1 ]] || _msg success $@
107 111
     return 0
108 112
 }
109 113
 
110 114
 function _warning no warn warning() {
111
-    [[ $quiet = 1 ]] || _msg warning $@
115
+    [[ $QUIET = 1 ]] || _msg warning $@
112 116
     return 0
113 117
 }
114 118
 
115 119
 function _failure fatal die error() {
116 120
     #    typeset -i exitcode=${exitv:-1}
117
-    [[ $quiet = 1 ]] || _msg failure $@
121
+    [[ $QUIET = 1 ]] || _msg failure $@
118 122
     return 1
119 123
 }
120 124
 
121 125
 function _print() {
122
-    [[ $quiet = 1 ]] || _msg print $@
126
+    [[ $QUIET = 1 ]] || _msg print $@
123 127
     return 0
124 128
 }
125 129
 
@@ -151,7 +155,7 @@ ckreq reqck() {
151 155
     return $err
152 156
 }
153 157
 
154
-zerr() { 
158
+zerr() {
155 159
     error "error in: ${fun:-$last_notice}"
156 160
 
157 161
     [[ "$last_func" = "" ]] || warn "called in: $last_func"
@@ -172,7 +176,10 @@ zdump() {
172 176
     done
173 177
 }
174 178
 
175
-
179
+# handy wrappers for throw/catch execution of blocks where we need the
180
+# program to exit on any error (non-zero) returned by any function
181
+throw() { function TRAPZERR() { zerr; return 1 } }
182
+catch() { function TRAPZERR() { } }
176 183
 
177 184
 ##########################
178 185
 # Endgame handling
@@ -248,7 +255,7 @@ destruens+=(_ztmp_destructor)
248 255
     # load a map from a file
249 256
     # map must be already instantiated with typeset -A by called
250 257
     # name of map is defined inside the file
251
-    zkv.load() {
258
+    function zkv.load() {
252 259
         fn "zkv-load $*"
253 260
 
254 261
         file=$1
@@ -271,7 +278,7 @@ destruens+=(_ztmp_destructor)
271 278
     # save a map in a file
272 279
     # $1 = name of the map associative array
273 280
     # $2 = full path to the file
274
-    zkv.save() {
281
+    function zkv.save() {
275 282
         fn "zkv.save $*"
276 283
 
277 284
         _map=$1
@@ -293,7 +300,7 @@ destruens+=(_ztmp_destructor)
293 300
         _karr=(${(Pk)_map})
294 301
         _varr=(${(Pv)_map})
295 302
         _num="${#_karr}"
296
-        for c in {1..$_num}; do        
303
+        for c in {1..$_num}; do
297 304
             # can also be cat here, however for speed we use builtins
298 305
             # switch to cat if compatibility is an issue
299 306
             sysread -o 1 <<EOF >> $_path
@@ -317,9 +324,9 @@ EOF
317 324
 
318 325
     zmodload zsh/net/tcp
319 326
 
320
-    zconsul.set() {
327
+    function zconsul.set() {
321 328
         fn "zconsul.set $*"
322
-        
329
+
323 330
         # checks if consul running up to the caller
324 331
 
325 332
         _host=$1 # ip address
@@ -362,7 +369,7 @@ EOF
362 369
 
363 370
         return 0
364 371
 
365
-    }    
372
+    }
366 373
 
367 374
     zconsul.get() {
368 375
         fn "zconsul.get $*"
@@ -371,7 +378,7 @@ EOF
371 378
         _port=${host[(ws@:@)2]:-8500}
372 379
         _k=$2 # key name
373 380
         _v=$3 # value
374
-        
381
+
375 382
         req=(_host _port _k _v)
376 383
         ckreq || return $?
377 384
 
@@ -402,3 +409,83 @@ EOF
402 409
     }
403 410
 
404 411
 }
412
+
413
+# {{{ Helpers
414
+[[ "$helpers" = "" ]] || {
415
+
416
+    # remote leading and trailing spaces in a string taken from stdin
417
+    function trim() {
418
+        sed -e 's/^[[:space:]]*//g ; s/[[:space:]]*\$//g'
419
+    }
420
+
421
+    zmodload zsh/mapfile
422
+    # faster substitute for cat
423
+    function printfile() {
424
+        print ${mapfile[$1]}
425
+    }
426
+
427
+    # extract all emails found in a text from stdin
428
+    # outputs them one per line
429
+    function extract_emails() {
430
+        awk '{ for (i=1;i<=NF;i++)
431
+     if ( $i ~ /[[:alnum:]]@[[:alnum:]]/ ) {
432
+       gsub(/<|>|,/ , "" , $i); print $i } }'
433
+    }
434
+
435
+
436
+    zmodload zsh/regex
437
+    # takes a string as argument, returns success if is an email
438
+    function isemail() {
439
+        [[ "$1" -regex-match "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" ]] && return 0
440
+
441
+        return 1
442
+    }
443
+
444
+    # takes a numeric argument and prints out a human readable size
445
+    function human_size() {
446
+        [[ $1 -gt 0 ]] || {
447
+            error "human_size() called with invalid argument"
448
+            return 1
449
+        }
450
+
451
+        # we use the binary operation for speed
452
+        # shift right 10 is divide by 1024
453
+
454
+        # gigabytes
455
+        [[ $1 -gt 1073741824 ]] && {
456
+            print -n "$(( $1 >> 30 )) GB"
457
+            return 0
458
+        }
459
+
460
+        # megabytes
461
+        [[ $1 -gt 1048576 ]] && {
462
+            print -n "$(( $1 >> 20 )) MB"
463
+            return 0
464
+        }
465
+        # kilobytes
466
+        [[ $1 -gt 1024 ]] && {
467
+            print -n "$(( $1 >> 10 )) KB"
468
+            return 0
469
+        }
470
+        # bytes
471
+        print -n "$1 Bytes"
472
+        return 0
473
+    }
474
+
475
+
476
+    # strips out all html/xml tags (everything between < >)
477
+    function xml_strip html_strip() { sed 's/<[^>]\+>//g' }
478
+
479
+    # changes stdin string special chars to be shown in html
480
+    function escape_html() {
481
+        sed -e '
482
+s/\&/\&amp;/g
483
+s/>/\&gt;/g
484
+s/</\&lt;/g
485
+s/"/\&quot;/g
486
+'
487
+    }
488
+
489
+}
490
+
491
+# }}} Helpers