mirror of
https://github.com/hassio-addons/bashio.git
synced 2025-05-06 20:11:24 +00:00
✨ 🎉 Initial library code
This commit is contained in:
parent
f91b4cbaa9
commit
2346cddca2
26 changed files with 4499 additions and 6 deletions
89
lib/exit.sh
Normal file
89
lib/exit.sh
Normal file
|
@ -0,0 +1,89 @@
|
|||
#!/usr/bin/env bash
|
||||
# ==============================================================================
|
||||
# Community Hass.io Add-ons: Bashio
|
||||
# Bashio is an bash function library for use with Hass.io 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.
|
||||
# ==============================================================================
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Exit the script with as failed with an optional error message.
|
||||
#
|
||||
# Arguments:
|
||||
# $1 Error message (optional)
|
||||
# ------------------------------------------------------------------------------
|
||||
function bashio::exit.nok() {
|
||||
local message=${1:-}
|
||||
|
||||
bashio::log.trace "${FUNCNAME[0]}:" "$@"
|
||||
|
||||
if bashio::var.has_value "${message}"; then
|
||||
bashio::log.fatal "${message}"
|
||||
fi
|
||||
|
||||
exit "${__BASHIO_EXIT_NOK}"
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Exit the script when given value is false, with an optional error message.
|
||||
#
|
||||
# Arguments:
|
||||
# $1 Value to check if false
|
||||
# $2 Error message (optional)
|
||||
# ------------------------------------------------------------------------------
|
||||
function bashio::exit.die_if_false() {
|
||||
local value=${1:-}
|
||||
local message=${2:-}
|
||||
|
||||
bashio::log.trace "${FUNCNAME[0]}:" "$@"
|
||||
|
||||
if bashio::var.false "${value}"; then
|
||||
bashio::exit.nok "${message}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Exit the script when given value is true, with an optional error message.
|
||||
#
|
||||
# Arguments:
|
||||
# $1 Value to check if true
|
||||
# $2 Error message (optional)
|
||||
# ------------------------------------------------------------------------------
|
||||
function hass.die_if_true() {
|
||||
local value=${1:-}
|
||||
local message=${2:-}
|
||||
|
||||
bashio::log.trace "${FUNCNAME[0]}:" "$@"
|
||||
|
||||
if bashio::var.true "${value}"; then
|
||||
bashio::exit.nok "${message}"
|
||||
fi
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Exit the script when given value is empty, with an optional error message.
|
||||
#
|
||||
# Arguments:
|
||||
# $1 Value to check if true
|
||||
# $2 Error message (optional)
|
||||
# ------------------------------------------------------------------------------
|
||||
function hass.die_if_empty() {
|
||||
local value=${1:-}
|
||||
local message=${2:-}
|
||||
|
||||
bashio::log.trace "${FUNCNAME[0]}:" "$@"
|
||||
|
||||
if bashio::var.is_empty "${value}"; then
|
||||
bashio::exit.nok "${message}"
|
||||
fi
|
||||
}
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
# Exit the script nicely.
|
||||
# ------------------------------------------------------------------------------
|
||||
function bashio::exit.ok() {
|
||||
bashio::log.trace "${FUNCNAME[0]}" "$@"
|
||||
exit "${__BASHIO_EXIT_OK}"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue