From bd1c6a7bcdbe10cb89b623015e7018282a5d7635 Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Sat, 28 Mar 2020 04:50:21 +0100 Subject: Show commands to install deps on supported systems --- etc/bindeps.d/gnu_linux-ubuntu.txt | 2 ++ etc/pacmans.txt | 1 + etc/perlmans.txt | 1 + lib/main.bash | 64 +++++++++++++++++++++++++++++++++++--- lib/util.bash | 45 ++++++++++++++++++++++++++- 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 etc/bindeps.d/gnu_linux-ubuntu.txt create mode 100644 etc/pacmans.txt create mode 100644 etc/perlmans.txt diff --git a/etc/bindeps.d/gnu_linux-ubuntu.txt b/etc/bindeps.d/gnu_linux-ubuntu.txt new file mode 100644 index 0000000..6d60a0e --- /dev/null +++ b/etc/bindeps.d/gnu_linux-ubuntu.txt @@ -0,0 +1,2 @@ +gcc=gcc +make=make diff --git a/etc/pacmans.txt b/etc/pacmans.txt new file mode 100644 index 0000000..acec4ff --- /dev/null +++ b/etc/pacmans.txt @@ -0,0 +1 @@ +gnu_linux-ubuntu=apt update && apt install -y diff --git a/etc/perlmans.txt b/etc/perlmans.txt new file mode 100644 index 0000000..9ffbacc --- /dev/null +++ b/etc/perlmans.txt @@ -0,0 +1 @@ +gnu_linux-ubuntu=apt install -y cpanminus && cpanm -v diff --git a/lib/main.bash b/lib/main.bash index 42fdd6e..440cda5 100644 --- a/lib/main.bash +++ b/lib/main.bash @@ -1,7 +1,7 @@ #!/usr/bin/env bash -source "$(dirname "$BASH_SOURCE")/util.bash" -source "$(dirname "$BASH_SOURCE")/logging.bash" +source "$(dirname "${BASH_SOURCE[0]}")/util.bash" +source "$(dirname "${BASH_SOURCE[0]}")/logging.bash" main() { [[ -z $1 ]] && usage && exit 2 @@ -92,6 +92,7 @@ EOF # $PATH environment variable. depcheck_bin() { local missing=() + local bindep_db for tool in "${RSTAR_DEPS_BIN[@]}" do @@ -107,11 +108,44 @@ depcheck_bin() { for tool in "${missing[@]}" do - # TODO: Include current distro's package name - # containing the tool alert " $tool" done + # Resolve the basename for the bindep file + bindep_db="bindeps.d/${RSTAR_PLATFORM[key]}.txt" + + debug "bindep_db resolved to $bindep_db" + + # If there's a bindep file, use it to resolve the missing utils + # into a list of package names. + if [[ -f "etc/$bindep_db" ]] + then + local packages + local pacman_cmd + + debug "bindep_db found" + + # Create a list of packages that needs to be installed + for tool in "${missing[@]}" + do + local package="$(config_etc_kv "$bindep_db" "$tool")" + + # Don't add duplicates + in_args "$package" "${packages[@]}" && continue + + packages+=("$package") + done + + # Figure out which package manager command install on + # the current platform. + pacman_cmd="$(config_etc_kv pacmans.txt "${RSTAR_PLATFORM[key]}") " + + # Tell the user of the command to install missing + # dependencies + info "The missing tools can be installed using your system package manager:" + info "$pacman_cmd$(join_args -c " " "${packages[@]}")" + fi + return 1 fi } @@ -137,6 +171,9 @@ depcheck_perl() { alert " $module" done + info "The missing Perl modules can be installed using this command:" + info "$(config_etc_kv perlmans.txt "${RSTAR_PLATFORM[key]}") $(join_args -c " " "${missing[@]}")" + return 1 fi } @@ -154,9 +191,17 @@ discover_system() { RSTAR_PLATFORM["kernel"]="$(discover_system_kernel)" RSTAR_PLATFORM["kernel_version"]="$(discover_system_kernel_version)" fi + + RSTAR_PLATFORM[key]="$(discover_system_key)" } discover_system_distro() { + if [[ -f /etc/os-release ]] + then + printf "%s" "$(source /etc/os-release && printf "%s" "$NAME" | awk '{print tolower($0)}')" + return + fi + awk -F= '$1 == "NAME" {print tolower($2);q}' /etc/*release } @@ -168,6 +213,17 @@ discover_system_kernel_version() { printf "%s" "$(uname -r | awk '{print tolower($0)}')" } +discover_system_key() { + key+="${RSTAR_PLATFORM[os]}" + + if [[ ${RSTAR_PLATFORM[distro]} ]] + then + key+="-${RSTAR_PLATFORM[distro]}" + fi + + printf "%s" "$key" +} + discover_system_os() { if command -v uname > /dev/null then diff --git a/lib/util.bash b/lib/util.bash index 1cf84f4..eb7c9a8 100644 --- a/lib/util.bash +++ b/lib/util.bash @@ -9,6 +9,8 @@ chgdir() { # Read a particular value from a key/value configuration file. Using this # function introduces a dependency on awk. config_etc_kv() { + local value + local file="$BASEDIR/etc/$1" shift @@ -20,7 +22,14 @@ config_etc_kv() { debug "Reading value for $1 from $file" - awk -F= '$1 == "'"$1"'" { print $NF }' "$file" + value="$(awk -F= '$1 == "'"$1"'" { print $NF }' "$file")" + + if [[ -z $value ]] + then + crit "Empty value for $1 from $file?" + fi + + printf "%s" "$value" } # Log a message as error, and exit the program. This is intended for serious @@ -84,6 +93,40 @@ fetch_http_wget() { wget --quiet --output-document "$2" "$1" } +# Check if the first argument given appears in the list of all following +# arguments. +in_args() { + local needle="$1" + shift + + for arg in "$@" + do + [[ $needle == $arg ]] && return 0 + done + + return 1 +} + +# Join a list of arguments into a single string. By default, this will join +# using a ",", but you can set a different character using -c. Note that this +# only joins with a single character, not a string of characters. +join_args() { + local OPTIND + local IFS="," + + while getopts ":c:" opt + do + case "$opt" in + c) IFS="$OPTARG" ;; + *) warn "Unused opt specified: $opt" ;; + esac + done + + shift $(( OPTIND - 1)) + + printf "%s" "$*" +} + # Pretty print a duration between a starting point (in seconds) and an end # point (in seconds). If no end point is given, the current time will be used. # A good way to get a current timestamp in seconds is through date's "%s" -- cgit v1.1