]> git.ipfire.org Git - people/arne_f/network.git/blobdiff - functions.util
network: Magnificent changes on code.
[people/arne_f/network.git] / functions.util
index c0721900195e904f98190468930f79b9a1d2fb0b..83ec302c9812c8c24a141635ac8fa600cd7c0726 100644 (file)
@@ -21,7 +21,7 @@
 
 # Print a pretty error message
 function error() {
-       echo -e " ${FAIL}ERROR${NORMAL}  : $@" >&2
+       echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL}  : $@" >&2
 }
 
 function error_log() {
@@ -31,7 +31,7 @@ function error_log() {
 
 # Print a pretty warn message
 function warning() {
-       echo -e " ${WARN}WARNING${NORMAL}: $@" >&2
+       echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
 }
 
 function warning_log() {
@@ -39,6 +39,7 @@ function warning_log() {
        log WARNING "$@"
 }
 
+# XXX uses tr
 function listsort() {
        local i
        for i in $@; do
@@ -46,6 +47,31 @@ function listsort() {
        done | sort | tr "\n" " "
 }
 
+function listmatch() {
+       local match=${1}
+       shift
+
+       assert isset match
+
+       local i
+       for i in $@; do
+               [ "${match}" = "${i}" ] && return ${EXIT_OK}
+       done
+
+       return ${EXIT_ERROR}
+}
+
+function listlength() {
+       local length=0
+
+       local i
+       for i in $@; do
+               length=$(( ${length} + 1 ))
+       done
+
+       echo "${length}"
+}
+
 function config_read() {
        local config_file=${1}
 
@@ -181,7 +207,7 @@ function mac_is_valid() {
 }
 
 function uuid() {
-       cat /proc/sys/kernel/random/uuid
+       echo $(</proc/sys/kernel/random/uuid)
 }
 
 function isset() {
@@ -219,13 +245,106 @@ function ismac() {
        mac_is_valid ${mac}
 }
 
+function backtrace() {
+       local start=1
+
+       echo # Empty line
+       error_log "Backtrace (most recent call in first line):"
+
+       local i
+       for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
+               [ -z "${FUNCNAME[${i}]}" ] && continue
+               [ "${FUNCNAME[${i}]}" == "main" ] && continue
+
+               error_log "  $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
+       done
+}
+
 function assert() {
        local assertion="$@"
 
        if ! ${assertion}; then
                error_log "Assertion '${assertion}' failed."
+               backtrace
                exit ${EXIT_ERROR}
        fi
 
        return ${EXIT_OK}
 }
+
+function exec_cmd() {
+       local cmd=$@
+
+       log DEBUG "Running command: ${cmd}"
+
+       ${SHELL} ${cmd}
+       local ret=$?
+
+       #log DEBUG "Returned with code '${ret}'"
+
+       if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
+               error_log "Stopping parent process due to assertion error in child process: ${cmd}"
+               exit ${EXIT_ERROR_ASSERT}
+       fi
+
+       return ${ret}
+}
+
+function uppercase() {
+       local input
+       read input
+       echo "${input^^}"
+}
+
+function lowercase() {
+       local input
+       read input
+       echo "${input,,}"
+}
+
+function seq() {
+       if [ $# -eq 2 ]; then
+               eval echo {${1}..${2}}
+       elif [ $# -eq 3 ]; then
+               eval echo {${1}..${3}..${2}}
+       fi
+}
+
+function beautify_time() {
+       local value=${1}
+
+       local unit
+       local limit
+       for unit in s m h d w; do
+               case "${unit}" in
+                       s|m|h)
+                               limit=60
+                               ;;
+                       d)
+                               limit=24
+                               ;;
+                       w)
+                               limit=7
+                               ;;
+               esac
+
+               [ ${value} -lt ${limit} ] && break
+
+               value=$(( ${value} / ${limit} ))
+       done
+
+       echo "${value}${unit}"
+}
+
+function beautify_bytes() {
+       local value=${1}
+
+       local unit
+       local limit=1024
+       for unit in B k M G T; do
+               [ ${value} -lt ${limit} ] && break
+               value=$(( ${value} / ${limit} ))
+       done
+
+       echo "${value}${unit}"
+}