cdist/cdist/conf/library/manifest

104 lines
2.6 KiB
Bash

# vi: set filetype=sh:
# $__library/manifest
#
# 2020 Matthias Stecher (matthiasstecher at gmx.de)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
# This library contains:
# common helper functions helping writing the manifest
# check if type is a singleton
#
# Arguments:
# 1: name of the type, e.g. `__some_type`
is_singleton() {
if [ -f "$__global/conf/type/$1/singleton" ]; then
return 0
else
return 1
fi
}
# print object id
#
# Arguments:
# 1: type name, e.g. `__some_type`
# 2: object id; will be ignored if type is a singleton
get_object_id() {
if is_singleton "$1"; then
printf "%s" "$1"
else
printf "%s/%s" "$1" "$2"
fi
}
# Helper function to get the correct chain name. It will shorten any
# invalid character.
#
# Arguments:
# 1: the dependency chain name
_rec_normalize_chain_name() {
# spaces will be underscores
# anything other than a-zA-Z0-9 will be removed
printf "$1" | sed 's/\([[:space:]]\|-\)/_/g; s/[^a-zA-Z0-9_]//g'
}
# Recording dependencies
#
# Arguments:
# 1: the dependency chain - only chars, numbers and underscores allowed
# 2..n: the type command; no more nesting possible
rec_requires() {
# make it ready to be passed to eval
_rec_chain="$(_rec_normalize_chain_name "$1")"
eval "_rec_ch_${_rec_chain}=\"\${_rec_ch_${_rec_chain}:-} $(get_object_id "$2" "$3")\""
# execute type
shift
"$@"
}
# Clearing dependency chains
#
# Arguments:
# 1: the dependency chain name
rec_clear() {
_rec_chain="$(printf "$1" | sed 's/\([[:space:]]\|-\)/_/g; s/[^a-zA-Z0-9_]//g')"
eval "unset -v _rec_ch_${_rec_chain}"
}
# Export dependencies to current command by appending to the current environment
#
# Arguments:
# 1: the dependency chain
# 2..n: the type command; no more nesting possible
depend() {
# assemble dependency
_rec_chain="$(_rec_normalize_chain_name "$1")"
_rec_deps="$(eval "printf \"%s\" \"\${_rec_ch_${_rec_chain}:-}\"")"
# execute type
shift
require="$require $_rec_deps" "$@"
}