helpers 2.1: Simplify ynh_app_upstream_version and ynh_read_manifest

This commit is contained in:
Alexandre Aubin 2024-05-27 23:45:03 +02:00
parent 88684c7937
commit 7c1b07ee0f

View file

@ -149,7 +149,7 @@ ynh_setup_source() {
source_id="${source_id:-main}"
# ===========================================
local sources_json=$(cat $YNH_APP_BASEDIR/manifest.toml | toml_to_json | jq ".resources.sources[\"$source_id\"]")
local sources_json=$(ynh_read_manifest ".resources.sources[\"$source_id\"]")
if jq -re ".url" <<< "$sources_json"
then
local arch_prefix=""
@ -453,9 +453,9 @@ ynh_add_config() {
local template
local destination
ynh_handle_getopts_args "$@"
local template_path
# ===========================================
local template_path
if [ -f "$YNH_APP_BASEDIR/conf/$template" ]; then
template_path="$YNH_APP_BASEDIR/conf/$template"
elif [ -f "$template" ]; then
@ -827,95 +827,27 @@ ynh_secure_remove() {
set -o xtrace # set -x
}
# Read the value of a key in a ynh manifest file
# Read the value of a key in the app's manifest
#
# usage: ynh_read_manifest --manifest="manifest.json" --key="key"
# | arg: -m, --manifest= - Path of the manifest to read
# | arg: -k, --key= - Name of the key to find
# usage: ynh_read_manifest "key"
# | arg: key - Name of the key to find
# | ret: the value associate to that key
#
# Requires YunoHost version 3.5.0 or higher.
ynh_read_manifest() {
# ============ Argument parsing =============
local -A args_array=([m]=manifest= [k]=manifest_key=)
local manifest
local manifest_key
ynh_handle_getopts_args "$@"
# ===========================================
if [ ! -e "${manifest:-}" ]; then
# If the manifest isn't found, try the common place for backup and restore script.
if [ -e "$YNH_APP_BASEDIR/manifest.json" ]
then
manifest="$YNH_APP_BASEDIR/manifest.json"
elif [ -e "$YNH_APP_BASEDIR/manifest.toml" ]
then
manifest="$YNH_APP_BASEDIR/manifest.toml"
else
ynh_die --message="No manifest found !?"
fi
fi
if echo "$manifest" | grep -q '\.json$'
then
jq ".$manifest_key" "$manifest" --raw-output
else
cat "$manifest" | python3 -c 'import json, toml, sys; print(json.dumps(toml.load(sys.stdin)))' | jq ".$manifest_key" --raw-output
fi
cat $YNH_APP_BASEDIR/manifest.toml | toml_to_json | jq ".$manifest_key" --raw-output
}
# Read the upstream version from the manifest or `$YNH_APP_MANIFEST_VERSION`
# Return the app upstream version, deduced from `$YNH_APP_MANIFEST_VERSION` and strippig the `~ynhX` part
#
# usage: ynh_app_upstream_version [--manifest="manifest.json"]
# | arg: -m, --manifest= - Path of the manifest to read
# usage: ynh_app_upstream_version
# | ret: the version number of the upstream app
#
# If the `manifest` is not specified, the envvar `$YNH_APP_MANIFEST_VERSION` will be used.
#
# The version number in the manifest is defined by `<upstreamversion>~ynh<packageversion>`.
#
# For example, if the manifest contains `4.3-2~ynh3` the function will return `4.3-2`
#
# Requires YunoHost version 3.5.0 or higher.
ynh_app_upstream_version() {
# ============ Argument parsing =============
local -A args_array=([m]=manifest=)
local manifest
ynh_handle_getopts_args "$@"
manifest="${manifest:-}"
# ===========================================
if [[ "$manifest" != "" ]] && [[ -e "$manifest" ]]; then
version_key_=$(ynh_read_manifest --manifest="$manifest" --manifest_key="version")
else
version_key_=$YNH_APP_MANIFEST_VERSION
fi
echo "${version_key_/~ynh*/}"
}
# Read package version from the manifest
#
# [internal]
#
# usage: ynh_app_package_version [--manifest="manifest.json"]
# | arg: -m, --manifest= - Path of the manifest to read
# | ret: the version number of the package
#
# The version number in the manifest is defined by `<upstreamversion>~ynh<packageversion>`.
#
# For example, if the manifest contains `4.3-2~ynh3` the function will return `3`
#
# Requires YunoHost version 3.5.0 or higher.
ynh_app_package_version() {
# ============ Argument parsing =============
local -A args_array=([m]=manifest=)
local manifest
ynh_handle_getopts_args "$@"
# ===========================================
version_key_=$YNH_APP_MANIFEST_VERSION
echo "${version_key_/*~ynh/}"
echo "${$YNH_APP_MANIFEST_VERSION/~ynh*/}"
}
# Checks the app version to upgrade with the existing app version and returns: