From ad736a66d055fc09b476a994e61dbe6fe10fff59 Mon Sep 17 00:00:00 2001 From: Matthias Stecher Date: Tue, 16 Mar 2021 21:38:08 +0100 Subject: [PATCH] __ini_value: remove is_state 'nosuchfile' Because the state 'nosuchfile' is exactly the same as 'absent', it was removed with the code connected to it. It's not important if the file exists or not - because in both cases, it contains no key-value. Also, the code if the explorer returned 'nosuchfile' was wrong: Completly overwrite the file with the assumption the file does not exist is not correct, as it can return for multiple objects of the same file and therefore, they overwrite themself. --- cdist/conf/type/__ini_value/explorer/state | 2 +- cdist/conf/type/__ini_value/gencode-remote | 118 ++++++--------------- 2 files changed, 35 insertions(+), 85 deletions(-) diff --git a/cdist/conf/type/__ini_value/explorer/state b/cdist/conf/type/__ini_value/explorer/state index e387d104..3827a354 100755 --- a/cdist/conf/type/__ini_value/explorer/state +++ b/cdist/conf/type/__ini_value/explorer/state @@ -21,7 +21,7 @@ file="$(cat "$__object/parameter/file")" # abort if no file exist if ! [ -f "$file" ]; then - echo nosuchfile + echo absent exit fi diff --git a/cdist/conf/type/__ini_value/gencode-remote b/cdist/conf/type/__ini_value/gencode-remote index dc6a0431..ab93077e 100755 --- a/cdist/conf/type/__ini_value/gencode-remote +++ b/cdist/conf/type/__ini_value/gencode-remote @@ -24,9 +24,8 @@ file="$(cat "$__object/parameter/file")" # validation check case "$state_should" in present|commented|absent) - if [ "$state_is" != "nosuchfile" ]; then - # Generate the basic awk struct if a file already exists - cat < "\$tmpfile" <<'AWK' SHELL - # generate the awk script and strip unnecessary things - { - # basic functions which everyone needs - cat "$__type/files/common.awk" + # generate the awk script and strip unnecessary things + { + # basic functions which everyone needs + cat "$__type/files/common.awk" - # generate the script - awk -v state="$state_should" ' - function parse(line) { - if(match(line, /^[ \t]*# %code_print%$/) > 0) { - if(state == "present") - print "v_print()" - else if(state == "commented") - print "v_print_commented()" - else - print "print \"script compile error! cdist state " state " unkown!\" > /dev/stderr" - } - else print line + # generate the script + awk -v state="$state_should" ' + function parse(line) { + if(match(line, /^[ \t]*# %code_print%$/) > 0) { + if(state == "present") + print "v_print()" + else if(state == "commented") + print "v_print_commented()" + else + print "print \"script compile error! cdist state " state " unkown!\" > /dev/stderr" } - { - if(match($0, /^[ \t]*# %codeblock_([^%]+)%$/) > 0) { - split($2, result, "_"); type = substr(result[2], 1, length(result[2]) - 1) - file = (ENVIRON["__type"] "/files/parts/" state "/" type ".awk") - while((getline line < file) > 0) - parse(line) - close(file) - } - else print - }' "$__type/files/base.awk" - } | strip_comments + else print line + } + { + if(match($0, /^[ \t]*# %codeblock_([^%]+)%$/) > 0) { + split($2, result, "_"); type = substr(result[2], 1, length(result[2]) - 1) + file = (ENVIRON["__type"] "/files/parts/" state "/" type ".awk") + while((getline line < file) > 0) + parse(line) + close(file) + } + else print + }' "$__type/files/base.awk" + } | strip_comments - # end of here-doc - cat < '%s'\n" "$file" - printf "%s cdist-generated file by %s/%s\n" "$comment_sign" \ - "${__type##*/}" "$__object_id" - echo - - # print values if available - section="$__object/parameter/section" - if [ -f "$section" ]; then - cat "$section" - echo - fi - comment="$__object/parameter/comment" - if [ -f "$comment" ]; then - printf "%s%s " "$i_indent" "$comment_sign" - cat "$comment" - fi - - # print the value - printf "%s%s%s%s%s%s%s\n" \ - "$i_indent" \ - "$(if [ "$state_should" = "commented" ]; then - printf "%s" "$comment_sign"; - fi)" \ - "$key" "$i_del_space" "$delimiter" "$i_del_space" "$value" - echo - - # terminate HERE-DOC - echo "FILE" - fi + # Do not threat it differently if the file does not exist. It's just + # absent. Because multiple explorers can say the file does not exist, + # so the file should not be completly overwritten all times. ;; *)