__ini_value: moved awk script generation to gencode-remote
The script generation of the awk script was moved from `files/gen-awk.sh` to `gencode-remote` because the size not really matters. If the file does not exist, it will be created by a predefined template via an here-doc to avoid a big awk-script where it is not needed.
This commit is contained in:
parent
7380fcaaf9
commit
e3e4a91abe
2 changed files with 108 additions and 67 deletions
|
@ -1,65 +0,0 @@
|
||||||
#!/bin/sh -e
|
|
||||||
# __ini_value/files/gen-awk.sh
|
|
||||||
|
|
||||||
# Generates the awk script that will modify the line
|
|
||||||
#
|
|
||||||
# Arguments:
|
|
||||||
# 1: the file to modify
|
|
||||||
# 2: the should_state
|
|
||||||
|
|
||||||
strip_comments() {
|
|
||||||
grep -v '^[[:space:]]*\($\|#\)'
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
file="$1"
|
|
||||||
state="$2"
|
|
||||||
|
|
||||||
|
|
||||||
# Generate the basic awk struct
|
|
||||||
cat <<SHELL
|
|
||||||
tmpfile="\$(mktemp '${file}.cdist.XXXXXXXX')"
|
|
||||||
if [ -f '$file' ]; then
|
|
||||||
cp -p '$file' "\$tmpfile"
|
|
||||||
fi
|
|
||||||
|
|
||||||
awk -f - '$file' > "\$tmpfile" <<'AWK'
|
|
||||||
SHELL
|
|
||||||
|
|
||||||
|
|
||||||
# generate the awk script and strip unnecessary things
|
|
||||||
{
|
|
||||||
# basic functions which everyone needs
|
|
||||||
cat "$__type/files/common.awk"
|
|
||||||
|
|
||||||
# generate the script and strip
|
|
||||||
awk -v state="$state" '
|
|
||||||
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
|
|
||||||
}
|
|
||||||
{
|
|
||||||
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 <<SHELL
|
|
||||||
AWK
|
|
||||||
mv -f "\$tmpfile" '$file'
|
|
||||||
SHELL
|
|
|
@ -1,5 +1,14 @@
|
||||||
#!/bin/sh -e
|
#!/bin/sh -e
|
||||||
# __ini_value/gencode-remote
|
# __ini_value/gencode-remote
|
||||||
|
#
|
||||||
|
# Generates the code. It will generate an AWK script to add, modify or remove
|
||||||
|
# the line. The script differ in some points depend on the state. If the file
|
||||||
|
# does not exist, it will only generate the script without the awk overhead.
|
||||||
|
|
||||||
|
# strip comments and newlines for a tighter script
|
||||||
|
strip_comments() {
|
||||||
|
grep -v '^[[:space:]]*\($\|#\)'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
state_is="$(cat "$__object/explorer/state")"
|
state_is="$(cat "$__object/explorer/state")"
|
||||||
|
@ -15,8 +24,105 @@ file="$(cat "$__object/parameter/file")"
|
||||||
# validation check
|
# validation check
|
||||||
case "$state_should" in
|
case "$state_should" in
|
||||||
present|commented|absent)
|
present|commented|absent)
|
||||||
# FIXME no need for a seperate file?
|
if [ "$state_is" != "nosuchfile" ]; then
|
||||||
"$__type/files/gen-awk.sh" "$file" "$state_should"
|
# Generate the basic awk struct if a file already exists
|
||||||
|
cat <<SHELL
|
||||||
|
tmpfile="\$(mktemp '${file}.cdist.XXXXXXXX')"
|
||||||
|
if [ -f '$file' ]; then
|
||||||
|
cp -p '$file' "\$tmpfile"
|
||||||
|
fi
|
||||||
|
|
||||||
|
awk -f - '$file' > "\$tmpfile" <<'AWK'
|
||||||
|
SHELL
|
||||||
|
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
{
|
||||||
|
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 <<SHELL
|
||||||
|
AWK
|
||||||
|
mv -f "\$tmpfile" '$file'
|
||||||
|
SHELL
|
||||||
|
|
||||||
|
# state_is 'nosuchfile' is the same as state_should 'absent'
|
||||||
|
elif [ "$state_should" != "absent" ]; then
|
||||||
|
# generate the indentation string
|
||||||
|
spaces() {
|
||||||
|
_i=$1
|
||||||
|
while [ $_i -gt 0 ]; do
|
||||||
|
printf " "
|
||||||
|
_i=$((_i - 1))
|
||||||
|
done
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
# get values required for the comment field
|
||||||
|
key="$(cat "$__object/parameter/key")"
|
||||||
|
delimiter="$(cat "$__object/parameter/delimiter")"
|
||||||
|
value="$(cat "$__object/parameter/value")"
|
||||||
|
indentation="$(cat "$__object/parameter/indentation" || true)"
|
||||||
|
i_indent="$(spaces $indentation)"
|
||||||
|
delimiter_space="$(cat "$__object/parameter/delimiter-space" || true)"
|
||||||
|
i_del_space="$(spaces $delimiter_space)"
|
||||||
|
comment_sign="$(cat "$__object/parameter/comment-sign" | cut -c1)"
|
||||||
|
|
||||||
|
# Generate a simple shell script which just generates the file
|
||||||
|
printf "cat <<'FILE' > '%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
|
||||||
;;
|
;;
|
||||||
|
|
||||||
*)
|
*)
|
||||||
|
|
Loading…
Reference in a new issue