From e856a7bc394f82a19c81d43df310f78c2304805f Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Mon, 9 Mar 2020 18:07:32 +0000 Subject: [PATCH] add config_has_key to the config stdlib --- examples/config-ini/configly | 19 ++++++++++++++++++- examples/config-ini/src/get_command.sh | 8 +++++++- examples/config-ini/src/lib/config.sh | 11 +++++++++++ examples/config-ini/test.sh | 1 + lib/bashly/templates/lib/config.sh | 11 +++++++++++ spec/approvals/examples/config-ini | 2 ++ 6 files changed, 50 insertions(+), 2 deletions(-) diff --git a/examples/config-ini/configly b/examples/config-ini/configly index 050520c8..2d1de6f3 100644 --- a/examples/config-ini/configly +++ b/examples/config-ini/configly @@ -283,6 +283,17 @@ config_keys() { echo "${keys[@]}" } +# Returns true if the specified key exists in the config file +# Usage: +# +# if config_has_key "key" ; then +# echo "key exists" +# fi +# +config_has_key() { + [[ $(config_get "$1") ]] +} + # :command.command_functions # :command.function configly_set_command() { @@ -296,7 +307,13 @@ configly_set_command() { configly_get_command() { # :src/get_command.sh # Using the standard library (lib/config.sh) to show a value from the config - config_get "${args[key]}" + + key="${args[key]}" + if config_has_key "$key" ; then + config_get "$key" + else + echo "No such key: $key" + fi # Example of how to assign the config value to a variable: # result=$(config_get "${args[key]}") diff --git a/examples/config-ini/src/get_command.sh b/examples/config-ini/src/get_command.sh index de37c289..2434e70c 100644 --- a/examples/config-ini/src/get_command.sh +++ b/examples/config-ini/src/get_command.sh @@ -1,5 +1,11 @@ # Using the standard library (lib/config.sh) to show a value from the config -config_get "${args[key]}" + +key="${args[key]}" +if config_has_key "$key" ; then + config_get "$key" +else + echo "No such key: $key" +fi # Example of how to assign the config value to a variable: # result=$(config_get "${args[key]}") diff --git a/examples/config-ini/src/lib/config.sh b/examples/config-ini/src/lib/config.sh index 83d5ee3c..4a5c1735 100644 --- a/examples/config-ini/src/lib/config.sh +++ b/examples/config-ini/src/lib/config.sh @@ -113,3 +113,14 @@ config_keys() { done < "$CONFIG_FILE" echo "${keys[@]}" } + +# Returns true if the specified key exists in the config file +# Usage: +# +# if config_has_key "key" ; then +# echo "key exists" +# fi +# +config_has_key() { + [[ $(config_get "$1") ]] +} diff --git a/examples/config-ini/test.sh b/examples/config-ini/test.sh index bb37cac3..75554036 100644 --- a/examples/config-ini/test.sh +++ b/examples/config-ini/test.sh @@ -8,4 +8,5 @@ bashly generate ./configly set hello world ./configly set bashly works ./configly get hello +./configly get invalid_key ./configly list diff --git a/lib/bashly/templates/lib/config.sh b/lib/bashly/templates/lib/config.sh index 83d5ee3c..4a5c1735 100644 --- a/lib/bashly/templates/lib/config.sh +++ b/lib/bashly/templates/lib/config.sh @@ -113,3 +113,14 @@ config_keys() { done < "$CONFIG_FILE" echo "${keys[@]}" } + +# Returns true if the specified key exists in the config file +# Usage: +# +# if config_has_key "key" ; then +# echo "key exists" +# fi +# +config_has_key() { + [[ $(config_get "$1") ]] +} diff --git a/spec/approvals/examples/config-ini b/spec/approvals/examples/config-ini index 66350cc4..a5483a9e 100644 --- a/spec/approvals/examples/config-ini +++ b/spec/approvals/examples/config-ini @@ -32,6 +32,8 @@ saved: hello = world saved: bashly = works + ./configly get hello world ++ ./configly get invalid_key +No such key: invalid_key + ./configly list hello = world bashly = works