From ef79f9623a4ed81e152b774daf8be8388ac9b7fe Mon Sep 17 00:00:00 2001 From: David Kebler Date: Thu, 23 Feb 2023 11:16:55 -0800 Subject: [PATCH] refactor get distro to work more universally --- modules/host-utils.mod | 44 ++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/modules/host-utils.mod b/modules/host-utils.mod index 3a01479..359b7e1 100644 --- a/modules/host-utils.mod +++ b/modules/host-utils.mod @@ -47,29 +47,31 @@ hostinfo () { } -function get_distro() { -source /etc/os-release -declare valid=${@:-"arch alpine debian ubuntu"} -# echo $ID $ID_LIKE -[[ "${valid}" =~ $ID ]] && echo $ID && return 0 -[[ "${valid}" =~ $ID_LIKE ]] && echo $ID_LIKE && return 0 -return 1 -} +# function get_distro() { +# source /etc/os-release +# declare distros=${@:-"arch alpine debian ubuntu"} +# # echo $ID $ID_LIKE +# for distro in $distros; do +# echo $distro $ID $ID_LIKE +# [[ $ID =~ $distro ]] && echo $distro && return 0 +# [[ $ID_LIKE =~ $distro ]] && echo $distro && return 0 +# done +# return 1 +# } # function get_release() { # } -# get_distro() { -# local file=/etc/upstream-release/lsb-release -# local distro -# if [[ -f $file ]]; then -# echo $(cat $file | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq) -# return 0 -# else -# file=/etc/os-release -# if [[ -f $file ]]; then -# echo $(cat $file | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq) -# fi -# fi -# } +get_distro() { + local file=/etc/upstream-release/lsb-release + if [[ -f $file ]]; then + echo $(cat $file | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq) + return 0 + else + file=/etc/os-release + if [[ -f $file ]]; then + echo $(cat $file | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq) + fi + fi +}