Skip to content

Commit e856a7b

Browse files
committed
add config_has_key to the config stdlib
1 parent 105976d commit e856a7b

File tree

6 files changed

+50
-2
lines changed

6 files changed

+50
-2
lines changed

examples/config-ini/configly

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,17 @@ config_keys() {
283283
echo "${keys[@]}"
284284
}
285285

286+
# Returns true if the specified key exists in the config file
287+
# Usage:
288+
#
289+
# if config_has_key "key" ; then
290+
# echo "key exists"
291+
# fi
292+
#
293+
config_has_key() {
294+
[[ $(config_get "$1") ]]
295+
}
296+
286297
# :command.command_functions
287298
# :command.function
288299
configly_set_command() {
@@ -296,7 +307,13 @@ configly_set_command() {
296307
configly_get_command() {
297308
# :src/get_command.sh
298309
# Using the standard library (lib/config.sh) to show a value from the config
299-
config_get "${args[key]}"
310+
311+
key="${args[key]}"
312+
if config_has_key "$key" ; then
313+
config_get "$key"
314+
else
315+
echo "No such key: $key"
316+
fi
300317

301318
# Example of how to assign the config value to a variable:
302319
# result=$(config_get "${args[key]}")

examples/config-ini/src/get_command.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Using the standard library (lib/config.sh) to show a value from the config
2-
config_get "${args[key]}"
2+
3+
key="${args[key]}"
4+
if config_has_key "$key" ; then
5+
config_get "$key"
6+
else
7+
echo "No such key: $key"
8+
fi
39

410
# Example of how to assign the config value to a variable:
511
# result=$(config_get "${args[key]}")

examples/config-ini/src/lib/config.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,14 @@ config_keys() {
113113
done < "$CONFIG_FILE"
114114
echo "${keys[@]}"
115115
}
116+
117+
# Returns true if the specified key exists in the config file
118+
# Usage:
119+
#
120+
# if config_has_key "key" ; then
121+
# echo "key exists"
122+
# fi
123+
#
124+
config_has_key() {
125+
[[ $(config_get "$1") ]]
126+
}

examples/config-ini/test.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ bashly generate
88
./configly set hello world
99
./configly set bashly works
1010
./configly get hello
11+
./configly get invalid_key
1112
./configly list

lib/bashly/templates/lib/config.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,14 @@ config_keys() {
113113
done < "$CONFIG_FILE"
114114
echo "${keys[@]}"
115115
}
116+
117+
# Returns true if the specified key exists in the config file
118+
# Usage:
119+
#
120+
# if config_has_key "key" ; then
121+
# echo "key exists"
122+
# fi
123+
#
124+
config_has_key() {
125+
[[ $(config_get "$1") ]]
126+
}

spec/approvals/examples/config-ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ saved: hello = world
3232
saved: bashly = works
3333
+ ./configly get hello
3434
world
35+
+ ./configly get invalid_key
36+
No such key: invalid_key
3537
+ ./configly list
3638
hello = world
3739
bashly = works

0 commit comments

Comments
 (0)