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/os.sh |
Initial PoC
Diffstat (limited to 'lib/os.sh')
-rw-r--r-- | lib/os.sh | 58 |
1 files changed, 58 insertions, 0 deletions
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}" +} |