Skip to content

Instantly share code, notes, and snippets.

@tflori
Created April 29, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tflori/fcc17c9d54ae6f4d646e9c9de55f3f00 to your computer and use it in GitHub Desktop.
Save tflori/fcc17c9d54ae6f4d646e9c9de55f3f00 to your computer and use it in GitHub Desktop.
Simple yaml setter
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