|
| 1 | +# --- |
| 2 | +# Config functions |
| 3 | +# This file is a part of Bashly standard library |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# - In your script, set the CONFIG_FILE variable. For rxample: |
| 7 | +# CONFIG_FILE=settings.ini. |
| 8 | +# If it is unset, it will default to 'config.ini'. |
| 9 | +# - Use any of the functions below to access the config file. |
| 10 | +# --- |
| 11 | + |
| 12 | +# Create a new config file. |
| 13 | +# There is normally no need to use this function, it is used by other |
| 14 | +# functions as needed. |
| 15 | +config_init() { |
| 16 | + CONFIG_FILE=${CONFIG_FILE:=config.ini} |
| 17 | + [[ -f "$CONFIG_FILE" ]] || touch "$CONFIG_FILE" |
| 18 | +} |
| 19 | + |
| 20 | +# Get a value from the config. |
| 21 | +# Usage: result=$(config_get hello) |
| 22 | +config_get() { |
| 23 | + local key=$1 |
| 24 | + local regex="^$key *= *(.+)$" |
| 25 | + local value="" |
| 26 | + |
| 27 | + config_init |
| 28 | + |
| 29 | + while IFS= read -r line || [ -n "$line" ]; do |
| 30 | + if [[ $line =~ $regex ]]; then |
| 31 | + value="${BASH_REMATCH[1]}" |
| 32 | + break |
| 33 | + fi |
| 34 | + done < "$CONFIG_FILE" |
| 35 | + |
| 36 | + echo "$value" |
| 37 | +} |
| 38 | + |
| 39 | +# Add or update a key=value pair in the config. |
| 40 | +# Usage: config_set key value |
| 41 | +config_set() { |
| 42 | + local key=$1 |
| 43 | + shift |
| 44 | + local value="$*" |
| 45 | + |
| 46 | + config_init |
| 47 | + |
| 48 | + local regex="^($key) *= *.+$" |
| 49 | + local output="" |
| 50 | + local found_key="" |
| 51 | + local newline |
| 52 | + |
| 53 | + while IFS= read -r line || [ -n "$line" ]; do |
| 54 | + newline=$line |
| 55 | + if [[ $line =~ $regex ]]; then |
| 56 | + found_key="${BASH_REMATCH[1]}" |
| 57 | + newline="$key = $value" |
| 58 | + output="$output$newline\n" |
| 59 | + elif [[ $line ]]; then |
| 60 | + output="$output$line\n" |
| 61 | + fi |
| 62 | + done < "$CONFIG_FILE" |
| 63 | + |
| 64 | + if [[ -z $found_key ]]; then |
| 65 | + output="$output$key = $value\n" |
| 66 | + fi |
| 67 | + |
| 68 | + printf "%b\n" "$output" > "$CONFIG_FILE" |
| 69 | +} |
| 70 | + |
| 71 | +# Delete a key from the config. |
| 72 | +# Usage: config_del key |
| 73 | +config_del() { |
| 74 | + local key=$1 |
| 75 | + |
| 76 | + local regex="^($key) *=" |
| 77 | + local output="" |
| 78 | + |
| 79 | + config_init |
| 80 | + |
| 81 | + while IFS= read -r line || [ -n "$line" ]; do |
| 82 | + if [[ $line ]] && [[ ! $line =~ $regex ]]; then |
| 83 | + output="$output$line\n" |
| 84 | + fi |
| 85 | + done < "$CONFIG_FILE" |
| 86 | + |
| 87 | + printf "%b\n" "$output" > "$CONFIG_FILE" |
| 88 | +} |
| 89 | + |
| 90 | +# Show the config file |
| 91 | +config_show() { |
| 92 | + config_init |
| 93 | + cat "$CONFIG_FILE" |
| 94 | +} |
| 95 | + |
| 96 | +# Return an array of the keys in the config file. |
| 97 | +# Usage: |
| 98 | +# |
| 99 | +# for k in $(config_keys); do |
| 100 | +# echo "- $k = $(config_get "$k")"; |
| 101 | +# done |
| 102 | +# |
| 103 | +config_keys() { |
| 104 | + local regex="^([a-zA-Z0-9_\-\/\.]+) *=" |
| 105 | + |
| 106 | + config_init |
| 107 | + |
| 108 | + local keys=() |
| 109 | + local key |
| 110 | + |
| 111 | + while IFS= read -r line || [ -n "$line" ]; do |
| 112 | + if [[ $line =~ $regex ]]; then |
| 113 | + key="${BASH_REMATCH[1]}" |
| 114 | + keys+=("$key") |
| 115 | + fi |
| 116 | + done < "$CONFIG_FILE" |
| 117 | + echo "${keys[@]}" |
| 118 | +} |
| 119 | + |
| 120 | +# Returns true if the specified key exists in the config file. |
| 121 | +# Usage: |
| 122 | +# |
| 123 | +# if config_has_key "key" ; then |
| 124 | +# echo "key exists" |
| 125 | +# fi |
| 126 | +# |
| 127 | +config_has_key() { |
| 128 | + [[ $(config_get "$1") ]] |
| 129 | +} |
0 commit comments