Browse Source

config section parser

Jaromil 4 years ago
parent
commit
fd6bd41585
1 changed files with 70 additions and 8 deletions
  1. 70 8
      zuper

+ 70 - 8
zuper

@@ -314,7 +314,7 @@ EOF
314 314
 
315 315
 # optional: define zconsul=1 on source
316 316
 
317
-[[ "$zconsul" = "" ]] || {
317
+[[ "$consul" = "" ]] || {
318 318
 
319 319
     ########
320 320
     # Consul
@@ -414,19 +414,19 @@ EOF
414 414
 [[ "$helpers" = "" ]] || {
415 415
 
416 416
     # remote leading and trailing spaces in a string taken from stdin
417
-    function trim() {
417
+    function helper.trim trim() {
418 418
         sed -e 's/^[[:space:]]*//g ; s/[[:space:]]*\$//g'
419 419
     }
420 420
 
421 421
     zmodload zsh/mapfile
422 422
     # faster substitute for cat
423
-    function printfile() {
423
+    function helper.printfile printfile() {
424 424
         print ${mapfile[$1]}
425 425
     }
426 426
 
427 427
     # extract all emails found in a text from stdin
428 428
     # outputs them one per line
429
-    function extract_emails() {
429
+    function helper.extract-emails extract_emails() {
430 430
         awk '{ for (i=1;i<=NF;i++)
431 431
      if ( $i ~ /[[:alnum:]]@[[:alnum:]]/ ) {
432 432
        gsub(/<|>|,/ , "" , $i); print $i } }'
@@ -435,14 +435,14 @@ EOF
435 435
 
436 436
     zmodload zsh/regex
437 437
     # takes a string as argument, returns success if is an email
438
-    function isemail() {
438
+    function helper.isemail isemail() {
439 439
         [[ "$1" -regex-match "\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b" ]] && return 0
440 440
 
441 441
         return 1
442 442
     }
443 443
 
444 444
     # takes a numeric argument and prints out a human readable size
445
-    function human_size() {
445
+    function helper.human-size human_size() {
446 446
         [[ $1 -gt 0 ]] || {
447 447
             error "human_size() called with invalid argument"
448 448
             return 1
@@ -474,10 +474,10 @@ EOF
474 474
 
475 475
 
476 476
     # strips out all html/xml tags (everything between < >)
477
-    function xml_strip html_strip() { sed 's/<[^>]\+>//g' }
477
+    function helper.html-strip xml_strip html_strip() { sed 's/<[^>]\+>//g' }
478 478
 
479 479
     # changes stdin string special chars to be shown in html
480
-    function escape_html() {
480
+    function helper.excape-html escape_html() {
481 481
         sed -e '
482 482
 s/\&/\&amp;/g
483 483
 s/>/\&gt;/g
@@ -489,3 +489,65 @@ s/"/\&quot;/g
489 489
 }
490 490
 
491 491
 # }}} Helpers
492
+
493
+# {{{ Config
494
+
495
+vars+=(config_section_type)
496
+arrs+=(config_section)
497
+config_section_type=org-mode
498
+
499
+config.section.type() {
500
+    fn config.section.type
501
+    _type=$1
502
+    req=(_type)
503
+    ckreq || return $?
504
+
505
+    case $_type in
506
+        org-mode)
507
+            config_section_type=org-mode
508
+            ;;
509
+        *)
510
+            error "Unknown config type:$_type"
511
+            return 1
512
+            ;;
513
+    esac
514
+
515
+    act "$_type config section parser initialized"
516
+    return 0
517
+
518
+}
519
+
520
+# fills in contents of section in array config_section
521
+config.section.read() {
522
+    fn config.section.read
523
+    _file=$1
524
+    _section=$2
525
+    req=(_file _section)
526
+    freq=($_file)
527
+    ckreq || return $?
528
+
529
+    case $config_section_type in
530
+        org-mode)
531
+            _contents=`awk '
532
+BEGIN { found=0 }
533
+/^#\+ '"$_section"'/ { found=1; next }
534
+/^#\+/ { if(found==1) exit 0 }
535
+/^$/ { next }
536
+{ if(found==1) print $0 }
537
+' $_file`
538
+
539
+            ;;
540
+        *)
541
+            error "Unknown config type:$_type"
542
+            ;;
543
+    esac
544
+
545
+    config_section=()
546
+    for c in ${(f)_contents}; do
547
+        config_section+=("$c")
548
+    done
549
+    return 0
550
+
551
+}
552
+
553
+# }}}