Config panel multi-line text getter / Getter du panneau de config pour un texte multi-lignes

Hello,

I have some issue getting the information of a text file containing a list of email addresses.

It’s a simple text file, with an email per line, and I’m using a text form to interact with it.

The setter and validation works correctly, both in config script, but the getter doesn’t work as expected. Instead of displaying an email address per line, it simply adds a blank between them.

I‘d like to know if it’s a misconfiguration, a bug, or even a feature and what is the best practice or solution for it.

My get__moderators() function currently is (I copy here my two different tries):

# first try not working: missing line breaks

#get__moderators() {
#    local moderators_file="$subscribers_dir/moderators"
#    if [ -f "$moderators_file" ]; then
#        cat "$moderators_file"  
#    else
#        echo ""  
#    fi
#}

# 2nd try workaround (process each line independently) but same issue

get__moderators() {
    local moderators_file="$subscribers_dir/moderators"
    if [ -f "$moderators_file" ]; then
        while IFS= read -r line; do
            echo "$line"
        done < "$moderators_file"
    else
        echo ""
    fi
}

Bonjour,

J’ai un problème pour afficher, via un getter, les informations d’un fichier texte contenant une liste d’adresses email, dans le panneau de configuration de mon app.

Il s’agit d’un simple fichier texte, avec une adresse courriel par ligne, et j’utilise un formulaire texte pour interagir avec celui-ci.

Le setter et la validation fonctionnent correctement, tous deux dans le script config, mais le getter ne fonctionne pas comme prévu. Au lieu d’afficher une adresse par ligne, il ajoute simplement un blanc entre chaque.

J’aimerais savoir s’il s’agit d’une mauvaise configuration, d’un bug, ou même d’une fonctionnalité et quelle est la meilleure pratique ou solution pour cela.

Ma fonction get__moderators() est actuellement (je recopie mes deux tentatives différentes) :

# first try not working: missing line breaks

#get__moderators() {
#    local moderators_file="$subscribers_dir/moderators"
#    if [ -f "$moderators_file" ]; then
#        cat "$moderators_file"  
#    else
#        echo ""  
#    fi
#}

# 2nd try workaround (process each line independently) but same issue

get__moderators() {
    local moderators_file="$subscribers_dir/moderators"
    if [ -f "$moderators_file" ]; then
        while IFS= read -r line; do
            echo "$line"
        done < "$moderators_file"
    else
        echo ""
    fi
}

I think read is meant to process strings with separators, be it with new lines, spaces, or whatever is defined with IFS. Here, maybe you should simply load the whole file without processing it: echo "$(<moderators_file)". Quotes are important in this case. :wink:

1 Like

Well, my first try was with cat "$moderators_file", as you can see in the commented part of the code, but I’ll try echo "$(<moderators_file)" if it works better.

Hmmm, can’t we use the famous “bind” to bind to the entire file ? Something like bind = '/path/to/conf' (without any ‘:’ like it’s usually found to bind to a specific field)

(edit: i mean without even requiring any custom getter/setter)

2 Likes

I did not think about it, as I have not key/value but only values.
I’ll try that.

It’s working, thanks.