Commit 51b1b11c authored by ssrq's avatar ssrq

[__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.
parent 4cdb8aaa
#!/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"
}
else {
state = "wrongposition"
found_line++
correct_pos = 1
exit 0
}
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"
}
else {
state = "wrongposition"
correct_pos = 1
exit 0
}
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"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment