[__line/state] Logic fixes in explorer

This commit fixes the incorrectly reported state "wrongposition" if position is
"after" and anchor is present in the file but the line missing.
This commit is contained in:
Dennis Camera 2020-01-15 17:54:40 +01:00
parent 4cdb8aaa03
commit 51b1b11cc2

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.
# #
@ -19,7 +20,7 @@
# #
if [ -f "$__object/parameter/file" ]; then if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")" file=$(cat "$__object/parameter/file")
else else
file="/$__object_id" file="/$__object_id"
fi fi
@ -55,41 +56,45 @@ function _find(_text, _pattern) {
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
exit 0
} }
else { } else if (_find($0, pattern)) {
state = "wrongposition" found_line++
}
exit 0
} }
} } 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
exit 0
} }
else {
state = "wrongposition"
}
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"