summaryrefslogtreecommitdiff
path: root/src/lib/config.print.source.sh
blob: ead6fb3744f00acf424f74889c0d581199eac98a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
print_shell_assignment() {
    local -r name="$1"; shift
    local -r value="$1"; shift

    printf '%s=%q\n' "$name" "$value"
}

print_shell_array_assignment() {
    local -r name="$1"; shift
    local value

    printf '%s=(' "$name"
    for value in "$@"; do
        printf ' %q' "$value"
    done
    printf ' )\n'
}

# Emit the effective config as a re-sourceable block, driven by CONFIG_SPECS
# (task mr0): the field set and their print order are the registry order, and
# each field's print_kind facet selects scalar (%s=%q) vs array (%s=( ... ))
# output. This replaces the hand-kept print list that had to stay in lockstep
# with apply_config_defaults. CONFIG_SOURCE is printed first and is NOT a
# registry entry: it is the resolved config path, not a config variable.
#
# The two array fields are not plain shell variables at print time -- they are
# normalised through resolve_tar_opts / resolve_sync_destinations (which fill an
# unset/empty array with its default) -- so the loop dispatches array fields to
# those resolved local copies rather than reading the raw global.
print_config() {
    local -a tar_opts=()
    local -a sync_destinations=()
    local spec
    local -a fields=()
    local name print_kind

    resolve_tar_opts tar_opts
    resolve_sync_destinations sync_destinations

    print_shell_assignment CONFIG_SOURCE "$SHURIKEN_CONFIG_SOURCE"

    for spec in "${CONFIG_SPECS[@]}"; do
        config_spec_split "$spec" fields
        name="${fields[0]}"
        print_kind="${fields[5]}"

        case "$print_kind" in
            scalar)
                print_shell_assignment "$name" "${!name}"
                ;;
            array)
                case "$name" in
                    TAR_OPTS)
                        print_shell_array_assignment TAR_OPTS \
                            "${tar_opts[@]}"
                        ;;
                    SYNC_DESTINATIONS)
                        print_shell_array_assignment SYNC_DESTINATIONS \
                            "${sync_destinations[@]}"
                        ;;
                esac
                ;;
        esac
    done
}