diff options
author | Jordan Bracco <href@random.sh> | 2022-03-22 21:36:58 +0000 |
---|---|---|
committer | Jordan Bracco <href@random.sh> | 2022-03-22 21:36:58 +0000 |
commit | 435531e42f3d1b1d0de291071aedb111d727e57d (patch) | |
tree | f51ae7f59750b41f6ac416e01f102801a1c420b6 /lib |
Initial PoC
Diffstat (limited to 'lib')
-rw-r--r-- | lib/datetime.sh | 10 | ||||
-rw-r--r-- | lib/dependencies.sh | 12 | ||||
-rw-r--r-- | lib/identifiers.sh | 10 | ||||
-rw-r--r-- | lib/os.sh | 58 |
4 files changed, 90 insertions, 0 deletions
diff --git a/lib/datetime.sh b/lib/datetime.sh new file mode 100644 index 0000000..7f67a49 --- /dev/null +++ b/lib/datetime.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +# date_iso_utc +date_now_iso_utc() { + TZ=GMT date +"%Y-%m-%dT%H:%M:%SZ" +} + +posix_time_microseconds() { + bc -e "($(perl -MTime::HiRes=gettimeofday -MPOSIX=strftime -e '($s,$us) = gettimeofday(); printf "%d.%06d\n", $s, $us') * 1000)" | cut -d '.' -f 1 +} diff --git a/lib/dependencies.sh b/lib/dependencies.sh new file mode 100644 index 0000000..bb86be3 --- /dev/null +++ b/lib/dependencies.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env sh + +ensure_command_exists() { + _cmd=$1 + _ret=0 + if [ -z "${_cmd}" ]; then + _execution_error error=missing_argument function=ensure_command_exists argument=cmd + fi + if ! which "$1" > /dev/null 2>&1; then + _execution_error error=missing_dependency dependency="${_cmd}" + fi +} diff --git a/lib/identifiers.sh b/lib/identifiers.sh new file mode 100644 index 0000000..cc7bc01 --- /dev/null +++ b/lib/identifiers.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env sh + +# make_random_identifier [SIZE=32] +# Generate a random alphanumeric identifier using `/dev/urandom`. +# id=$(make_random_identifier) +# id=$(make_random_identifier 64) +make_random_identifier() { + _size="${1:-32}" + </dev/urandom env LC_CTYPE=C tr -dc "[:alnum:]" | head -c "$_size" +} diff --git a/lib/os.sh b/lib/os.sh new file mode 100644 index 0000000..211bfaa --- /dev/null +++ b/lib/os.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env sh +set -e + +export JSON_OS= + +if [ -f /etc/os-release ]; then + . /etc/os-release + _os_type="${NAME}" + _os_name="${ID}" + _os_pretty_name="${PRETTY_NAME}" + _os_version="${VERSION}" + _os_version_id="${VERSION_ID}" + _os_supported=1 +else + uname=$(uname | tr '[:upper:]' '[:lower:]') + case "${uname}" in + darwin) + _os_type="macos" + >&2 echo "${0}: error: macos not done yet!" + exit 1;; + *) + _os_type="${uname}" + >&2 echo "cloyster/os_detect.sh: error: unknown operating system: '${uname}'" + esac +fi + +_m=$(uname -m) +case "${_m}" in +x86_64) _os_machine=amd64;; +i*86) _os_machine=x86;; +*) + _os_machine="${_m}" + ;; +esac + +JSON_OS=$(jo -d. \ + "os.supported@${_os_supported}" \ + "os.type=${_os_type}" \ + "os.name=${_os_name}" \ + "os.pretty_name=${_os_pretty_name}" \ + "os.version=${_os_version}" \ + "os.version_id=${_os_version_id}" \ + "os.machine=${_os_machine}") + +# os_get KEY|jq [JQ_QUERY] +os_get() { + _key="${1}" + if [ -z "${1}" ]; then + _key=".os" + elif [ "${1}" = "jq" ] && [ -n "${2}" ]; then + _key="${2}" + else + _key=".os.${_key}" + fi + value="$(echo ${JSON_OS} | jq -r "${_key}")" + if [ "${value}" = "null" ]; then value=; fi + echo "${value}" +} |