[__directory] Add --state exists and --state pre-exists

This commit is contained in:
Dennis Camera 2020-01-15 19:00:30 +01:00
parent 3258fc98e1
commit 2cf44c66d4
2 changed files with 27 additions and 5 deletions

View file

@ -3,6 +3,7 @@
# 2011-2013 Nico Schottelius (nico-cdist at schottelius.org)
# 2013 Steven Armstrong (steven-cdist armstrong.cc)
# 2014 Daniel Heule (hda at sfs.biz)
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
@ -21,8 +22,8 @@
#
destination="/$__object_id"
state_should="$(cat "$__object/parameter/state")"
type="$(cat "$__object/explorer/type")"
state_should=$(cat "$__object/parameter/state")
type=$(cat "$__object/explorer/type")
stat_file="$__object/explorer/stat"
# variable to keep track if we have to set directory attributes
@ -72,7 +73,7 @@ set_mode() {
}
case "$state_should" in
present)
present|exists)
if [ "$type" != "directory" ]; then
set_attributes=1
if [ "$type" != "none" ]; then
@ -83,6 +84,10 @@ case "$state_should" in
fi
echo "mkdir $mkdiropt '$destination'"
echo "create" >> "$__messages_out"
elif [ "$state_should" = 'exists' ]; then
# The type is directory and --state exists. We are done and do not
# check or set the attributes.
exit 0
fi
# Note: Mode - needs to happen last as a chown/chgrp can alter mode by
@ -103,6 +108,12 @@ case "$state_should" in
fi
done
;;
pre-exists)
if [ "$type" != "directory" ]; then
echo "Directory \"$destination\" does not exist" >&2
exit 1
fi
;;
absent)
if [ "$type" = "directory" ]; then
echo "rm -rf '$destination'"

View file

@ -19,7 +19,18 @@ None.
OPTIONAL PARAMETERS
-------------------
state
'present' or 'absent', defaults to 'present'
'present', 'absent', 'exists' or 'pre-exists', defaults to 'present' where:
present
the directory exists and the given attributes are set.
absent
the directory does not exist.
exists
the directory exists, but its attributes are not altered if it already
existed.
pre-exists
check that the directory exists and is indeed a directory, but do not
create or modify it.
group
Group to chgrp to.