Skip to content

Commit 55f3985

Browse files
jbtrystramdustymabe
authored andcommitted
cmdlib: make a helper for manifest.metadata knobs
Prep work to add a knob for using bootc install in osbuild. Refactor the override logic in a helper function so we can easily add those knobs down the line.
1 parent e64c822 commit 55f3985

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

src/cmdlib.sh

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,28 +153,47 @@ yaml2json() {
153153
python3 -c 'import sys, json, yaml; json.dump(yaml.safe_load(sys.stdin), sys.stdout, sort_keys=True)' < "$1" > "$2"
154154
}
155155

156-
should_build_with_buildah() {
157-
local variant manifest
158-
if [ -n "${COSA_BUILD_WITH_BUILDAH:-}" ]; then
159-
if [ "${COSA_BUILD_WITH_BUILDAH:-}" = 1 ]; then
156+
# Common helper to check for features that can be enabled via an env var or
157+
# in the manifest metadata.
158+
_should_enable_feature() {
159+
local env_var_name=$1
160+
local metadata_key=$2
161+
local env_var_value
162+
# Indirect expansion
163+
env_var_value=${!env_var_name:-}
164+
165+
if [ -n "${env_var_value}" ]; then
166+
if [ "${env_var_value}" = 1 ]; then
160167
return 0
161168
else
162169
return 1
163170
fi
164171
fi
172+
173+
# Make sure we are in the config directory (e.g. cmd-osbuild set a different working directory).
174+
# When called very early (e.g. cmd-fetch), configdir isn't initialized yet so we assume we are in the top
175+
# cosa initialized dir and use `src/config`.
176+
# We redirect the output to /dev/null to avoid the noisy `dirs` output.
177+
pushd "${configdir:-src/config}" > /dev/null
165178
# this slightly duplicates some logic in `prepare_build`, but meh...
166-
if [[ -f "src/config.json" ]]; then
167-
variant="$(jq --raw-output '."coreos-assembler.config-variant"' src/config.json)"
168-
manifest="src/config/manifest-${variant}.yaml"
179+
if [[ -f "../config.json" ]]; then
180+
variant="$(jq --raw-output '."coreos-assembler.config-variant"' ../config.json)"
181+
manifest="manifest-${variant}.yaml"
169182
else
170-
manifest="src/config/manifest.yaml"
183+
manifest="manifest.yaml"
171184
fi
172-
if [ "$(yq .metadata.build_with_buildah "${manifest}")" = true ]; then
185+
if [ "$(yq ".metadata.${metadata_key}" "${manifest}")" = true ]; then
186+
popd > /dev/null
173187
return 0
174188
fi
189+
popd > /dev/null
175190
return 1
176191
}
177192

193+
should_build_with_buildah() {
194+
_should_enable_feature "COSA_BUILD_WITH_BUILDAH" "build_with_buildah"
195+
}
196+
178197
# Only used by legacy (not via container tools) path. Delete when we
179198
# have moved away from legacy building (i.e. delete or overwrite cmd-build
180199
# and delete cmd-fetch)

0 commit comments

Comments
 (0)