mirror of
https://github.com/TheLocehiliosan/yadm
synced 2026-03-02 03:49:29 +00:00
Add support for multiple local classes
A local class is set with:
$ yadm config local.class cls1
More classes can be added with:
$ yadm config --add local.class cls2
$ yadm config --add local.class cls3
Any of cls1, cls2 and cls3 can be used in an alternate condition.
For templates, the existing variable yadm.class/YADM_CLASS is set to
the last class (i.e. cls3) to remain compatible with how it works
today and with what the following command gives:
$ yadm config local.class
For the default template processor there is no explicit yadm.classes
variable. Instead a yadm.class condition will check against all
classes.
For the other processors, a new template variable YADM_CLASSES will be
set to all classes separated by newline. For jinja2 templates a class
can be checked with: {%- if "cls" in YADM_CLASSES.split("\n") %}
For esh templates the logic is a bit more complex, but it is possible
to do.
Fixes #185.
This commit is contained in:
40
yadm
40
yadm
@@ -211,7 +211,7 @@ function score_file() {
|
||||
return
|
||||
fi
|
||||
elif [[ "$label" =~ ^(c|class)$ ]]; then
|
||||
if [ "$value" = "$local_class" ]; then
|
||||
if in_list "$value" "${local_classes[@]}"; then
|
||||
score=$((score + 8))
|
||||
else
|
||||
score=0
|
||||
@@ -418,12 +418,22 @@ function replace_vars() {
|
||||
gsub(("{{" blank "*yadm\\." label blank "*}}"), c[label])
|
||||
}
|
||||
}
|
||||
function condition_helper(label, value) {
|
||||
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value)
|
||||
return sprintf("yadm\\.%s" blank "*==" blank "*\"%s\"", label, value)
|
||||
}
|
||||
function conditions() {
|
||||
pattern = ifs blank "+("
|
||||
for (label in c) {
|
||||
value = c[label]
|
||||
gsub(/[\\.^$(){}\[\]|*+?]/, "\\\\&", value)
|
||||
pattern = sprintf("%syadm\\.%s" blank "*==" blank "*\"%s\"|", pattern, label, value)
|
||||
if (label != "class") {
|
||||
value = c[label]
|
||||
pattern = sprintf("%s%s|", pattern, condition_helper(label, value));
|
||||
}
|
||||
}
|
||||
split(classes, cls_array, "\n")
|
||||
for (idx in cls_array) {
|
||||
value = cls_array[idx]
|
||||
pattern = sprintf("%s%s|", pattern, condition_helper("class", value));
|
||||
}
|
||||
sub(/\|$/, ")" blank "*%}$", pattern)
|
||||
return pattern
|
||||
@@ -439,6 +449,7 @@ EOF
|
||||
-v distro="$local_distro" \
|
||||
-v source="$input" \
|
||||
-v source_dir="$(dirname "$input")" \
|
||||
-v classes="$(join_string $'\n' "${local_classes[@]}")" \
|
||||
"$awk_pgm" \
|
||||
"$input" > "$temp_file" || rm -f "$temp_file"
|
||||
|
||||
@@ -457,6 +468,7 @@ function template_j2cli() {
|
||||
YADM_USER="$local_user" \
|
||||
YADM_DISTRO="$local_distro" \
|
||||
YADM_SOURCE="$input" \
|
||||
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
|
||||
"$J2CLI_PROGRAM" "$input" -o "$temp_file"
|
||||
|
||||
move_file "$input" "$output" "$temp_file"
|
||||
@@ -474,6 +486,7 @@ function template_envtpl() {
|
||||
YADM_USER="$local_user" \
|
||||
YADM_DISTRO="$local_distro" \
|
||||
YADM_SOURCE="$input" \
|
||||
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
|
||||
"$ENVTPL_PROGRAM" --keep-template "$input" -o "$temp_file"
|
||||
|
||||
move_file "$input" "$output" "$temp_file"
|
||||
@@ -484,6 +497,7 @@ function template_esh() {
|
||||
output="$2"
|
||||
temp_file="${output}.$$.$RANDOM"
|
||||
|
||||
YADM_CLASSES="$(join_string $'\n' "${local_classes[@]}")" \
|
||||
"$ESH_PROGRAM" -o "$temp_file" "$input" \
|
||||
YADM_CLASS="$local_class" \
|
||||
YADM_ARCH="$local_arch" \
|
||||
@@ -521,6 +535,7 @@ function alt() {
|
||||
|
||||
# gather values for processing alternates
|
||||
local local_class
|
||||
local -a local_classes
|
||||
local local_arch
|
||||
local local_system
|
||||
local local_host
|
||||
@@ -620,7 +635,12 @@ function remove_stale_links() {
|
||||
|
||||
function set_local_alt_values() {
|
||||
|
||||
local_class="$(config local.class)"
|
||||
local -a all_classes
|
||||
all_classes=$(config --get-all local.class)
|
||||
while IFS='' read -r local_class; do
|
||||
local_classes+=("$local_class")
|
||||
done <<< "$all_classes"
|
||||
local_class="${local_classes[-1]:-}"
|
||||
|
||||
local_arch="$(config local.arch)"
|
||||
if [ -z "$local_arch" ] ; then
|
||||
@@ -2032,6 +2052,16 @@ function join_string {
|
||||
printf "%s" "${*:2}"
|
||||
}
|
||||
|
||||
function in_list {
|
||||
local element="$1"
|
||||
shift
|
||||
|
||||
for e in "$@"; do
|
||||
[[ "$e" = "$element" ]] && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
function get_mode {
|
||||
local filename="$1"
|
||||
local mode
|
||||
|
||||
Reference in New Issue
Block a user