Merge pull request #1151 from ericgaspar/ynh_exec_as

Add ynh_exec_as to official
This commit is contained in:
Alexandre Aubin 2021-01-30 16:43:59 +01:00 committed by GitHub
commit 598408aeb2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -163,3 +163,27 @@ ynh_system_user_delete () {
delgroup $username delgroup $username
fi fi
} }
# Execute a command as another user
#
# usage: ynh_exec_as --user=USER --command=COMMAND [ARG ...]
# | arg: -u, --user= - the user that will execute the command
# | arg: -n, --command= - the command to be executed
#
# Requires YunoHost version 4.1.7 or higher.
ynh_exec_as()
{
# Declare an array to define the options of this helper.
local legacy_args=uc
local -A args_array=( [u]=user= [c]=command= )
local user
local command
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
if [[ $user = $(whoami) ]]; then
eval "$command"
else
sudo -u "$user" "$command"
fi
}