Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6247fc3bf1f151fd89bc241a6898e0ab27527517 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash

# General purpose utilities that are only related to bash (not 
# the build, per se.

# A general purpose utility to check number of arguments match
# what was expected. Its purpose is to help bullet-proof these scripts
# when future changes made.
# Example: (such as in first line of a function)
#   checkNArgs $# 3
# NOTE: can not be called, until buildDirectory has been defined.
checkNArgs ()
{
  actual=$1
  expected=$2
  if [[ -z "$actual" || -z "$expected" ]]
  then
    BUILD_FAILED="${buildDirectory}/buildFailed-program-error"
    printf "\n\tPROGRAM ERROR: number of arguments, $actual, or number expected, $expected, was not provided as arguments.\n\n" >${BUILD_FAILED}
    printf "             Called from ${FUNCNAME[1]}, called from line number ${BASH_LINENO[1]} in ${BASH_SOURCE[2]}.\n\n" >>${BUILD_FAILED}
    return 9
  fi
  # possible is used when there are optional arguments, corresponds to "max possible", then "expected" means "min possible"
  possible=$3
  #echo "     DEBUG checkNArgs funcName[1]: ${FUNCNAME[1]}" >> ${TRACE_OUTPUT}
  #echo "     DEBUG checkNArgs bashSource[2]:   ${BASH_SOURCE[2]}" >> ${TRACE_OUTPUT}
  #echo "     DEBUG checkNArgs bashLineNo[1]:   ${BASH_LINENO[1]}" >> ${TRACE_OUTPUT}
  if [[ -n "${possible}" ]]
  then
    # if 3 total arguments, make sure first is between 2 and 3rd args (inclusive)
    #echo DEBUG: actual:   $actual
    #echo DEBUG: min expected: $expected
    #echo DEBUG: max possible: $possible
    #arg1=$(( $expected <= $actual ))
    #arg2=$(( $actual <= $possible ))
    #echo "DEBUG: expected <= actual $arg1"
    #echo "DEBUG: actual <= possible $arg2"
    if  (( $expected <= $actual )) && (( $actual <= $possible ))
    then
      #echo DEBUG: return 0
      return 0
    else
      BUILD_FAILED="${buildDirectory}/buildFailed-program-error"
      printf "\n\tPROGRAM ERROR: number of arguments, $actual, was not betwen expected, $expected, and possible, $possible.\n" >${BUILD_FAILED}
      printf "             Called from ${FUNCNAME[1]}, called from line number ${BASH_LINENO[1]} in ${BASH_SOURCE[2]}.\n\n" >>${BUILD_FAILED}
      #echo DEBUG: return 1
      return 1
    fi
  elif [[ $actual != $expected ]]
  then
    # depends on buildDirectory being exported
    BUILD_FAILED="${buildDirectory}/buildFailed-program-error"
    printf "\n\tPROGRAM ERROR: expected $expected arguments but was passed $actual.\n" >${BUILD_FAILED}
    printf "             Called from ${FUNCNAME[1]}, called from line number ${BASH_LINENO[1]} in ${BASH_SOURCE[2]}.\n\n" >>${BUILD_FAILED}
    return 1
  else
    return 0
  fi
}

# general purpose utility for "hard exit" if return code not zero.
# especially useful to call/check after basic things that should normally
# easily succeeed.
# usage:
#   checkForErrorExit $? "Failed to copy file (for example)"
checkForErrorExit ()
{
  # arg 1 must be return code, $?
  # arg 2 (remaining line) can be message to print before exiting do to non-zero exit code
  exitCode=$1
  shift
  message="$*"
  if [[ -z "${exitCode}" ]]
  then
    echo "PROGRAM ERROR: checkForErrorExit called with no arguments"
    exit 1
  fi

  if [[ -z "${message}" ]]
  then
    echo "WARNING: checkForErrorExit called without message"
    message="(Calling program provided no message)"
  fi

  # first make sure exit code is well formed
  if [[ "${exitCode}" =~ [0] ]]
  then
    #echo "exitcode was zero"
    exitrc=0
  else
    if [[ "${exitCode}" =~ ^-?[0-9]+$ ]]
    then
      #echo "exitcode was indeed a legal, non-zero numeric return code"
      exitrc=$exitCode
    else
      #echo "exitode was not numeric, so will force to 1"
      exitrc=1
    fi
  fi

  if [[ $exitrc != 0 ]]
  then
    echo
    echo "   ERROR. exit code: ${exitrc}"
    echo "   ERROR. message: ${message}"
    echo
    exit $exitrc
  fi
}

# A general purpose utility to check if variable is 
# undefinded or empty string and exit if so, 
# printing a useful diagnosic trace if variable was empty.
# NOTE: only the variable NAME should be passed as argument, 
# note its value. Such as 
#   assertNotEmpty BUILD_ID
# not
#   assertNotEmpty $BUILD_ID
assertNotEmpty ()
{
  VAR_NAME=$1

  #echo "DEBUG: VAR_NAME: $VAR_NAME" >&2
  #echo "DEBUG: \$VAR_NAME: ${!VAR_NAME}" >&2
  if [[ -z "${!VAR_NAME}" ]]
  then 
    printf "\n\tPROGRAM ERROR: %s\n" "${VAR_NAME} was unexpectedly empty or undefined." 
    printf "\t%s\n\n" "in ${FUNCNAME[1]}, called from line number ${BASH_LINENO[0]} in ${BASH_SOURCE[1]}."
    exit 1
  else
    printf "\n\t%s:\t%s\n" "${VAR_NAME}" "${!VAR_NAME}"
  fi  

}

Back to the top