implemented some suggestions from steven

This commit is contained in:
Daniel Heule 2014-02-27 14:45:19 +01:00
parent d1c55a9acb
commit 8d80e52de9
6 changed files with 124 additions and 30 deletions

View file

@ -27,6 +27,11 @@ file="$(cat "$__object/parameter/file")"
export delimiter="$(cat "$__object/parameter/delimiter")" export delimiter="$(cat "$__object/parameter/delimiter")"
export value="$(cat "$__object/parameter/value" 2>/dev/null \ export value="$(cat "$__object/parameter/value" 2>/dev/null \
|| echo "__CDIST_NOTSET__")" || echo "__CDIST_NOTSET__")"
if [ -f "$__object/parameter/exact_delimiter" ]; then
export exact_delimiter=1
else
export exact_delimiter=0
fi
awk -f - "$file" <<"AWK_EOF" awk -f - "$file" <<"AWK_EOF"
BEGIN { BEGIN {
@ -34,27 +39,48 @@ BEGIN {
key=ENVIRON["key"] key=ENVIRON["key"]
delimiter=ENVIRON["delimiter"] delimiter=ENVIRON["delimiter"]
value=ENVIRON["value"] value=ENVIRON["value"]
keydel=key delimiter exact_delimiter=ENVIRON["exact_delimiter"]
line=keydel value
found=0 found=0
} }
# enter the main loop # enter the main loop
{ {
i = index($0,keydel) i = index($0,key)
if(i == 1) { 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
next
}
if( exact_delimiter == 1) {
# we have key and delimiter, but since additional spaces are not alowed
# return wrongformat
found=1
print "wrongformat"
exit
}
}
found=1 found=1
if(state == "absent") { if(state == "absent") {
# on state absent, only the ocurance is relevant, so exit here # on state absent, only the ocurance is relevant, so exit here
print "present" print "present"
exit exit
} }
linevalue=substr(delval,delpos + length(delimiter))
if(exact_delimiter == 0){
#ok, now strip tabs and whitespaces at the beginning of the value
sub(/[ \t]*/,"",linevalue)
}
# Key with separator found # Key with separator found
if($0 == line) { if(linevalue == value) {
# exact match found, so state is present # exact match found, so state is present
print "present" print "present"
} }
else { else {
# not value is wrong ...
print "wrongvalue" print "wrongvalue"
} }
exit exit

View file

@ -21,21 +21,49 @@
# #
#set -x #set -x
key="$__object_id"
[ -f "$__object/parameter/key" ] && key="$(cat "$__object/parameter/key")"
state_should="$(cat "$__object/parameter/state")" state_should="$(cat "$__object/parameter/state")"
file="$(cat "$__object/parameter/file")"
state_is="$(cat "$__object/explorer/state")" state_is="$(cat "$__object/explorer/state")"
[ "$state_is" = "$state_should" ] && exit 0 if [ "$state_is" = "$state_should" ]; then
exit 0
fi
# here we check only if the states are valid, let awk do the work ... 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,
# emmit messages and
# let awk do the work ...
case "$state_should" in case "$state_should" in
absent|present) absent)
case "$state_is" in case "$state_is" in
absent|wrongvalue|present) absent)
# nothing to do
;;
wrongformat|wrongvalue|present)
echo removed >> "$__messages_out"
;;
*)
echo "Unknown explorer state: $state_is" >&2
exit 1
esac
present)
case "$state_is" in
absent)
echo inserted >> "$__messages_out"
;;
wrongformated|wrongvalue)
echo changed >> "$__messages_out"
;;
present)
# nothing to do
;; ;;
*) *)
echo "Unknown explorer state: $state_is" >&2 echo "Unknown explorer state: $state_is" >&2
@ -56,16 +84,20 @@ export key="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER"
$key $key
__CDIST_INPUT_END_HERE_MARKER __CDIST_INPUT_END_HERE_MARKER
)" )"
export delimiter="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER"
$(cat "$__object/parameter/delimiter")
__CDIST_INPUT_END_HERE_MARKER
)"
export value="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER" export value="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER"
$(cat "$__object/parameter/value") $(cat "$__object/parameter/value")
__CDIST_INPUT_END_HERE_MARKER __CDIST_INPUT_END_HERE_MARKER
)" )"
export delimiter="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER"
$(cat "$__object/parameter/delimiter")
__CDIST_INPUT_END_HERE_MARKER
)"
export exact_delimiter="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER"
$exact_delimiter
__CDIST_INPUT_END_HERE_MARKER
)"
export comment="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER" export comment="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER"
$(cat "$__object/parameter/comment_line") $(cat "$__object/parameter/comment")
__CDIST_INPUT_END_HERE_MARKER __CDIST_INPUT_END_HERE_MARKER
)" )"
@ -80,17 +112,33 @@ BEGIN {
delimiter=ENVIRON["delimiter"] delimiter=ENVIRON["delimiter"]
value=ENVIRON["value"] value=ENVIRON["value"]
comment=ENVIRON["comment"] comment=ENVIRON["comment"]
keydel=key delimiter exact_delimiter=ENVIRON["exact_delimiter"]
line=keydel value
inserted=0 inserted=0
ll="" ll=""
llpopulated=0 llpopulated=0
line=key delimiter value
} }
# enter the main loop # 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 dont use regex, this is by design, so we can match against every value without special meanings of chars ...
i = index(\$0,keydel) i = index(\$0,key)
if(i == 1) { 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(state == "absent") {
if(ll == comment) { if(ll == comment) {
# if comment is present, clear llpopulated flag # if comment is present, clear llpopulated flag
@ -136,4 +184,5 @@ END {
} }
AWK_EOF AWK_EOF
mv -f "\$tmpfile" "$file" mv -f "\$tmpfile" "$file"
exit 1
__CDIST_HEREDOC_END_HERE_MARKER __CDIST_HEREDOC_END_HERE_MARKER

View file

@ -31,9 +31,28 @@ key::
The key to change. Defaults to object_id. The key to change. Defaults to object_id.
value:: value::
The value for the key. Optional if state=absent, required otherwise. The value for the key. Optional if state=absent, required otherwise.
comment_line:: comment::
If supplied, the comment line is inserted before the line with key and value, If supplied, the value will be inserted before the line with the key,
but only if key or value is about to be changed. but only if the key or value must be changed.
You need to ensure yourself that the line is prefixed with the correct
comment sign. (for example # or ; or wathever ..)
BOOLEAN PARAMETERS
------------------
exact_delimiter::
If supplied, thread additional whitespaces between key, delimiter and value
as wrong value.
MESSAGES
--------
removed::
Line with key was removed
inserted::
A new line was inserted
changed::
An existing line was changed
EXAMPLES EXAMPLES
@ -58,11 +77,9 @@ __key_value LEGACY_KEY --file /etc/somefile --state absent --delimiter '='
MORE INFORMATION MORE INFORMATION
---------------- ----------------
This type does not use regex to avoid quoting problems. This type try to handle as many values as possible, so it doen't use regexes.
So you need to specify the key and delimiteri exactly. So you need to exatly specify the key and delimiter. Delimiter can be of any lenght.
Delimiter can be one or more characters. Due to shell limitations, we have some values which you can't use, this values are:
Due to shell limitations, we have some values which can not be used.
These values are:
__CDIST_HEREDOC_END_HERE_MARKER __CDIST_HEREDOC_END_HERE_MARKER
__CDIST_INPUT_END_HERE_MARKER __CDIST_INPUT_END_HERE_MARKER

View file

@ -0,0 +1 @@
exact_delimiter

View file

@ -0,0 +1 @@

View file

@ -1,4 +1,4 @@
key key
value value
state state
comment_line comment