Commit ffb3c003 authored by Darko Poljak's avatar Darko Poljak

Merge branch 'ssrq/cdist-line-patches'

parents e292c9f1 5a9a1ba5
Pipeline #585 passed with stage
in 57 seconds
#!/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.
#
......@@ -18,6 +19,14 @@
# 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
position="before"
elif [ -f "$__object/parameter/after" ]; then
......@@ -33,63 +42,56 @@ else
needle="line"
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" '
function _find(_text, _pattern) {
if (needle == "regex") {
return match(_text, _pattern)
} else {
return index(_text, _pattern)
return index(_text, _pattern) == 1
}
}
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"
#!/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.
#
......@@ -23,9 +24,20 @@ if [ -f "$__object/parameter/before" ] && [ -f "$__object/parameter/after" ]; th
exit 1
fi
if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")"
else
file="/$__object_id"
fi
state_should="$(cat "$__object/parameter/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
# nothing to do
exit 0
......@@ -46,12 +58,6 @@ else
needle="line"
fi
if [ -f "$__object/parameter/file" ]; then
file="$(cat "$__object/parameter/file")"
else
file="/$__object_id"
fi
add=0
remove=0
case "$state_should" in
......@@ -104,10 +110,12 @@ BEGIN {
if (anchor && match(\$0, anchor)) {
if (position == "before") {
print line
add = 0
print
} else if (position == "after") {
print
print line
add = 0
}
next
}
......@@ -115,7 +123,7 @@ BEGIN {
print
}
END {
if (add && position == "end") {
if (add) {
print line
}
}
......
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