-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
services.sh
116 lines (100 loc) · 3.5 KB
/
services.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
# ==============================================================================
# Home Assistant Community Add-ons: Bashio
# Bashio is a bash function library for use with Home Assistant add-ons.
#
# It contains a set of commonly used operations and can be used
# to be included in add-on scripts to reduce code duplication across add-ons.
# ==============================================================================
# ------------------------------------------------------------------------------
# Get configuration object or configuration options from a service.
#
# Arguments:
# $1 Service name
# $2 Config option to get (optional)
# ------------------------------------------------------------------------------
function bashio::services() {
local service=${1}
local key=${2:-}
local cache_key="service.info.${service}"
local config
local query
local response
bashio::log.trace "${FUNCNAME[0]}" "$@"
if bashio::cache.exists "${cache_key}"; then
config=$(bashio::cache.get "${cache_key}")
else
config=$(bashio::api.supervisor GET "/services/${service}" false)
bashio::cache.set "${cache_key}" "${config}"
fi
response="${config}"
if bashio::var.has_value "${key}"; then
read -r -d '' query << QUERY
if (.${key} == null) then
null
elif (.${key} | type == "string") then
.${key} // empty
elif (.${key} | type == "boolean") then
.${key} // false
elif (.${key} | type == "array") then
if (.${key} == []) then
empty
else
.${key}[]
end
elif (.${key} | type == "object") then
if (.${key} == {}) then
empty
else
.${key}
end
else
.${key}
end
QUERY
response=$(bashio::jq "${config}" "${query}")
fi
printf "%s" "${response}"
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Check if this service is available.
#
# Arguments:
# $1 Service name
# ------------------------------------------------------------------------------
function bashio::services.available() {
local service=${1}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if ! bashio::api.supervisor GET "/services/${service}" > /dev/null 2>&1;
then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Publish a new configuration object for this service.
#
# Arguments:
# $1 Service name
# $2 Configuration object (JSON)
# ------------------------------------------------------------------------------
function bashio::services.publish() {
local service=${1}
local config=${2}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::api.supervisor "POST" "/services/${service}" "${config}"
bashio::cache.flush_all
}
# ------------------------------------------------------------------------------
# Deletes configuration object for this service.
#
# Arguments:
# $1 Service name
# ------------------------------------------------------------------------------
function bashio::services.delete() {
local service=${1}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::api.supervisor "DELETE" "/services/${service}"
bashio::cache.flush_all
}