diff --git a/data/apps/helpers.d/user b/data/apps/helpers.d/user index de9a2cbce..1fd8d203c 100644 --- a/data/apps/helpers.d/user +++ b/data/apps/helpers.d/user @@ -1,5 +1,15 @@ - # Check if a user exists +# +# usage: ynh_user_exists username +# | ret: retcode - 0 if user exists, 1 otherwise ynh_user_exists() { sudo yunohost user list --json | grep -q "\"username\": \"${1}\"" -} \ No newline at end of file +} + +# Retrieve a user information +# +# usage: ynh_user_info username key +# | ret: string - the key's value +ynh_user_get_info() { + sudo yunohost user info "${1}" --plain | ynh_get_plain_key "${2}" +} diff --git a/data/apps/helpers.d/utils b/data/apps/helpers.d/utils new file mode 100644 index 000000000..87cd47f6b --- /dev/null +++ b/data/apps/helpers.d/utils @@ -0,0 +1,24 @@ +# Extract a key from a plain command output +# +# usage: ynh_get_plain_key key [subkey [subsubkey ...]] +# | ret: string - the key's value +ynh_get_plain_key() { + prefix="#" + founded=0 + key=$1 + shift + while read line; do + if [[ "$founded" == "1" ]] ; then + [[ "$line" =~ ^${prefix}[^#] ]] && return + echo $line + elif [[ "$line" =~ ^${prefix}${key}$ ]]; then + if [[ -n "$1" ]]; then + prefix+="#" + key=$1 + shift + else + founded=1 + fi + fi + done +}