-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcli.sh
192 lines (164 loc) · 6.9 KB
/
cli.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/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.
# ==============================================================================
# ------------------------------------------------------------------------------
# Updates the CLI to the latest version.
#
# Arguments:
# $1 Version to update to (optional)
# ------------------------------------------------------------------------------
function bashio::cli.update() {
local version=${1:-}
bashio::log.trace "${FUNCNAME[0]}:" "$@"
if bashio::var.has_value "${version}"; then
version=$(bashio::var.json version "${version}")
bashio::api.supervisor POST /cli/update "${version}"
else
bashio::api.supervisor POST /cli/update
fi
bashio::cache.flush_all
}
# ------------------------------------------------------------------------------
# Returns a JSON object with generic version information about the CLI.
#
# Arguments:
# $1 Cache key to store results in (optional)
# $2 jq Filter to apply on the result (optional)
# ------------------------------------------------------------------------------
function bashio::cli() {
local cache_key=${1:-'cli.info'}
local filter=${2:-}
local info
local response
bashio::log.trace "${FUNCNAME[0]}" "$@"
if bashio::cache.exists "${cache_key}"; then
bashio::cache.get "${cache_key}"
return "${__BASHIO_EXIT_OK}"
fi
if bashio::cache.exists 'cli.info'; then
info=$(bashio::cache.get 'cli.info')
else
info=$(bashio::api.supervisor GET /cli/info false)
bashio::cache.set 'cli.info' "${info}"
fi
response="${info}"
if bashio::var.has_value "${filter}"; then
response=$(bashio::jq "${info}" "${filter}")
fi
bashio::cache.set "${cache_key}" "${response}"
printf "%s" "${response}"
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Returns the Home Assistant CLI version used.
# ------------------------------------------------------------------------------
function bashio::cli.version() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli 'cli.info.version' '.version'
}
# ------------------------------------------------------------------------------
# Returns the latest version of the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.version_latest() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli 'cli.info.version_latest' '.version_latest'
}
# ------------------------------------------------------------------------------
# Checks if there is an update available for the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.update_available() {
bashio::log.trace "${FUNCNAME[0]}" "$@"
bashio::cli 'cli.info.update_available' '.update_available // false'
}
# ------------------------------------------------------------------------------
# List all available stats about the CLI.
#
# Arguments:
# $1 Cache key to store results in (optional)
# $2 jq Filter to apply on the result (optional)
# ------------------------------------------------------------------------------
function bashio::cli.stats() {
local cache_key=${1:-'cli.stats'}
local filter=${2:-}
local info
local response
bashio::log.trace "${FUNCNAME[0]}" "$@"
if bashio::cache.exists "${cache_key}"; then
bashio::cache.get "${cache_key}"
return "${__BASHIO_EXIT_OK}"
fi
if bashio::cache.exists 'cli.stats'; then
info=$(bashio::cache.get 'cli.stats')
else
info=$(bashio::api.supervisor GET /cli/stats false)
bashio::cache.set 'cli.stats' "${info}"
fi
response="${info}"
if bashio::var.has_value "${filter}"; then
response=$(bashio::jq "${info}" "${filter}")
fi
bashio::cache.set "${cache_key}" "${response}"
printf "%s" "${response}"
return "${__BASHIO_EXIT_OK}"
}
# ------------------------------------------------------------------------------
# Returns CPU usage from the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.cpu_percent() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli.stats 'cli.stats.cpu_percent' '.cpu_percent'
}
# ------------------------------------------------------------------------------
# Returns memory usage from the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.memory_usage() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli.stats 'cli.stats.memory_usage' '.memory_usage'
}
# ------------------------------------------------------------------------------
# Returns memory limit from the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.memory_limit() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli.stats 'cli.stats.memory_limit' '.memory_limit'
}
# ------------------------------------------------------------------------------
# Returns memory usage in percent from the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.memory_percent() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli.stats 'cli.stats.memory_percent' '.memory_percent'
}
# ------------------------------------------------------------------------------
# Returns outgoing network usage from the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.network_tx() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli.stats 'cli.stats.network_tx' '.network_tx'
}
# ------------------------------------------------------------------------------
# Returns incoming network usage from the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.network_rx() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli.stats 'cli.stats.network_rx' '.network_rx'
}
# ------------------------------------------------------------------------------
# Returns disk read usage from the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.blk_read() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli.stats 'cli.stats.blk_read' '.blk_read'
}
# ------------------------------------------------------------------------------
# Returns disk write usage from the CLI.
# ------------------------------------------------------------------------------
function bashio::cli.blk_write() {
bashio::log.trace "${FUNCNAME[0]}"
bashio::cli.stats 'cli.stats.blk_write' '.blk_write'
}