[fix] ynh_read_var_in_file with ini file

This commit is contained in:
ljf 2021-09-06 00:58:46 +02:00
parent ba6f90d966
commit 4a3d6e53c6
2 changed files with 13 additions and 10 deletions

View file

@ -515,7 +515,7 @@ ynh_read_var_in_file() {
local key local key
# Manage arguments with getopts # Manage arguments with getopts
ynh_handle_getopts_args "$@" ynh_handle_getopts_args "$@"
set +o xtrace #set +o xtrace
local filename="$(basename -- "$file")" local filename="$(basename -- "$file")"
[[ -f $file ]] || ynh_die "File $file does not exists" [[ -f $file ]] || ynh_die "File $file does not exists"
@ -525,9 +525,12 @@ ynh_read_var_in_file() {
local assign="=>|:|=" local assign="=>|:|="
local comments="#" local comments="#"
local string="\"'" local string="\"'"
if [[ "yaml yml toml ini env" =~ *"$ext"* ]]; then if [[ "$ext" =~ ^ini|env|toml|yml|yaml$ ]]; then
endline='#' endline='#'
fi fi
if [[ "$ext" =~ ^ini|env$ ]]; then
comments="[;#]"
fi
if [[ "php" == "$ext" ]] || [[ "$ext" == "js" ]]; then if [[ "php" == "$ext" ]] || [[ "$ext" == "js" ]]; then
comments="//" comments="//"
fi fi
@ -535,7 +538,7 @@ ynh_read_var_in_file() {
local var_part='^\s*(?:(const|var|let)\s+)?\$?(\w+('$list')*(->|\.|\[))*\s*' local var_part='^\s*(?:(const|var|let)\s+)?\$?(\w+('$list')*(->|\.|\[))*\s*'
var_part+="[$string]?${key}[$string]?" var_part+="[$string]?${key}[$string]?"
var_part+='\s*\]?\s*' var_part+='\s*\]?\s*'
var_part+="(?:$assign)" var_part+="($assign)"
var_part+='\s*' var_part+='\s*'
# Extract the part after assignation sign # Extract the part after assignation sign
@ -546,7 +549,7 @@ ynh_read_var_in_file() {
fi fi
# Remove comments if needed # Remove comments if needed
local expression="$(echo "$expression_with_comment" | sed "s@$comments[^$string]*\$@@" | sed "s@\s*[$endline]*\s*]*\$@@")" local expression="$(echo "$expression_with_comment" | sed "s@$comments[^$string]*\$@@g" | sed "s@\s*[$endline]*\s*]*\$@@")"
local first_char="${expression:0:1}" local first_char="${expression:0:1}"
if [[ "$first_char" == '"' ]] ; then if [[ "$first_char" == '"' ]] ; then

View file

@ -79,8 +79,8 @@ ENABLED = False
# TITLE = "Old title" # TITLE = "Old title"
TITLE = "Lorem Ipsum" TITLE = "Lorem Ipsum"
THEME = "colib'ris" THEME = "colib'ris"
EMAIL = "root@example.com" // This is a comment without quotes EMAIL = "root@example.com" # This is a comment without quotes
PORT = 1234 // This is a comment without quotes PORT = 1234 # This is a comment without quotes
URL = 'https://yunohost.org' URL = 'https://yunohost.org'
DICT = {} DICT = {}
DICT['ldap_base'] = "ou=users,dc=yunohost,dc=org" DICT['ldap_base'] = "ou=users,dc=yunohost,dc=org"
@ -156,8 +156,8 @@ enabled = False
# title = Old title # title = Old title
title = Lorem Ipsum title = Lorem Ipsum
theme = colib'ris theme = colib'ris
email = root@example.com # This is a comment without quotes email = root@example.com ; This is a comment without quotes
port = 1234 # This is a comment without quotes port = 1234 ; This is a comment without quotes
url = https://yunohost.org url = https://yunohost.org
[dict] [dict]
ldap_base = ou=users,dc=yunohost,dc=org ldap_base = ou=users,dc=yunohost,dc=org
@ -175,10 +175,10 @@ EOF
test "$(_read_ini "$file" "theme")" == "colib'ris" test "$(_read_ini "$file" "theme")" == "colib'ris"
test "$(ynh_read_var_in_file "$file" "theme")" == "colib'ris" test "$(ynh_read_var_in_file "$file" "theme")" == "colib'ris"
test "$(_read_ini "$file" "email")" == "root@example.com" #test "$(_read_ini "$file" "email")" == "root@example.com"
test "$(ynh_read_var_in_file "$file" "email")" == "root@example.com" test "$(ynh_read_var_in_file "$file" "email")" == "root@example.com"
test "$(_read_ini "$file" "port")" == "1234" #test "$(_read_ini "$file" "port")" == "1234"
test "$(ynh_read_var_in_file "$file" "port")" == "1234" test "$(ynh_read_var_in_file "$file" "port")" == "1234"
test "$(_read_ini "$file" "url")" == "https://yunohost.org" test "$(_read_ini "$file" "url")" == "https://yunohost.org"