implemented some suggestions from steven
This commit is contained in:
parent
d1c55a9acb
commit
8d80e52de9
6 changed files with 124 additions and 30 deletions
|
@ -27,6 +27,11 @@ 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
|
||||
|
||||
awk -f - "$file" <<"AWK_EOF"
|
||||
BEGIN {
|
||||
|
@ -34,27 +39,48 @@ BEGIN {
|
|||
key=ENVIRON["key"]
|
||||
delimiter=ENVIRON["delimiter"]
|
||||
value=ENVIRON["value"]
|
||||
keydel=key delimiter
|
||||
line=keydel value
|
||||
exact_delimiter=ENVIRON["exact_delimiter"]
|
||||
found=0
|
||||
}
|
||||
# enter the main loop
|
||||
{
|
||||
i = index($0,keydel)
|
||||
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
|
||||
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
|
||||
if(state == "absent") {
|
||||
# on state absent, only the ocurance is relevant, so exit here
|
||||
print "present"
|
||||
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
|
||||
if($0 == line) {
|
||||
if(linevalue == value) {
|
||||
# exact match found, so state is present
|
||||
print "present"
|
||||
}
|
||||
else {
|
||||
# not value is wrong ...
|
||||
print "wrongvalue"
|
||||
}
|
||||
exit
|
||||
|
|
|
@ -21,21 +21,49 @@
|
|||
#
|
||||
#set -x
|
||||
|
||||
key="$__object_id"
|
||||
[ -f "$__object/parameter/key" ] && key="$(cat "$__object/parameter/key")"
|
||||
|
||||
state_should="$(cat "$__object/parameter/state")"
|
||||
|
||||
file="$(cat "$__object/parameter/file")"
|
||||
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
|
||||
absent|present)
|
||||
absent)
|
||||
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
|
||||
|
@ -56,16 +84,20 @@ export key="\$(cat <<"__CDIST_INPUT_END_HERE_MARKER"
|
|||
$key
|
||||
__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"
|
||||
$(cat "$__object/parameter/value")
|
||||
__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"
|
||||
$(cat "$__object/parameter/comment_line")
|
||||
$(cat "$__object/parameter/comment")
|
||||
__CDIST_INPUT_END_HERE_MARKER
|
||||
)"
|
||||
|
||||
|
@ -80,17 +112,33 @@ BEGIN {
|
|||
delimiter=ENVIRON["delimiter"]
|
||||
value=ENVIRON["value"]
|
||||
comment=ENVIRON["comment"]
|
||||
keydel=key delimiter
|
||||
line=keydel value
|
||||
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,keydel)
|
||||
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
|
||||
|
@ -136,4 +184,5 @@ END {
|
|||
}
|
||||
AWK_EOF
|
||||
mv -f "\$tmpfile" "$file"
|
||||
exit 1
|
||||
__CDIST_HEREDOC_END_HERE_MARKER
|
||||
|
|
|
@ -31,9 +31,28 @@ key::
|
|||
The key to change. Defaults to object_id.
|
||||
value::
|
||||
The value for the key. Optional if state=absent, required otherwise.
|
||||
comment_line::
|
||||
If supplied, the comment line is inserted before the line with key and value,
|
||||
but only if key or value is about to be changed.
|
||||
comment::
|
||||
If supplied, the value will be inserted before the line with the key,
|
||||
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
|
||||
|
@ -58,11 +77,9 @@ __key_value LEGACY_KEY --file /etc/somefile --state absent --delimiter '='
|
|||
|
||||
MORE INFORMATION
|
||||
----------------
|
||||
This type does not use regex to avoid quoting problems.
|
||||
So you need to specify the key and delimiteri exactly.
|
||||
Delimiter can be one or more characters.
|
||||
Due to shell limitations, we have some values which can not be used.
|
||||
These values are:
|
||||
This type try to handle as many values as possible, so it doen't use regexes.
|
||||
So you need to exatly specify the key and delimiter. Delimiter can be of any lenght.
|
||||
Due to shell limitations, we have some values which you can't use, this values are:
|
||||
__CDIST_HEREDOC_END_HERE_MARKER
|
||||
__CDIST_INPUT_END_HERE_MARKER
|
||||
|
||||
|
|
1
cdist/conf/type/__key_value/parameter/boolean
Normal file
1
cdist/conf/type/__key_value/parameter/boolean
Normal file
|
@ -0,0 +1 @@
|
|||
exact_delimiter
|
1
cdist/conf/type/__key_value/parameter/default/comment
Normal file
1
cdist/conf/type/__key_value/parameter/default/comment
Normal file
|
@ -0,0 +1 @@
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
key
|
||||
value
|
||||
state
|
||||
comment_line
|
||||
comment
|
||||
|
|
Loading…
Reference in a new issue