From b4fb0865900194c5f8eefd088edcdda0a7c06e87 Mon Sep 17 00:00:00 2001 From: Dante Date: Tue, 4 Oct 2022 18:00:46 +0100 Subject: [PATCH] Better handling of special chars like * --- scripts/config | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/scripts/config b/scripts/config index dbe4b5a..1e3a10d 100644 --- a/scripts/config +++ b/scripts/config @@ -64,27 +64,29 @@ EOF } function set__role { + set -o noglob # Disable globbing to avoid expansions when passing * as value. declare values="list$role" newValues="${!values}" # Here we expand the dynamic variable we created in the previous line. ! Does the trick usersArray=(${newValues//,/ }) # Split the values using comma (,) as separator. - if [ -n "${!values}" ] + if [ -n "$newValues" ] then ynh_systemd_action --service_name="$app" --action=stop # Get all entries between "permissions:" and "relay:" keys, remove the role part, remove commented parts, format it with newlines and clean whitespaces and double quotes. - allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag' "$final_path/config.yaml" | sed -r 's/: (admin|user|relay)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' ) + allDefinedEntries=$(awk '/permissions:/{flag=1; next} /relay:/{flag=0} flag' "$final_path/config.yaml" | sed "/: $role/d" | sed -r 's/: (admin|user|relay)//' | tr -d '[:blank:]' | sed '/^#/d' | tr -d '\"' | tr ',' '\n' ) # Delete everything from the corresponding role to insert the new defined values. This way we also handle deletion of users. - sed -i "/permissions:/,/relay:/{/: ${role}/d;}" "$final_path/config.yaml" + sed -i "/permissions:/,/relay:/{/: $role/d;}" "$final_path/config.yaml" for user in "${usersArray[@]}" do - if echo "$allDefinedEntries" | grep -q -E "^${user}$" + if grep -q -x "${user}" <<< "$allDefinedEntries" then ynh_print_info "User $user already defined in another role." else - sed -i "/permissions:/a \ \\\"${user}\": ${role}" "$final_path/config.yaml" # Whitespaces are needed so that the file can be correctly parsed + sed -i "/permissions:/a \ \\\"$user\": $role" "$final_path/config.yaml" # Whitespaces are needed so that the file can be correctly parsed fi done fi + set +o noglob ynh_print_info "Users with role $role added in $final_path/config.yaml" }