Skip to content

Commit f5ce9bc

Browse files
committed
refactor getopts to make args work similar to api.supervisor
1 parent 5763af9 commit f5ce9bc

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

  • tailscale/rootfs

tailscale/rootfs/etc/s6-overlay/s6-rc.d/post-tailscaled/run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ if keyexpiry=$(/opt/tailscale status --self=true --peers=false --json | jq -rce
158158
"Consider disabling key expiry to avoid losing connection to your Home Assistant device." \
159159
"You can use the [Machines page](https://login.tailscale.com/admin/machines) of Tailscale's admin console to disable key expiry." \
160160
"Please check your configuration based on the app's documentation under the \"Configuration\" section.")")
161-
if api.core -w POST services/persistent_notification/create "${attributes}" > /dev/null; then
161+
if api.core POST services/persistent_notification/create "${attributes}" -w > /dev/null; then
162162
touch "/data/key_expiry_notification_is_done"
163163
fi
164164
fi

tailscale/rootfs/usr/lib/api.core.sh

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,41 @@ function log.error_or_warning() {
1414
# Makes a call to the Home Assistant REST API.
1515
#
1616
# Arguments:
17-
# $1 HTTP Method (GET/POST)
18-
# $2 API Resource requested
19-
# $3 In case of a POST method, this parameter is the JSON to POST (optional)
17+
# $1 HTTP Method (GET/POST)
18+
# $2 API Resource requested
19+
# $3 In case of a POST method, this parameter is the JSON to POST (optional)
20+
# In case of a GET method, this parameter is not present
21+
# $3/4 jq filter command (optional)
2022
#
21-
# Options (BEFORE arguments):
22-
# -f <filter> jq filter command
23-
# -s "Silent" Supress error message in case of a 404 Not found HTTP status code
24-
# -w "Warning only" Log only warnings instead of errors
23+
# Options (after arguments):
24+
# -s "Silent" Supress error message in case of a 404 Not found HTTP status code
25+
# -w "Warning only" Log only warnings instead of errors
2526
# ------------------------------------------------------------------------------
2627
function api.core() {
28+
# read arguments
29+
local method="${1}"; shift
30+
local resource="/core/api/${1}"; shift
31+
local data="{}"
32+
if [[ "${method}" = "POST" && -n "${1++}" && "${1::1}" != "-" ]]; then
33+
data="${1}"; shift
34+
fi
35+
local filter=
36+
if [[ -n "${1++}" && "${1::1}" != "-" ]]; then
37+
filter="${1}"; shift
38+
fi
39+
2740
# read options
2841
local OPTIND o
2942
local silent=
3043
local warning_only=
31-
local filter=
32-
33-
while getopts ":swf:" o; do
44+
while getopts ":sw" o; do
3445
case "${o}" in
3546
s)
3647
silent=1
3748
;;
3849
w)
3950
warning_only=1
4051
;;
41-
f)
42-
filter="$OPTARG"
43-
;;
4452
\?)
4553
bashio::log.error "Invalid option: -${OPTARG}"
4654
return "${__BASHIO_EXIT_NOK}"
@@ -55,11 +63,6 @@ function api.core() {
5563
done
5664
shift $((OPTIND-1))
5765

58-
# read arguments
59-
local method="${1}"
60-
local resource="/core/api/${2}"
61-
local data="${3:-{\}}"
62-
6366
local auth_header='Authorization: Bearer'
6467
local response
6568
local status

0 commit comments

Comments
 (0)