[__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
1 changed files with 23 additions and 18 deletions

View File

@ -1,6 +1,7 @@
#!/bin/sh -e
#
# 2018 Steven Armstrong (steven-cdist at armstrong.cc)
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
@ -19,7 +20,7 @@
#
if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")"
file=$(cat "$__object/parameter/file")
else
file="/$__object_id"
fi
@ -55,41 +56,45 @@ function _find(_text, _pattern) {
BEGIN {
getline anchor < (ENVIRON["__object"] "/parameter/" position)
getline pattern < (ENVIRON["__object"] "/parameter/" needle)
state = "absent"
found_line = 0
correct_pos = (position != "after" && position != "before")
}
{
if (position == "after") {
if (match($0, anchor)) {
getline
if (_find($0, pattern)) {
state = "present"
found_line++
correct_pos = 1
exit 0
}
else {
state = "wrongposition"
}
exit 0
} else if (_find($0, pattern)) {
found_line++
}
}
else if (position == "before") {
} else if (position == "before") {
if (_find($0, pattern)) {
found_line++
getline
if (match($0, anchor)) {
state = "present"
correct_pos = 1
exit 0
}
else {
state = "wrongposition"
}
exit 0
}
}
else {
} else {
if (_find($0, pattern)) {
state = "present"
found_line++
exit 0
}
}
}
END {
print state
if (found_line && correct_pos) {
print "present"
} else if (found_line) {
print "wrongposition"
} else {
print "absent"
}
}
' "$file"