-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
jq.sh
173 lines (146 loc) · 5.16 KB
/
jq.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/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.
# ==============================================================================
# ------------------------------------------------------------------------------
# Execute a JSON query.
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter (optional)
# ------------------------------------------------------------------------------
function bashio::jq() {
local data=${1}
local filter=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if [[ -f "${data}" ]]; then
jq --raw-output -c -M "$filter" "${data}"
else
jq --raw-output -c -M "$filter" <<< "${data}"
fi
}
# ------------------------------------------------------------------------------
# Checks if variable exists (optionally after filtering).
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter (optional)
# ------------------------------------------------------------------------------
function bashio::jq.exists() {
local data=${1}
local filter=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if [[ $(bashio::jq "${data}" "${filter}") = "null" ]]; then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if data exists (optionally after filtering).
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter (optional)
# ------------------------------------------------------------------------------
function bashio::jq.has_value() {
local data=${1}
local filter=${2:-}
local value
bashio::log.trace "${FUNCNAME[0]}:" "$@"
value=$(bashio::jq "${data}" \
"${filter} | if (. == {} or . == []) then empty else . end // empty")
if ! bashio::var.has_value "${value}"; then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if resulting data is of a specific type.
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter
# $3 type (boolean, string, number, array, object, null)
# ------------------------------------------------------------------------------
function bashio::jq.is() {
local data=${1}
local filter=${2}
local type=${3}
local value
bashio::log.trace "${FUNCNAME[0]}:" "$@"
value=$(bashio::jq "${data}" \
"${filter} | if type==\"${type}\" then true else false end")
if [[ "${value}" = "false" ]]; then
return "${__BASHIO_EXIT_NOK}"
fi
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Checks if resulting data is a boolean.
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter (optional)
# ------------------------------------------------------------------------------
function bashio::jq.is_boolean() {
local data=${1}
local filter=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::jq.is "${data}" "${filter}" "boolean"
}
# ------------------------------------------------------------------------------
# Checks if resulting data is a string.
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter (optional)
# ------------------------------------------------------------------------------
function bashio::jq.is_string() {
local data=${1}
local filter=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::jq.is "${data}" "${filter}" "string"
}
# ------------------------------------------------------------------------------
# Checks if resulting data is an object.
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter (optional)
# ------------------------------------------------------------------------------
function bashio::jq.is_object() {
local data=${1}
local filter=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::jq.is "${data}" "${filter}" "object"
}
# ------------------------------------------------------------------------------
# Checks if resulting data is a number.
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter (optional)
# ------------------------------------------------------------------------------
function bashio::jq.is_number() {
local data=${1}
local filter=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::jq.is "${data}" "${filter}" "number"
}
# ------------------------------------------------------------------------------
# Checks if resulting data is an array.
#
# Arguments:
# $1 JSON string or path to a JSON file
# $2 jq filter (optional)
# ------------------------------------------------------------------------------
function bashio::jq.is_array() {
local data=${1}
local filter=${2:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
bashio::jq.is "${data}" "${filter}" "array"
}