Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Last active January 25, 2024 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaytaylor/ad80d62be9d891f3fdc6d625997f2010 to your computer and use it in GitHub Desktop.
Save jaytaylor/ad80d62be9d891f3fdc6d625997f2010 to your computer and use it in GitHub Desktop.
Multi-line search and inject new variable into helm templates.
#
# Multi-line search and inject new variable into helm templates.
#
# n.b. Works somewhat dynamically.
#
BEGIN {
insertion = "{{- include \"bots.k8s-envvars\" . | indent 8 }}"
expr_seq[0] = " +envFrom: *$"
expr_seq[1] = "^{prefix1}( )?- configMapRef: *$"
expr_seq[2] = "^{prefix1}( )? name: bots-envvars *$"
expr_seq[3] = "^{prefix1}env: *$"
expr_idx = -1
prefix1 = ""
prefix2 = ""
}
# Skip empty and / or comment lines.
/^ *$/ || /^ *#/ {
print $0
next
}
expr_idx == length(expr_seq) {
# Isolate any additional requisite leading spaces.
prefix2 = gensub(/^( *).*$/, "\\1", "g", $0)
print prefix2 insertion
expr_idx = -1
}
{ print $0 }
# First line match.
expr_idx == -1 && $0 ~ expr_seq[0] {
expr_idx = 1
# Match subsequent expr_seq against any leading spaces.
prefix1 = gensub(/^( *)[^ ]+.*$/, "\\1", "g", $0)
# Replace template parameters in expressions.
for (x in expr_seq) {
expr_seq[x] = gensub(/[{]prefix1[}]/, prefix1, "g", expr_seq[x])
}
next
}
# Match next expression.
expr_idx != -1 && $0 ~ expr_seq[expr_idx] {
expr_idx += 1
next
}
# If this bock is reached, reset the state machine.
{
expr_idx = -1
next
}
#
# Multi-line search and inject new variable into helm templates.
#
# n.b. Works somewhat dynamically.
#
BEGIN {
# n.b. valid values: "before" or "after".
match_indent = "before"
#insertion = "{{- include \"bots.k8s-envvars\" . | indent 8 }}"
insertion = "{{- include \"bots.jfr-volume-mount\" . | indent 8 }}"
insertion = "{{- include \"bots.jfr-volume\" . | indent 6 }}"
#expr_seq[0] = " +envFrom: *$"
#expr_seq[1] = "^{prefix1}( )?- configMapRef: *$"
#expr_seq[2] = "^{prefix1}( )? name: bots-envvars *$"
#expr_seq[3] = "^{prefix1}env: *$"
expr_seq[0] = "^ +[{][{]- include \"bots\\.log-volume-mount\""
expr_seq[0] = "^ +[{][{]- include \"bots\\.log-volume\""
expr_idx = -1
prefix1 = ""
prefix2 = ""
done = 0
}
function do_insert() {
if (expr_idx == length(expr_seq)) {
# Isolate any additional requisite leading spaces.
prefix2 = gensub(/^( *).*$/, "\\1", "g", $0)
if (match_indent == "after") {
printf prefix2
} else {
printf prefix1
}
print insertion
expr_idx = -1
}
}
# Skip empty and / or comment lines.
/^ *$/ || /^ *#/ {
print $0
next
}
do_insert()
{ print $0 }
# First line match.
expr_idx == -1 && $0 ~ expr_seq[0] {
expr_idx = 1
# Match subsequent expr_seq against any leading spaces.
prefix1 = gensub(/^( *)[^ ]+.*$/, "\\1", "g", $0)
# Replace template parameters in expressions.
for (x in expr_seq) {
expr_seq[x] = gensub(/[{]prefix1[}]/, prefix1, "g", expr_seq[x])
}
next
}
# Match next expression.
expr_idx != -1 && $0 ~ expr_seq[expr_idx] {
expr_idx += 1
next
}
# If this bock is reached, reset the state machine.
{
expr_idx = -1
next
}
function f() {
print "HI"
}
END {
if (done == 0) {
do_insert()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment