Merge branch 'ssrq/cdist-line-patches'

This commit is contained in:
Darko Poljak 2020-01-17 18:07:14 +01:00
commit ffb3c003ca
2 changed files with 46 additions and 36 deletions

View file

@ -1,6 +1,7 @@
#!/bin/sh -e #!/bin/sh -e
# #
# 2018 Steven Armstrong (steven-cdist at armstrong.cc) # 2018 Steven Armstrong (steven-cdist at armstrong.cc)
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -18,6 +19,14 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>. # along with cdist. If not, see <http://www.gnu.org/licenses/>.
# #
if [ -f "$__object/parameter/file" ]; then
file=$(cat "$__object/parameter/file")
else
file="/$__object_id"
fi
[ -f "$file" ] || exit 0
if [ -f "$__object/parameter/before" ]; then if [ -f "$__object/parameter/before" ]; then
position="before" position="before"
elif [ -f "$__object/parameter/after" ]; then elif [ -f "$__object/parameter/after" ]; then
@ -33,63 +42,56 @@ else
needle="line" needle="line"
fi fi
if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")"
else
file="/$__object_id"
fi
if [ ! -f "$file" ]; then
echo "file_missing"
exit 0
fi
awk -v position="$position" -v needle="$needle" ' awk -v position="$position" -v needle="$needle" '
function _find(_text, _pattern) { function _find(_text, _pattern) {
if (needle == "regex") { if (needle == "regex") {
return match(_text, _pattern) return match(_text, _pattern)
} else { } else {
return index(_text, _pattern) return index(_text, _pattern) == 1
} }
} }
BEGIN { BEGIN {
getline anchor < (ENVIRON["__object"] "/parameter/" position) getline anchor < (ENVIRON["__object"] "/parameter/" position)
getline pattern < (ENVIRON["__object"] "/parameter/" needle) getline pattern < (ENVIRON["__object"] "/parameter/" needle)
state = "absent"
found_line = 0
correct_pos = (position != "after" && position != "before")
} }
{ {
if (position == "after") { if (position == "after") {
if (match($0, anchor)) { if (match($0, anchor)) {
getline getline
if (_find($0, pattern)) { if (_find($0, pattern)) {
state = "present" found_line++
} correct_pos = 1
else {
state = "wrongposition"
}
exit 0 exit 0
} }
} else if (_find($0, pattern)) {
found_line++
} }
else if (position == "before") { } else if (position == "before") {
if (_find($0, pattern)) { if (_find($0, pattern)) {
found_line++
getline getline
if (match($0, anchor)) { if (match($0, anchor)) {
state = "present" correct_pos = 1
}
else {
state = "wrongposition"
}
exit 0 exit 0
} }
} }
else { } else {
if (_find($0, pattern)) { if (_find($0, pattern)) {
state = "present" found_line++
exit 0 exit 0
} }
} }
} }
END { END {
print state if (found_line && correct_pos) {
print "present"
} else if (found_line) {
print "wrongposition"
} else {
print "absent"
}
} }
' "$file" ' "$file"

View file

@ -1,6 +1,7 @@
#!/bin/sh -e #!/bin/sh -e
# #
# 2018 Steven Armstrong (steven-cdist at armstrong.cc) # 2018 Steven Armstrong (steven-cdist at armstrong.cc)
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
# #
# This file is part of cdist. # This file is part of cdist.
# #
@ -23,9 +24,20 @@ if [ -f "$__object/parameter/before" ] && [ -f "$__object/parameter/after" ]; th
exit 1 exit 1
fi fi
if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")"
else
file="/$__object_id"
fi
state_should="$(cat "$__object/parameter/state")" state_should="$(cat "$__object/parameter/state")"
state_is="$(cat "$__object/explorer/state")" state_is="$(cat "$__object/explorer/state")"
if [ -z "$state_is" ]; then
printf 'The file "%s" is missing. Please create it before using %s on it.\n' "$file" "${__type##*/}" >&2
exit 1
fi
if [ "$state_should" = "$state_is" ]; then if [ "$state_should" = "$state_is" ]; then
# nothing to do # nothing to do
exit 0 exit 0
@ -46,12 +58,6 @@ else
needle="line" needle="line"
fi fi
if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")"
else
file="/$__object_id"
fi
add=0 add=0
remove=0 remove=0
case "$state_should" in case "$state_should" in
@ -104,10 +110,12 @@ BEGIN {
if (anchor && match(\$0, anchor)) { if (anchor && match(\$0, anchor)) {
if (position == "before") { if (position == "before") {
print line print line
add = 0
print print
} else if (position == "after") { } else if (position == "after") {
print print
print line print line
add = 0
} }
next next
} }
@ -115,7 +123,7 @@ BEGIN {
print print
} }
END { END {
if (add && position == "end") { if (add) {
print line print line
} }
} }