|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# YAML parser helper |
| 4 | +# We do some (incomplete) YAML parsing in bash as helper to 04-cloud-secret.sh |
| 5 | +# to extract and construct a conforming clouds.yaml with exactly one |
| 6 | +# openstack entry. |
| 7 | +# This used to be SLOW by using several grep calls (forks) per line. |
| 8 | +# Optimized a bit using three helpers; still going line by line in bash, so expect |
| 9 | +# 2s for 1000 lines of clouds.yaml or so. |
| 10 | +# |
| 11 | +# (c) Kurt Garloff <s7n@garloff.de>, 5/2025 |
| 12 | +# SPDX-License-Identifier: CC-BY-SA-4.0 |
| 13 | + |
| 14 | +# linestart detection |
| 15 | +# $1: linestart to look for |
| 16 | +# $2: string to search in |
| 17 | +startswith() |
| 18 | +{ |
| 19 | + case "$2" in |
| 20 | + "$1"*) |
| 21 | + return 0;; |
| 22 | + esac |
| 23 | + return 1 |
| 24 | +} |
| 25 | + |
| 26 | +# emptyline helper |
| 27 | +# $1: line |
| 28 | +islineempty() |
| 29 | +{ |
| 30 | + local LN |
| 31 | + IFS=" " read LN < <(echo "$1") |
| 32 | + if test -z "$LN"; then return 0; else return 1; fi |
| 33 | +} |
| 34 | + |
| 35 | +# comment helper |
| 36 | +# $1: line |
| 37 | +islinecomment() |
| 38 | +{ |
| 39 | + local LN |
| 40 | + IFS=" " read LN < <(echo "$1") |
| 41 | + if test "${LN:0:1}" = "#"; then return 0; else return 1; fi |
| 42 | +} |
| 43 | + |
| 44 | +# Helper: Parse YAML (recursive) |
| 45 | +# |
| 46 | +# We take two parameters |
| 47 | +# $1: The indentation whitespace |
| 48 | +# $2: "1" => We want more indentation |
| 49 | +# $3-: The keywords |
| 50 | +# |
| 51 | +# Environment to pass special functions |
| 52 | +# $RMVTREE nonempty: Do not output yaml path leading to this section |
| 53 | +# $INSERT and $APPEND is text injected in the outputted block (at beginning and end resp.) |
| 54 | +# $REMOVE is a tag to filter out |
| 55 | +# $RMVCOMMTENT nonempty: Strip comments |
| 56 | +# |
| 57 | +# Return value: 0 if we found (and output) a block, 1 otherwise |
| 58 | +extract_yaml_rec() |
| 59 | +{ |
| 60 | + #echo "DEBUG: Called extract_yaml_rec $@" 1>&2 |
| 61 | + local previndent="$1" |
| 62 | + local more="$2" |
| 63 | + #global LNNO |
| 64 | + shift 2 |
| 65 | + if test -n "$1"; then NOTFOUND=1; fi |
| 66 | + while IFS="" read line; do |
| 67 | + let LNNO+=1 |
| 68 | + # Ignore empty lines |
| 69 | + #if echo "$line" | grep -q '^\s*$'; then continue; fi |
| 70 | + if islineempty "$line"; then continue; fi |
| 71 | + # First line of new block: We need more indentation ... |
| 72 | + if test "$more" = "1"; then |
| 73 | + if ! echo "$line" | grep -q "^$previndent\s"; then return; fi |
| 74 | + more=$(echo "$line" | sed "s/^$previndent\\(\s*\\)[^\s].*\$/\\1/") |
| 75 | + #echo "$previndent$more# $LNNO: New indent level" |
| 76 | + fi |
| 77 | + # Detect less indentation than wanted, return |
| 78 | + #if ! echo "$line" | grep -q "^$previndent$more"; then return; fi |
| 79 | + if ! startswith "$previndent$more" "$line"; then return; fi |
| 80 | + # Strip comments if requested |
| 81 | + #if test -n "$RMVCOMMENT" && echo "$line" | grep -q '^\s*#'; then continue; fi |
| 82 | + if test -n "$RMVCOMMENT" && islinecomment "$line"; then continue; fi |
| 83 | + # OK, we we have at least the indentation level needed |
| 84 | + # 3 cases: |
| 85 | + # (a) We are prior to finding the right block, continue searching |
| 86 | + # (b1) Found it, no more nesting needed, output till the end |
| 87 | + # (b2) Found it, recurse into next keyword |
| 88 | + # (c) We idenitfy the end by finding a different keyword |
| 89 | + # |
| 90 | + # Case b1: No more keywords to look for, we found the previous ones if we |
| 91 | + # got here, just output until the less indentation clause above indicates the end |
| 92 | + if test -z "$1"; then |
| 93 | + #echo "$previndent$more# $LNNO: Outputing block" |
| 94 | + #if test -z "$REMOVE" || ! echo "$line" | grep -q "^$previndent$more$REMOVE:"; then |
| 95 | + if test -z "$REMOVE" || ! startswith "$previndent$more$REMOVE:" "$line"; then |
| 96 | + echo "$line" |
| 97 | + fi |
| 98 | + continue |
| 99 | + fi |
| 100 | + # b2: Search for the keyword |
| 101 | + #if echo "$line" | grep -q "^$previndent$more$1:"; then |
| 102 | + if startswith "$previndent$more$1:" "$line"; then |
| 103 | + #echo "$previndent$more# $LNNO: Found keyword $1" |
| 104 | + # Output tree unless we suppress it |
| 105 | + if test -z "$RMVTREE"; then |
| 106 | + echo "$line" |
| 107 | + else |
| 108 | + # At the leaf, we may hold a value |
| 109 | + if test -z "$2"; then |
| 110 | + echo "$line" | grep --color=never "^$previndent$more$1: [^\\s]" |
| 111 | + fi |
| 112 | + fi |
| 113 | + shift |
| 114 | + # TODO: Reformat INSERT to match |
| 115 | + if test -z "$1"; then |
| 116 | + NOTFOUND=0 |
| 117 | + if test -n "$INSERT"; then echo "$INSERT"; fi |
| 118 | + fi |
| 119 | + extract_yaml_rec "$previndent$more" "1" "$@" |
| 120 | + # TODO: Reformat APPEND to match |
| 121 | + if test -z "$1" -a -n "$APPEND"; then echo "$APPEND"; fi |
| 122 | + # A return here would allows for only one block of a kind |
| 123 | + return $NOTFOUND |
| 124 | + # Otherwise we would have needed to save "$@" adn restore it here |
| 125 | + fi |
| 126 | + # a: OK, just continue to search (without the return above, this is also c) |
| 127 | + done |
| 128 | + return $NOTFOUND |
| 129 | +} |
| 130 | + |
| 131 | +# Helper: extract_yaml |
| 132 | +# $1: The tag to search for and output (separated by dots) |
| 133 | +extract_yaml() |
| 134 | +{ |
| 135 | + LNNO=0 |
| 136 | + SRCH=($(echo "$1" | sed 's/\./ /g')) |
| 137 | + extract_yaml_rec "" "" "${SRCH[@]}" |
| 138 | +} |
| 139 | + |
0 commit comments