Skip to content

Instantly share code, notes, and snippets.

@jlongman
Last active May 31, 2024 17:08
Show Gist options
  • Save jlongman/8261ceaf387c6e3222f5f0ed692d5613 to your computer and use it in GitHub Desktop.
Save jlongman/8261ceaf387c6e3222f5f0ed692d5613 to your computer and use it in GitHub Desktop.
CDK without Tags: awk script to hide Tag-only changes in CDK
#!/usr/bin/env
# strict mode
set -euo pipefail
IFS=$'\n\t'
# from https://github.com/aws/aws-cdk/issues/25742
# why am i debugging^Wworking in awk
format_cdk_diff() {
DEBUGGING=$1;
awk 'BEGIN{ inTagBlock=0; previousLine=""; debugging='$DEBUGGING'; }
{
if (debugging){print "pre " previousLine " / " $0;}
if (intTagBlock = 1) {
if (debugging){
print "exit? : " (!(previousLine ~ /^ .. \[~] Tags/)) " && \
(" (previousLine ~ /^\[~]/) " || "( previousLine ~ /^ .. \[~]/ )" || "( previousLine == "") ")";
print "why : " previousLine;
}
if ( \
!(previousLine ~ /^ .. \[~] Tags/) && \
(previousLine ~ /^\[~]/ || previousLine ~ /^ .. \[~]/ || previousLine == "") \
)
{
if (debugging){print "exit";}
inTagBlock=0;
}
}
if (inTagBlock == 0 && (previousLine ~ /\[~] (AWS|Custom)::/ && $0 ~ /Tags/ || $0 ~ /Tags/ ))
{
if (debugging){print "enter";}
print previousLine " / " $0;
inTagBlock=1;
}
if (inTagBlock == 0) {
if (debugging){
print "show : " previousLine;
} else {
print previousLine;
}
} else if (debugging) {
print "hide : " previousline;
}
if (debugging){print "line " inTagBlock " " $0;}
previousLine=$0;
}'
}
# actually, permit unset $2 so we can do `all` case
set +u
DEBUGGING=$1
shift
echo '"'$*'"'
# note color breaks the matching
cdk diff $* 2>&1 >/dev/null | format_cdk_diff $DEBUGGING
@jlongman
Copy link
Author

jlongman commented May 31, 2024

no debug version

#!/usr/bin/env
# strict mode
set -euo pipefail
IFS=$'\n\t'

# from https://github.com/aws/aws-cdk/issues/25742
format_cdk_diff() {
  
    awk 'BEGIN{ inTagBlock=0; previousLine=""; }
    {
        if (intTagBlock = 1) {
          if ( \
              !(previousLine ~ /^ .. \[~] Tags/) && \
              (previousLine ~ /^\[~]/ || previousLine ~ /^ .. \[~]/ || previousLine == "") \
              )
          {
              inTagBlock=0;
          }
        }
        if (inTagBlock == 0 && (previousLine ~ /\[~] (AWS|Custom)::/ && $0 ~ /Tags/ || $0 ~ /Tags/ ))
        {
            print previousLine " / " $0;
            inTagBlock=1;
        }
        if (inTagBlock == 0) {
            print previousLine;
        }
        previousLine=$0;
    }'
}
# actually, permit unset so we can do `all` case
set +u
echo '"'$@'"'
cdk diff $@ 2>&1 >/dev/null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment