Use getopts for user's helpers

This commit is contained in:
Maniack Crudelis 2018-12-29 18:12:33 +01:00 committed by GitHub
parent c057d38fe1
commit 6425117c4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,22 +2,35 @@
# #
# example: ynh_user_exists 'toto' || exit 1 # example: ynh_user_exists 'toto' || exit 1
# #
# usage: ynh_user_exists username # usage: ynh_user_exists --username=username
# | arg: username - the username to check # | arg: -u, --username - the username to check
ynh_user_exists() { ynh_user_exists() {
sudo yunohost user list --output-as json | grep -q "\"username\": \"${1}\"" # Declare an array to define the options of this helper.
declare -Ar args_array=( [u]=username= )
local username
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
sudo yunohost user list --output-as json | grep -q "\"username\": \"${username}\""
} }
# Retrieve a YunoHost user information # Retrieve a YunoHost user information
# #
# example: mail=$(ynh_user_get_info 'toto' 'mail') # example: mail=$(ynh_user_get_info 'toto' 'mail')
# #
# usage: ynh_user_get_info username key # usage: ynh_user_get_info --username=username --key=key
# | arg: username - the username to retrieve info from # | arg: -u, --username - the username to retrieve info from
# | arg: key - the key to retrieve # | arg: -k, --key - the key to retrieve
# | ret: string - the key's value # | ret: string - the key's value
ynh_user_get_info() { ynh_user_get_info() {
sudo yunohost user info "$1" --output-as plain | ynh_get_plain_key "$2" # Declare an array to define the options of this helper.
declare -Ar args_array=( [u]=username= [k]=key= )
local username
local key
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
sudo yunohost user info "$username" --output-as plain | ynh_get_plain_key "$key"
} }
# Get the list of YunoHost users # Get the list of YunoHost users
@ -33,39 +46,58 @@ ynh_user_list() {
# Check if a user exists on the system # Check if a user exists on the system
# #
# usage: ynh_system_user_exists username # usage: ynh_system_user_exists --username=username
# | arg: username - the username to check # | arg: -u, --username - the username to check
ynh_system_user_exists() { ynh_system_user_exists() {
getent passwd "$1" &>/dev/null # Declare an array to define the options of this helper.
declare -Ar args_array=( [u]=username= )
local username
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
getent passwd "$username" &>/dev/null
} }
# Create a system user # Create a system user
# #
# usage: ynh_system_user_create user_name [home_dir] # usage: ynh_system_user_create --username=user_name [--home_dir=home_dir]
# | arg: user_name - Name of the system user that will be create # | arg: -u, --username - Name of the system user that will be create
# | arg: home_dir - Path of the home dir for the user. Usually the final path of the app. If this argument is omitted, the user will be created without home # | arg: -h, --home_dir - Path of the home dir for the user. Usually the final path of the app. If this argument is omitted, the user will be created without home
ynh_system_user_create () { ynh_system_user_create () {
if ! ynh_system_user_exists "$1" # Check if the user exists on the system # Declare an array to define the options of this helper.
declare -Ar args_array=( [u]=username= [h]=home_dir= )
local username
local home_dir
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
if ! ynh_system_user_exists "$username" # Check if the user exists on the system
then # If the user doesn't exist then # If the user doesn't exist
if [ $# -ge 2 ]; then # If a home dir is mentioned if [ $# -ge 2 ]; then # If a home dir is mentioned
local user_home_dir="-d $2" local user_home_dir="-d $home_dir"
else else
local user_home_dir="--no-create-home" local user_home_dir="--no-create-home"
fi fi
sudo useradd $user_home_dir --system --user-group $1 --shell /usr/sbin/nologin || ynh_die "Unable to create $1 system account" sudo useradd $user_home_dir --system --user-group $username --shell /usr/sbin/nologin || ynh_die "Unable to create $username system account"
fi fi
} }
# Delete a system user # Delete a system user
# #
# usage: ynh_system_user_delete user_name # usage: ynh_system_user_delete --username=user_name
# | arg: user_name - Name of the system user that will be create # | arg: -u, --username - Name of the system user that will be create
ynh_system_user_delete () { ynh_system_user_delete () {
if ynh_system_user_exists "$1" # Check if the user exists on the system # Declare an array to define the options of this helper.
declare -Ar args_array=( [u]=username= )
local username
# Manage arguments with getopts
ynh_handle_getopts_args "$@"
if ynh_system_user_exists "$username" # Check if the user exists on the system
then then
echo "Remove the user $1" >&2 echo "Remove the user $username" >&2
sudo userdel $1 sudo userdel $username
else else
echo "The user $1 was not found" >&2 echo "The user $username was not found" >&2
fi fi
} }