From 76bc9546152e6d0026a26df49480f02a2a801f1c Mon Sep 17 00:00:00 2001 From: Matthias Stecher Date: Tue, 28 Jul 2020 18:29:03 +0200 Subject: [PATCH] Added sample library script for manifest dependency handling This adds a draft of a library shell script which can be sourced to utilise all functions. Can be extended with various checks, which are not implemented yet. Original, it was done so there is a file in the global library directory that no errors occur. This should fix the unit tests. --- cdist/conf/library/manifest | 103 ++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 cdist/conf/library/manifest diff --git a/cdist/conf/library/manifest b/cdist/conf/library/manifest new file mode 100644 index 00000000..2592beec --- /dev/null +++ b/cdist/conf/library/manifest @@ -0,0 +1,103 @@ +# 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 . +# +# +# 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" "$@" +}