aboutsummaryrefslogtreecommitdiff
path: root/lib/util/config.bash
blob: 76b6567760d0d4f838d64637dd371de187a2f106 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2023 Patrick Spek <p.spek@tyil.nl>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

# Read a particular value from a key/value configuration file. Using this
# function introduces a dependency on awk.
config() {
	config_for "${BASHTARD_PLATFORM[fqdn]}" "$@"
}

config_subkeys() {
	config_subkeys_for "${BASHTARD_PLATFORM[fqdn]}" "$@"
}

config_for() {
	local host=$1 ; shift
	local key=$1 ; shift
	local default=$1 ; shift

	local default
	local file
	local files

	files=(
		"$BASHTARD_ETCDIR/secrets"
		"$BASHTARD_ETCDIR/hosts.d/$host"
		"$BASHTARD_ETCDIR/os.d/${BASHTARD_PLATFORM[key]}"
		"$BASHTARD_ETCDIR/defaults"
	)

	if [[ -n "$BASHTARD_PLAYBOOK" ]]
	then
		debug "bashtard/config_for" "BASHTARD_PLAYBOOK=$BASHTARD_PLAYBOOK, adding etc entries"
		files+=(
			"$(playbook_path "base")/etc/os.d/${BASHTARD_PLATFORM[key]}"
			"$(playbook_path "base")/etc/defaults"
		)
	fi

	files+=(
		"$BASHTARD_SHAREDIR/os.d/${BASHTARD_PLATFORM[key]}"
		"$BASHTARD_SHAREDIR/defaults"
	)

	# Check configuration files
	for file in "${files[@]}"
	do
		debug "bashtard/config_for" "Checking for '$key' in '$file'"

		[[ ! -f $file ]] && continue

		# Use awk to find the right line, then use cut to get the
		# actual value. Cutting it out with awk _is_ possible, but
		# comes with whitespace issues or having to deal with values
		# containing the seperator (=), so using cut is much simpler
		# and easier to understand.
		value="$(awk -F= '$1 == "'"$key"'" { print $0 }' "$file" | cut -d'=' -f 2-)"

		if [[ -n $value ]]
		then
			debug "bashtard/config_for" "Found $key=$value in $file"

			printf "%s" "$value"
			return
		fi
	done

	# Return default value
	if [[ -n $default ]]
	then
		printf "%s" "$default"
		return
	fi

	# Error
	alert "bashtard/config_for" "No configuration value for $key"
}

config_subkeys_for() {
	local host=$1 ; shift
	local key=$1 ; shift

	local file
	local files
	local results

	files=(
		"$BASHTARD_ETCDIR/secrets"
		"$BASHTARD_ETCDIR/hosts.d/$host"
		"$BASHTARD_ETCDIR/os.d/${BASHTARD_PLATFORM[key]}"
		"$BASHTARD_ETCDIR/defaults"
	)

	if [[ -n "$BASHTARD_PLAYBOOK" ]]
	then
		debug "bashtard/config_for" "BASHTARD_PLAYBOOK=$BASHTARD_PLAYBOOK, adding etc entries"
		files+=(
			"$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/etc/os.d/${BASHTARD_PLATFORM[key]}"
			"$BASHTARD_ETCDIR/playbooks.d/$BASHTARD_PLAYBOOK/etc/defaults"
		)
	fi

	files+=(
		"$BASHTARD_SHAREDIR/os.d/${BASHTARD_PLATFORM[key]}"
		"$BASHTARD_SHAREDIR/defaults"
	)

	# Check configuration files
	for file in "${files[@]}"
	do
		debug "bashtard/config_subkeys" "Checking for subkeys of '$key' in '$file'"

		[[ ! -f $file ]] && continue

		while read -r result
		do
			local subkey

			subkey="$(awk -F. '{ print $1 }' <<< "${result#"$key."}")"

			debug "bashtard/config_subkeys" "Found '$subkey' as subkey of '$key' through '$result'"
			results+=("$subkey")
		done < <(grep "^$key\." "$file" | awk -F= '{ print $1 }')
	done

	# Return unique results
	for result in "${results[@]}"
	do
		printf "%s\n" "$result"
	done | sort -u
}