Improve ynh_load_environment helper

This commit is contained in:
tituspijean 2023-05-18 16:10:21 +00:00
parent d27e9a9eea
commit 68a4f2b4bc
No known key found for this signature in database
GPG key ID: EF3B0D7CC0A94720

View file

@ -126,9 +126,6 @@ ynh_load_app_environment() {
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
# Retrieve the list of installed apps
local installed_apps_list=($(yunohost app list --output-as json --quiet | jq -r .apps[].id))
# Force Bash to be used to run this helper # Force Bash to be used to run this helper
if [ $0 != "bash" ] if [ $0 != "bash" ]
then then
@ -137,14 +134,21 @@ ynh_load_app_environment() {
fi fi
# Make sure the app is installed # Make sure the app is installed
local installed_apps_list=($(yunohost app list --output-as json --quiet | jq -r .apps[].id))
if [[ " ${installed_apps_list[*]} " != *" ${app} "* ]] if [[ " ${installed_apps_list[*]} " != *" ${app} "* ]]
then then
ynh_print_err --message="$app is not in the apps list" ynh_print_err --message="$app is not in the apps list"
exit 1 exit 1
fi fi
# Make sure the app is installed
if ! id -u "$app" &>/dev/null; then
ynh_print_err --message="There is no \"$app\" system user"
exit 1
fi
# Make sure the app has an install_dir setting # Make sure the app has an install_dir setting
install_dir="$(yunohost app setting $app install_dir)" local install_dir="$(yunohost app setting $app install_dir)"
if [ -z "$install_dir" ] if [ -z "$install_dir" ]
then then
ynh_print_err --message="$app has no install_dir setting (does it use packaging format >=2?)" ynh_print_err --message="$app has no install_dir setting (does it use packaging format >=2?)"
@ -152,18 +156,21 @@ ynh_load_app_environment() {
fi fi
# Load the Environment variables from the app's service # Load the Environment variables from the app's service
env_var=`systemctl show $app.service -p "Environment" --value` local env_var=`systemctl show $app.service -p "Environment" --value`
[ -n "$env_var" ] && export $env_var; [ -n "$env_var" ] && export $env_var;
export HOME=$install_dir; export HOME=$install_dir;
# Source the EnvironmentFiles from the app's service # Source the EnvironmentFiles from the app's service
env_files=(`systemctl show $app.service -p "EnvironmentFiles" --value`) local env_files=(`systemctl show $app.service -p "EnvironmentFiles" --value`)
if [ ${#env_files[*]} -gt 0 ] if [ ${#env_files[*]} -gt 0 ]
then then
# set -/+a enables and disables new variables being automatically exported. Needed when using `source`.
set -a
for file in ${env_files[*]} for file in ${env_files[*]}
do do
[[ $file = /* ]] && source $file [[ $file = /* ]] && source $file
done done
set +a
fi fi
# Open the app shell # Open the app shell