#@IgnoreInspection BashAddShebang # Config file functionality (Koalephant Shell Script Library) # Version: 2.0.0 # Copyright: 2017, Koalephant Co., Ltd # Author: Stephen Reay , Koalephant Packaging Team # Run a cfget command # # Input: # $1 - the config file # $2...n - extra arguments for cfget # # Output: # The result from cfget # # Return: # The cfget return code proc k_config_command { var file = $1 shift CFGET_BIN --plugin "$(KOALEPHANT_LIB_PATH)/cfget-plugins/" --format nestedini --cfg $(file) --quiet @Argv } # Get config sections # # Input: # $1 - the config file # $2 - the config root to work from # # Output: # the section names, one per line proc k_config_sections { var file = $1 var root = ''"" if test -n $(2:-) { set root = ""--root $(2)"" } k_config_command $(file) --dump sections $(root) || true } # Get config keys # # Input: # $1 - the config file # $2 - the config root to work from # # Output: # the key names, one per line proc k_config_keys { var file = $1 var root = ''"" if test -n $(2:-) { set root = ""--root $(2)"" } k_config_command $(file) --dump keys $(root) } # Read a Config Value # Unlike (#k_config_read_error), this function will not error if the config key is not found # # Input: # $1 - the config file # $2 - the key to read # $3 - the config root to work from # # Output: # the value of the key, if it exists proc k_config_read { k_config_read_error @Argv || true } # Read a Config Value, or error # # Input: # $1 - the config file # $2 - the key to read # $3 - the config root to work from # # Output: # the value of the key, if it exists # # Return: # 0 on success, 1 on error proc k_config_read_error { var file = $1 var key = $2 var root = ''"" if test -n $(3:-) { set root = ""--root $(3)"" } k_config_command $(file) $(root) $(key) } # Get a Config value or a default value # # Input: # $1 - the config file # $2 - the key to read # $3 - the default value (defaults to empty string) # $4 - the config root to work from # # Output: # the value of the key, or the default value proc k_config_read_string { var file = $1 var key = $2 var default = $(3:-) var root = $(4:-) var value = $[k_config_read $file $key $(root)] printf "%s" $(value:-$default) } # Get a 'Boolean' Config value or a default value # # Input: # $1 - the config file # $2 - the key to read # $3 - the default value (defaults to empty string) # $4 - the config root to work from # # Output: # `true`, `false` or the default value given if the config value cannot be parsed using (#k_bool_parse) proc k_config_read_bool { var file = $1 var key = $2 var default = $(3:-) var root = $(4:-) k_bool_parse $[k_config_read $(file) $(key) $(root)] $(default) } # Test a 'Boolean' Config value or a default value # # Input: # $1 - the config file # $2 - the key to read # $3 - the default value (defaults to empty string) # $4 - the config root to work from # # Return: # 0 if either the config value or the default value is true, 1 otherwise proc k_config_test_bool { var file = $1 var key = $2 var default = $(3:-) var root = $(4:-) k_bool_test $[k_config_read_string $(file) $(key) $[k_bool_parse $(default)] $(root)] } # Get a 'keyword' Config value or a default value # # Input: # $1 - the config file # $2 - the key to read # $3 - the default value # $4 - the config root to work from # $5...n - the list of valid keyword values # # Output: # the valid keyword or default value proc k_config_read_keyword { var file = $1 var key = $2 var default = $3 var root = $4 shift 4 k_string_keyword $[k_config_read_string $(file) $(key) $(default) $(root)] @Argv $default }