Created
April 29, 2020 19:52
-
-
Save tflori/fcc17c9d54ae6f4d646e9c9de55f3f00 to your computer and use it in GitHub Desktop.
Simple yaml setter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IFS='.' | |
read -ra keyToSet <<< $1 | |
commentPattern="^ *(#|$)" | |
current=${keyToSet[0]} | |
keyToSet=("${keyToSet[@]:1}") | |
currentIndent=0 | |
currentPattern="^ {${currentIndent}}${current} *:" | |
nextIndent=0 | |
while read line; do | |
if [[ $line =~ $commentPattern ]]; then | |
echo $line | |
continue | |
fi | |
leadingSpaces=$(echo $line | awk -F'[^ ]' '{print length($1)}') | |
if [[ $nextIndent -gt $currentIndent ]]; then | |
if [[ $leadingSpaces -ge $nextIndent ]]; then | |
currentIndent=$leadingSpaces | |
currentPattern="^ {${currentIndent}}${current} *:" | |
else | |
# we left the current object - no way we find the rest of the key | |
echo $line | |
break; | |
fi | |
fi | |
if [[ $line =~ $currentPattern ]]; then | |
if [[ ${#keyToSet[@]} -eq "0" ]]; then | |
echo $(echo $line|egrep -o $currentPattern) $2 | |
break; | |
fi | |
current=${keyToSet[0]} | |
keyToSet=("${keyToSet[@]:1}") | |
nextIndent=$(( $currentIndent + 1 )) | |
fi | |
echo $line | |
done | |
# output the rest | |
while read line; do | |
echo $line | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment