Skip to content

Commit f788a0b

Browse files
authored
Fix various issues with bashio::addons() (#175)
The default `cache_key` and `filter` argument values in `bashio::addons()` caused problems when only a 'slug' was specified, or when an empty `cache_key` was specified with a non-empty `filter`. See #163 for more details. This PR adds more robust default value handling for those arguments. Fixes #163
1 parent d9f9ee5 commit f788a0b

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

lib/addons.sh

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,35 @@ function bashio::addon.changelog() {
138138
}
139139

140140
# ------------------------------------------------------------------------------
141-
# Returns a JSON object with generic version information about addons.
141+
# Returns a JSON object with information about addons.
142142
#
143143
# Arguments:
144144
# $1 Add-on slug (optional)
145-
# $1 Cache key to store results in (optional)
146-
# $2 jq Filter to apply on the result (optional)
145+
# (default/empty/'false' for all add-ons)
146+
# $2 Cache key to store filtered results in (optional)
147+
# (default/empty/'false' to cache only unfiltered results)
148+
# $3 jq filter to apply on the result (optional)
149+
# (default/empty is '.addons[].slug' with no slug or '.slug' with slug)
150+
# ('false' for no filtering)
147151
# ------------------------------------------------------------------------------
148152
function bashio::addons() {
149153
local slug=${1:-false}
150-
local cache_key=${2:-'addons.list'}
151-
local filter=${3:-'.addons[].slug'}
154+
local cache_key=${2:-false}
155+
local filter=${3:-}
156+
if bashio::var.is_empty "${filter}"; then
157+
if bashio::var.false "${slug}"; then
158+
filter='.addons[].slug'
159+
else
160+
filter='.slug'
161+
fi
162+
fi
152163
local info
153164
local response
154165

155166
bashio::log.trace "${FUNCNAME[0]}" "$@"
156167

157-
if bashio::cache.exists "${cache_key}"; then
168+
if ! bashio::var.false "${cache_key}" \
169+
&& bashio::cache.exists "${cache_key}"; then
158170
bashio::cache.get "${cache_key}"
159171
return "${__BASHIO_EXIT_OK}"
160172
fi
@@ -184,12 +196,14 @@ function bashio::addons() {
184196
fi
185197

186198
response="${info}"
187-
if bashio::var.has_value "${filter}"; then
199+
if ! bashio::var.false "${filter}"; then
188200
response=$(bashio::jq "${info}" "${filter}")
201+
if ! bashio::var.false "${cache_key}"; then
202+
bashio::cache.set "${cache_key}" "${response}"
203+
fi
189204
fi
190205

191-
bashio::cache.set "${cache_key}" "${response}"
192-
printf "%s" "${response}"
206+
printf '%s' "${response}"
193207

194208
return "${__BASHIO_EXIT_OK}"
195209
}

0 commit comments

Comments
 (0)