bugfix for whitespace stripping, its no more simple

This commit is contained in:
Daniel Heule 2014-03-21 09:35:47 +01:00
parent 807e2902ea
commit b7fb973fa5
3 changed files with 107 additions and 122 deletions

View file

@ -21,8 +21,8 @@
export key="$(cat "$__object/parameter/key" 2>/dev/null \ export key="$(cat "$__object/parameter/key" 2>/dev/null \
|| echo "$__object_id")" || echo "$__object_id")"
export state="$(cat "$__object/parameter/state" 2>/dev/null \ export state="$(cat "$__object/parameter/state")"
|| echo "present")"
file="$(cat "$__object/parameter/file")" file="$(cat "$__object/parameter/file")"
if [ ! -f "$file" ]; then if [ ! -f "$file" ]; then

View file

@ -0,0 +1,104 @@
#!/bin/sh
export key="$(cat "$__object/parameter/key" 2>/dev/null \
|| echo "$__object_id")"
export state="$(cat "$__object/parameter/state")"
file="$(cat "$__object/parameter/file")"
export delimiter="$(cat "$__object/parameter/delimiter")"
export value="$(cat "$__object/parameter/value" 2>/dev/null \
|| echo "__CDIST_NOTSET__")"
if [ -f "$__object/parameter/exact_delimiter" ]; then
export exact_delimiter=1
else
export exact_delimiter=0
fi
tmpfile=$(mktemp "${file}.cdist.XXXXXXXXXX")
# preserve ownership and permissions by copying existing file over tmpfile
if [ -f "$file" ]; then
cp -p "$file" "$tmpfile"
else
touch "$file"
fi
awk -f - "$file" >"$tmpfile" <<"AWK_EOF"
BEGIN {
# import variables in a secure way ..
state=ENVIRON["state"]
key=ENVIRON["key"]
delimiter=ENVIRON["delimiter"]
value=ENVIRON["value"]
comment=ENVIRON["comment"]
exact_delimiter=ENVIRON["exact_delimiter"]
inserted=0
ll=""
llpopulated=0
line=key delimiter value
}
# enter the main loop
{
# I dont use regex, this is by design, so we can match against every value without special meanings of chars ...
i = index($0,key)
if(i == 1) {
delval = substr($0,length(key)+1)
delpos = index(delval,delimiter)
if(delpos > 1) {
spaces = substr(delval,1,delpos-1)
sub(/[ \t]*/,"",spaces)
if( length(spaces) > 0 ) {
# if there are not only spaces between key and delimiter,
# continue since we we are on the wrong line
if(llpopulated == 1) {
print ll
}
ll=$0
llpopulated=1
next
}
}
if(state == "absent") {
if(ll == comment) {
# if comment is present, clear llpopulated flag
llpopulated=0
}
# if absent, simple yump over this line
next
}
else {
# if comment is present and not present in last line
if (llpopulated == 1) {
print ll
if( comment != "" && ll != comment) {
print comment
}
llpopulated=0
}
inserted=1
# state is present, so insert correct line here
print line
ll=line
next
}
}
else {
if(llpopulated == 1) {
print ll
}
ll=$0
llpopulated=1
}
}
END {
if(llpopulated == 1) {
print ll
}
if(inserted == 0 && state == "present" ) {
if(comment != "" && ll != comment){
print comment
}
print line
}
}
AWK_EOF
mv -f "$tmpfile" "$file"

View file

@ -28,15 +28,6 @@ if [ "$state_is" = "$state_should" ]; then
exit 0 exit 0
fi fi
file="$(cat "$__object/parameter/file")"
key="$__object_id"
[ -f "$__object/parameter/key" ] && key="$(cat "$__object/parameter/key")"
if [ -f "$__object/parameter/exact_delimiter" ]; then
export exact_delimiter=1
else
export exact_delimiter=0
fi
# here we check only if the states are valid, # here we check only if the states are valid,
# emmit messages and # emmit messages and
# let awk do the work ... # let awk do the work ...
@ -81,114 +72,4 @@ case "$state_should" in
;; ;;
esac esac
cat <<__CDIST_HEREDOC_END_HERE_MARKER cat "$__type/files/remote_script.sh"
IFS='\n' read -r state <<'__CDIST_INPUT_END_HERE_MARKER'
$state_should
__CDIST_INPUT_END_HERE_MARKER
export state
IFS='\n' read -r key <<'__CDIST_INPUT_END_HERE_MARKER'
$key
__CDIST_INPUT_END_HERE_MARKER
export key
IFS='\n' read -r value <<'__CDIST_INPUT_END_HERE_MARKER'
$(cat "$__object/parameter/value")
__CDIST_INPUT_END_HERE_MARKER
export value
IFS='\n' read -r delimiter <<'__CDIST_INPUT_END_HERE_MARKER'
$(cat "$__object/parameter/delimiter")
__CDIST_INPUT_END_HERE_MARKER
export delimiter
IFS='\n' read -r comment <<'__CDIST_INPUT_END_HERE_MARKER'
$(cat "$__object/parameter/comment")
__CDIST_INPUT_END_HERE_MARKER
export comment
export exact_delimiter="$exact_delimiter"
tmpfile=\$(mktemp "${file}.cdist.XXXXXXXXXX")
# preserve ownership and permissions by copying existing file over tmpfile
if [ -f "$file" ]; then
cp -p "$file" "\$tmpfile"
else
touch "$file"
fi
awk -f - "$file" >"\$tmpfile" <<"AWK_EOF"
BEGIN {
# import variables in a secure way ..
state=ENVIRON["state"]
key=ENVIRON["key"]
delimiter=ENVIRON["delimiter"]
value=ENVIRON["value"]
comment=ENVIRON["comment"]
exact_delimiter=ENVIRON["exact_delimiter"]
inserted=0
ll=""
llpopulated=0
line=key delimiter value
}
# enter the main loop
{
# I dont use regex, this is by design, so we can match against every value without special meanings of chars ...
i = index(\$0,key)
if(i == 1) {
delval = substr(\$0,length(key)+1)
delpos = index(delval,delimiter)
if(delpos > 1) {
spaces = substr(delval,1,delpos-1)
sub(/[ \t]*/,"",spaces)
if( length(spaces) > 0 ) {
# if there are not only spaces between key and delimiter,
# continue since we we are on the wrong line
if(llpopulated == 1) {
print ll
}
ll=\$0
llpopulated=1
next
}
}
if(state == "absent") {
if(ll == comment) {
# if comment is present, clear llpopulated flag
llpopulated=0
}
# if absent, simple yump over this line
next
}
else {
# if comment is present and not present in last line
if (llpopulated == 1) {
print ll
if( comment != "" && ll != comment) {
print comment
}
llpopulated=0
}
inserted=1
# state is present, so insert correct line here
print line
ll=line
next
}
}
else {
if(llpopulated == 1) {
print ll
}
ll=\$0
llpopulated=1
}
}
END {
if(llpopulated == 1) {
print ll
}
if(inserted == 0 && state == "present" ) {
if(comment != "" && ll != comment){
print comment
}
print line
}
}
AWK_EOF
mv -f "\$tmpfile" "$file"
__CDIST_HEREDOC_END_HERE_MARKER