Replies: 11 comments 44 replies
-
I agree it is a good idea. It will most likely make the library code much more complex, especially for setting values. But I will take a closer look at it, see if I can implement. |
Beta Was this translation helpful? Give feedback.
-
I was able to make a very bad mockup for the config_get (With a little bit of help of AI) but I didn't save it since it barely worked. I was trying to see what the possibilities were for the config_set but I couldn't make heads or tails of it. I know the yaml library uses awk and sed, maybe it could potentially be easier with that since sed can modify the files. |
Beta Was this translation helpful? Give feedback.
-
I will try in the next few days to make something. |
Beta Was this translation helpful? Give feedback.
-
That would be super nice.
And just get the specific one with |
Beta Was this translation helpful? Give feedback.
-
This seems to be a working ## Get a value from the config.
## Usage: result=$(config_get KEY) -or- result=$(config_get SECTION.KEY)
config_get() {
local key=$1
# Determine if we received SECTION.KEY or just KEY
if [[ $key == *.* ]]; then
IFS='.' read -r section key <<< "$key"
else
section=""
fi
local regex="^$key *= *(.+)$"
local section_regex="^\[$section\]"
local other_section_regex="^\["
local value=""
config_init
while IFS= read -r line || [ -n "$line" ]; do
# If SECTION was given, find it in the INI first
if [[ $section && $found_section != true ]]; then
[[ $line =~ $section_regex ]] && found_section=true
continue
fi
# If SECTION was given and found, any other [SECTION] means we did not
# find the requested KEY in the requested SECTION - abort.
[[ $section && $line =~ $other_section_regex ]] && break
# Found the value
if [[ $line =~ $regex ]]; then
value="${BASH_REMATCH[1]}"
break
fi
done <"$CONFIG_FILE"
echo "$value"
} The |
Beta Was this translation helpful? Give feedback.
-
A bit off topic, but do you know what the best way to approach to check if a config file exists? I want to make sure one exists with some default values before running any commands. |
Beta Was this translation helpful? Give feedback.
-
How about this prototype? #!/usr/bin/env bash
config_load() {
declare -gA config
local section=""
local key=""
local value=""
local section_regex="^\[(.+)\]"
local key_regex="^([^ =]+) *= *(.*) *$"
local comment_regex="^;"
while IFS= read -r line; do
if [[ $line =~ $comment_regex ]]; then
continue
elif [[ $line =~ $section_regex ]]; then
section="${BASH_REMATCH[1]}."
elif [[ $line =~ $key_regex ]]; then
key="${BASH_REMATCH[1]}"
value="${BASH_REMATCH[2]}"
config["${section}${key}"]="$value"
fi
done < config.ini
}
config_show() {
for key in "${!config[@]}"; do
echo "$key=${config[$key]}"
done
}
config_save() {
local filename="out.ini"
local current_section=""
rm -f "$filename"
for key in "${!config[@]}"; do
IFS="." read -r section_name key_name <<< "$key"
value="${config[$key]}"
if [ "$current_section" != "$section_name" ]; then
[[ $current_section ]] && echo >> "$filename"
echo "[$section_name]" >> "$filename"
current_section="$section_name"
fi
echo "$key_name = $value" >> "$filename"
done
}
# read file to the config var
config_load
# use the values
echo "${config[section1.key]}"
echo "${config[section1.key2]}"
echo "${config[section2.key]}"
echo "${config[section2.key2]}"
echo "${config[section1.under_key]}"
# show the entire loaded config (associative array)
config_show
# update a value
config[section1.under_key]="updated"
# delete a value
unset config[section1.key2]
unset config[section2.key2]
# save the entire array back to the file
config_save
|
Beta Was this translation helpful? Give feedback.
-
See #406 - with a new config library and updated example. Since this is a breaking change, I need to consider the release plan more carefully, but you can already use this example by simply copying the config.sh file from that PR. Let me know how it works for you. |
Beta Was this translation helpful? Give feedback.
-
@Zelaf - note:
You can either:
|
Beta Was this translation helpful? Give feedback.
-
Ok - another attempt. Do you want to try it out? Highlights
|
Beta Was this translation helpful? Give feedback.
-
Ok - I think this is it: #409 When you add the config library, it will also add the INI library. I think this is a good conclusion.
Makes sense? |
Beta Was this translation helpful? Give feedback.
-
I was wondering if it's possible to add sections to the INI parser so you can have a clearer config structure.
I'm currently working on a script that has a few config options available for the user so I want to be able to have sections for the config file and if they want to update file output file locations and such they can do that, but instead of having long strings like
It would be nice to be able to do
and if you were to call to the config file like
$(config_get command_config_option)
it would returnsetting
.If you know any better approach to this it would also be welcomed. At first I tried to use the yaml addon but realised that it can't really set the options so if I were to want a reset option or more expandability it wouldn't really work in the bigger picture.
Beta Was this translation helpful? Give feedback.
All reactions