-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_parser.sh
55 lines (49 loc) · 1.79 KB
/
config_parser.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Function: parse_config
# Description:
# Parses a specified section of the 'fss.conf' configuration file. It reads
# key-value pairs from the specified section and sets them as global
# environment variables. Designed for use by sourcing this script and then
# calling the function with a desired section name. This makes the
# configuration settings from that section available as environment variables
# in the sourcing file.
#
# Arguments:
# section: A string representing the section of the configuration file to
# parse.
#
# Outputs:
# Writes an error message to standard output if the configuration file is not
# found. Sets global variables based on the configuration file's content.
#
# Globals:
# Variables defined in the specified section of the configuration file.
parse_config() {
local section=$1
local in_section=false
local user_config_file="$HOME/.config/fss/fss.conf"
local default_config_file="$FSS_ROOT_DIR/config/fss.conf"
local config_file
# Check if the user-specific configuration file exists, otherwise use the default
if [ -f "$user_config_file" ]; then
config_file="$user_config_file"
else
config_file="$default_config_file"
fi
# Check if the resolved configuration file exists
if [ ! -f "$config_file" ]; then
echo "config_parser.sh Error: Configuration file not found: $config_file"
exit 1
fi
while IFS='=' read -r line_key line_value; do
if [[ $line_key =~ ^\[$section\]$ ]]; then
in_section=true
continue
fi
if $in_section && [[ $line_key =~ ^[A-Z_]+$ ]]; then
eval "$line_key=$line_value"
elif [[ $line_key =~ ^\[.*\]$ ]]; then
in_section=false
fi
done < "$config_file"
}