]> git.ipfire.org Git - thirdparty/dehydrated.git/commitdiff
reworked dependency check and moved it up a bit in code (fixes #715, resolves #717...
authorLukas Schauer <lukas@schauer.so>
Tue, 28 Apr 2020 19:25:08 +0000 (21:25 +0200)
committerLukas Schauer <lukas@schauer.so>
Tue, 28 Apr 2020 19:25:08 +0000 (21:25 +0200)
dehydrated

index fe00b3ec58fc4b35262a2c0daad955cec4c5702f..e865e7125fb4f4f510bce12f6415530a527426d2 100755 (executable)
@@ -39,23 +39,20 @@ _mktemp() {
 
 # Check for script dependencies
 check_dependencies() {
-  # just execute some dummy and/or version commands to see if required tools exist and are actually usable
+  # look for required binaries
+  for binary in grep mktemp diff sed awk curl cut; do 
+    bin_path="$(command -v "${binary}" 2>/dev/null)" || _exiterr "This script requires ${binary}."
+    [[ -x "${bin_path}" ]] || _exiterr "${binary} found in PATH but it's not executable"
+  done
+
+  # just execute some dummy and/or version commands to see if required tools are actually usable
   "${OPENSSL}" version > /dev/null 2>&1 || _exiterr "This script requires an openssl binary."
   _sed "" < /dev/null > /dev/null 2>&1 || _exiterr "This script requires sed with support for extended (modern) regular expressions."
-  command -v grep > /dev/null 2>&1 || _exiterr "This script requires grep."
-  command -v mktemp > /dev/null 2>&1 || _exiterr "This script requires mktemp."
-  command -v diff > /dev/null 2>&1 || _exiterr "This script requires diff."
 
   # curl returns with an error code in some ancient versions so we have to catch that
-  # storing raw version output temporarily to catch curl error instead of head/awk exit codes
   set +e
-  raw_curl_version="$(curl -V 2>&1)"
-  retcode="$?"
+  CURL_VERSION="$(curl -V 2>&1 | head -n1 | awk '{print $2}')"
   set -e
-  if [[ ! "${retcode}" = "0" ]] && [[ ! "${retcode}" = "2" ]]; then
-    _exiterr "This script requires curl."
-  fi
-  CURL_VERSION="$(head -n1 <<< "${raw_curl_version}" | awk '{print $2}')"
 }
 
 store_configvars() {
@@ -192,6 +189,9 @@ load_config() {
     [[ -n "${ZSH_VERSION:-}" ]] && set -o noglob || set -f
   fi
 
+  # Check for missing dependencies
+  check_dependencies
+
   # Check if we are running & are allowed to run as root
   if [[ -n "$DEHYDRATED_USER" ]]; then
     command -v sudo > /dev/null 2>&1 || _exiterr "DEHYDRATED_USER set but sudo not available. Please install sudo."
@@ -218,9 +218,6 @@ load_config() {
     _exiterr "DEHYDRATED_GROUP can only be used in combination with DEHYDRATED_USER."
   fi
 
-  # Check for missing dependencies
-  check_dependencies
-
   # Remove slash from end of BASEDIR. Mostly for cleaner outputs, doesn't change functionality.
   [[ "$BASEDIR" != "/" ]] && BASEDIR="${BASEDIR%%/}"