mirror of
https://github.com/flatcar/scripts.git
synced 2025-11-24 12:01:59 +01:00
build_library: add torcx_manifest helper
This implements some basic functionality around working with torcx manifests.
This commit is contained in:
parent
37f3657539
commit
0604b5c40a
97
build_library/torcx_manifest.sh
Normal file
97
build_library/torcx_manifest.sh
Normal file
@ -0,0 +1,97 @@
|
||||
# Copyright (c) 2017 The Container Linux by CoreOS Authors. All rights
|
||||
# reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
|
||||
# torcx_manifest.sh contains helper functions for creating, editing, and
|
||||
# reading torcx manifest files.
|
||||
|
||||
# create_empty creates an empty torcx manfiest at the given path.
|
||||
function torcx_manifest::create_empty() {
|
||||
local path="${1}"
|
||||
jq '.' > "${path}" <<EOF
|
||||
{
|
||||
"kind": "torcx-package-list-v0",
|
||||
"value": {
|
||||
"packages": []
|
||||
}
|
||||
}
|
||||
EOF
|
||||
}
|
||||
|
||||
# add_pkg adds a new version of a package to the torcx manifest specified by
|
||||
# path.
|
||||
# That manifest will be edited to include this version, with the associated
|
||||
# package of the given name being created as well if necessary.
|
||||
function torcx_manifest::add_pkg() {
|
||||
path="${1}"; shift
|
||||
name="${1}"; shift
|
||||
version="${1}"; shift
|
||||
pkg_hash="${1}"; shift
|
||||
cas_digest="${1}"; shift
|
||||
source_package="${1}"; shift
|
||||
update_default="${1}"; shift
|
||||
|
||||
local manifest=$(cat "${path}")
|
||||
local pkg_version_obj=$(jq '.' <<EOF
|
||||
{
|
||||
"version": "${version}",
|
||||
"hash": "${pkg_hash}",
|
||||
"casDigest": "${cas_digest}",
|
||||
"sourcePackage": "${source_package}",
|
||||
"locations": []
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
for location in "${@}"; do
|
||||
if [[ "${location}" == /* ]]; then
|
||||
# filepath
|
||||
pkg_version_obj=$(jq ".locations |= . + [{\"path\": \"${location}\"}]" <(echo "${pkg_version_obj}"))
|
||||
else
|
||||
# url
|
||||
pkg_version_obj=$(jq ".locations |= . + [{\"url\": \"${location}\"}]" <(echo "${pkg_version_obj}"))
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
local existing_pkg="$(echo "${manifest}" | jq ".value.packages[] | select(.name == \"${name}\")")"
|
||||
|
||||
# If there isn't yet a package in the manifest for $name, initialize it to an empty one.
|
||||
if [[ "${existing_pkg}" == "" ]]; then
|
||||
pkg_json=$(cat <<EOF
|
||||
{
|
||||
"name": "${name}",
|
||||
"versions": []
|
||||
}
|
||||
EOF
|
||||
)
|
||||
manifest="$(echo "${manifest}" | jq ".value.packages |= . + [${pkg_json}]")"
|
||||
fi
|
||||
|
||||
if [[ "${update_default}" == "true" ]]; then
|
||||
manifest="$(echo "${manifest}" | jq "(.value.packages[] | select(.name = \"${name}\") | .defaultVersion) |= \"${version}\"")"
|
||||
fi
|
||||
|
||||
# append this specific package version to the manifest
|
||||
manifest="$(echo "${manifest}" | jq "(.value.packages[] | select(.name = \"${name}\") | .versions) |= . + [${pkg_version_obj}]")"
|
||||
|
||||
echo "${manifest}" | jq '.' > "${path}"
|
||||
}
|
||||
|
||||
# get_pkg_names returns the list of packages in a given manifest. Each package
|
||||
# may have one or more versions associated with it.
|
||||
#
|
||||
# Example:
|
||||
# pkg_name_arr=($(torcx_manifest::get_pkg_names "torcx_manifest.json"))
|
||||
function torcx_manifest::get_pkg_names() {
|
||||
local file="${1}"
|
||||
jq -r '.value.packages[].name' < "${file}"
|
||||
}
|
||||
|
||||
# get_digests returns the list of digests for a given package.
|
||||
function torcx_manifest::get_digests() {
|
||||
local file="${1}"
|
||||
local name="${2}"
|
||||
jq -r ".value.packages[] | select(.name == \"${name}\").versions[].casDigest" < "${file}"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user