Validate config file

This commit is contained in:
Alex
2023-05-28 16:25:42 -06:00
parent 03d870296d
commit d13d786d6c

55
ishare2
View File

@@ -93,6 +93,61 @@ function create_config() {
echo -e "${RED} [!] Failed to create configuration file. Please try again.${NO_COLOR}"
exit 1
fi
remove_duplicates
}
remove_duplicates() {
local config_file="$ISHARE2_DIR/ishare2.conf"
# Check if the configuration file exists
if [ ! -f "$config_file" ]; then
echo -e "${RED} [!] Configuration file does not exist.${NO_COLOR}"
return
fi
# Remove duplicate lines from the configuration file
awk '!seen[$0]++' "$config_file" >"$config_file.tmp" && mv "$config_file.tmp" "$config_file"
echo -e "${GREEN} [+] Duplicates removed from the configuration file.${NO_COLOR}"
}
validate_conf_file() {
local config_file="$ISHARE2_DIR/ishare2.conf"
# Check if the configuration file exists
if [ ! -f "$config_file" ]; then
echo -e "${RED} [!] Configuration file does not exist.${NO_COLOR}"
return 1
fi
# Array of allowed variables
local allowed_vars=("USE_ARIA2C" "CHANNEL")
# Read the configuration file line by line and filter out invalid variables
local filtered_content=""
while IFS= read -r line; do
# Skip empty lines and comments
if [[ -z "$line" || "$line" == \#* ]]; then
continue
fi
# Extract variable name
local var_name="${line%%=*}"
# Check if the variable is allowed
if [[ " ${allowed_vars[@]} " =~ " $var_name " ]]; then
filtered_content+="$line"$'\n'
else
echo -e "${RED} [!] Removing invalid variable from the configuration file: $line.${NO_COLOR}"
fi
done <"$config_file"
# Write the filtered content back to the configuration file
echo "$filtered_content" >"$config_file"
echo -e "${GREEN} [+] Configuration file validated and updated.${NO_COLOR}" >$ISHARE2_DIR/logs.txt
source "$config_file"
return 0
}
function install_aria2c() {