From 11d683cc8703e1e70083f27ac02df745caa30401 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 2 Apr 2020 02:12:23 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Adds=20new=20CLI=20features=20(#24)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/bashio.sh | 2 + lib/cli.sh | 203 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/os.sh | 55 -------------- 3 files changed, 205 insertions(+), 55 deletions(-) create mode 100644 lib/cli.sh diff --git a/lib/bashio.sh b/lib/bashio.sh index 352fc7e..f3c677b 100644 --- a/lib/bashio.sh +++ b/lib/bashio.sh @@ -51,6 +51,8 @@ source "${__BASHIO_LIB_DIR}/cache.sh" source "${__BASHIO_LIB_DIR}/addons.sh" # shellcheck source=lib/api.sh source "${__BASHIO_LIB_DIR}/api.sh" +# shellcheck source=lib/cli.sh +source "${__BASHIO_LIB_DIR}/cli.sh" # shellcheck source=lib/config.sh source "${__BASHIO_LIB_DIR}/config.sh" # shellcheck source=lib/core.sh diff --git a/lib/cli.sh b/lib/cli.sh new file mode 100644 index 0000000..9a51d58 --- /dev/null +++ b/lib/cli.sh @@ -0,0 +1,203 @@ +#!/usr/bin/env bash +# ============================================================================== +# Home Assistant Community Add-ons: Bashio +# Bashio is an 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() { + local version + local version_latest + + bashio::log.trace "${FUNCNAME[0]}" + + version=$(bashio::cli.version) + version_latest=$(bashio::cli.version_latest) + + if [[ "${version}" = "${version_latest}" ]]; then + return "${__BASHIO_EXIT_NOK}" + fi + + return "${__BASHIO_EXIT_OK}" +} + +# ------------------------------------------------------------------------------ +# 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' +} diff --git a/lib/os.sh b/lib/os.sh index 580c1ca..ce28a82 100644 --- a/lib/os.sh +++ b/lib/os.sh @@ -27,26 +27,6 @@ function bashio::os.update() { bashio::cache.flush_all } -# ------------------------------------------------------------------------------ -# Updates HassOS CLI to the latest version. -# -# Arguments: -# $1 Version to update to (optional) -# ------------------------------------------------------------------------------ -function bashio::os.update_cli() { - 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 /os/update/cli "${version}" - else - bashio::api.supervisor POST /os/update/cli - fi - bashio::cache.flush_all -} - # ------------------------------------------------------------------------------ # Load HassOS host configuration from USB stick. # ------------------------------------------------------------------------------ @@ -128,41 +108,6 @@ function bashio::os.update_available() { return "${__BASHIO_EXIT_OK}" } -# ------------------------------------------------------------------------------ -# Returns the CLI version of HassOS. -# ------------------------------------------------------------------------------ -function bashio::os.version_cli() { - bashio::log.trace "${FUNCNAME[0]}" - bashio::os 'os.info.version_cli' '.version_cli' -} - -# ------------------------------------------------------------------------------ -# Returns the latest CLI version of HassOS. -# ------------------------------------------------------------------------------ -function bashio::os.version_cli_latest() { - bashio::log.trace "${FUNCNAME[0]}" - bashio::os 'os.info.version_cli_latest' '.version_cli_latest' -} - -# ------------------------------------------------------------------------------ -# Checks if there is an update available for the Supervisor. -# ------------------------------------------------------------------------------ -function bashio::os.update_available_cli() { - local version - local last_version - - bashio::log.trace "${FUNCNAME[0]}" - - version=$(bashio::os.version_cli) - last_version=$(bashio::os.version_cli_latest) - - if [[ "${version}" = "${last_version}" ]]; then - return "${__BASHIO_EXIT_NOK}" - fi - - return "${__BASHIO_EXIT_OK}" -} - # ------------------------------------------------------------------------------ # Returns the board running HassOS. # ------------------------------------------------------------------------------