|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit -o errtrace -o nounset -o pipefail |
| 4 | + |
| 5 | +inline_source() { |
| 6 | + |
| 7 | + # ============================================================================== |
| 8 | + # Restricting user-defined exit codes to the range 64 - 113 (in addition to 0, |
| 9 | + # for success) allows for 50 exit codes. The range 114-125 *could* be utilized |
| 10 | + # (adding another ten codes) but DO NOT GO BEYOND 126! |
| 11 | + # ============================================================================== |
| 12 | + |
| 13 | + # ============================================================================== |
| 14 | + # ------------------------------------------------------------------------------ |
| 15 | + : readonly -i "${EXIT_OK:=0}" |
| 16 | + # ============================================================================== |
| 17 | + |
| 18 | + # ============================================================================== |
| 19 | + # Generic errors (60 range) |
| 20 | + # ----------------------------------------------------------------------------- |
| 21 | + : readonly -i "${EXIT_UNKNOWN_ERROR_OCCURRED:=64}" |
| 22 | + : readonly -i "${EXIT_NOT_ENOUGH_PARAMETERS:=65}" |
| 23 | + : readonly -i "${EXIT_INVALID_PARAMETER:=66}" |
| 24 | + : readonly -i "${EXIT_DEPENDENCY_MISSING:=67}" |
| 25 | + : readonly -i "${EXIT_ENVIRONMENT_VARIABLE_MISSING:=68}" |
| 26 | + # ============================================================================== |
| 27 | + |
| 28 | + # ============================================================================== |
| 29 | + # File and folder errors (70 range) |
| 30 | + # ------------------------------------------------------------------------------ |
| 31 | + : readonly -i "${EXIT_COULD_NOT_FIND_FILE:=70}" |
| 32 | + : readonly -i "${EXIT_COULD_NOT_CREATE_FILE:=71}" |
| 33 | + : readonly -i "${EXIT_COULD_NOT_UPDATE_FILE:=72}" |
| 34 | + : readonly -i "${EXIT_COULD_NOT_DELETE_FILE:=73}" |
| 35 | + : readonly -i "${EXIT_COULD_NOT_MOVE_FILE:=74}" |
| 36 | + : readonly -i "${EXIT_COULD_NOT_FIND_DIRECTORY:=75}" |
| 37 | + : readonly -i "${EXIT_COULD_NOT_CREATE_DIRECTORY:=76}" |
| 38 | + : readonly -i "${EXIT_COULD_NOT_UPDATE_DIRECTORY:=77}" |
| 39 | + : readonly -i "${EXIT_COULD_NOT_DELETE_DIRECTORY:=78}" |
| 40 | + : readonly -i "${EXIT_COULD_NOT_MOVE_DIRECTORY:=79}" |
| 41 | + # ============================================================================== |
| 42 | + |
| 43 | + # ============================================================================== |
| 44 | + # Other errors (80 range) |
| 45 | + # ------------------------------------------------------------------------------ |
| 46 | + : readonly -i "${EXIT_COULD_NOT_FIND:=80}" |
| 47 | + : readonly -i "${EXIT_NOT_CORRECT_TYPE:=81}" |
| 48 | + : readonly -i "${EXIT_NOT_SUPPORTED:=82}" |
| 49 | + : readonly -i "${EXIT_COULD_NOT_CREATE:=83}" |
| 50 | + : readonly -i "${EXIT_COULD_NOT_UPDATE:=84}" |
| 51 | + : readonly -i "${EXIT_COULD_NOT_DELETE:=85}" |
| 52 | + : readonly -i "${EXIT_COULD_NOT_MOVE:=86}" |
| 53 | + # ============================================================================== |
| 54 | + |
| 55 | + # ============================================================================== |
| 56 | + # Credential and validation errors (90 range) |
| 57 | + # ------------------------------------------------------------------------------ |
| 58 | + : readonly -i "${EXIT_VALIDATION_FAILED:=90}" |
| 59 | + : readonly -i "${EXIT_CREDENTIALS_MISSING:=91}" |
| 60 | + : readonly -i "${EXIT_CREDENTIALS_INVALID:=92}" |
| 61 | + # ============================================================================== |
| 62 | + |
| 63 | + # ============================================================================== |
| 64 | + # Application specific errors (93-113 range) |
| 65 | + # These should be reserved for program specific errors |
| 66 | + # : readonly -i "${EXIT_APPLICATION_SPECIFIC_RANGE:=93-113}" |
| 67 | + # ============================================================================== |
| 68 | + |
| 69 | + # ============================================================================== |
| 70 | + # Exit Codes With Special Meanings |
| 71 | + # ------------------------------------------------------------------------------ |
| 72 | + # : readonly -i "${EXIT_GENERAL_ERROR:=1}" # Catchall for general errors Miscellaneous errors, such as 'divide by zero' and other impermissible operations |
| 73 | + # : readonly -i "${EXIT_MISUSE_OF_SHELL_BUILTINS:=2}" # Misuse of shell builtins Missing keyword or command, or permission problem (and diff return code on a failed binary file comparison). |
| 74 | + # ------------------------------------------------------------------------------ |
| 75 | + # : readonly -i "${EXIT_CANNOT_EXECUTE_COMMAND:=126}" # Command invoked cannot execute Permission problem or command is not an executable |
| 76 | + # : readonly -i "${EXIT_COMMAND_NOT_FOUND:=127}" # "command not found" Possible problem with $PATH or a typo |
| 77 | + # : readonly -i "${EXIT_INVALID_EXIT_ARGUMENT:=128}" # Invalid argument to exit exit takes only integer args in the range 0 - 255 (see first footnote) |
| 78 | + # ============================================================================== |
| 79 | + |
| 80 | + # ============================================================================== |
| 81 | + # Reserved codes (range 3-63 and 114-125 and 129-192) |
| 82 | + # ------------------------------------------------------------------------------ |
| 83 | + # : readonly -i "${EXIT_RESERVED_ERROR_RANGE:=3-63}" |
| 84 | + # : readonly -i "${EXIT_RESERVED_ERROR_RANGE:=114-125}" |
| 85 | + # : readonly -i "${EXIT_FATAL_ERROR:=129-192}" |
| 86 | + # ============================================================================== |
| 87 | + |
| 88 | + get_directory_path() { |
| 89 | + (unset CDPATH && cd "${1}" && pwd -P) |
| 90 | + } |
| 91 | + |
| 92 | + get_file_from_string() { |
| 93 | + local sLine sSourceFile |
| 94 | + |
| 95 | + sLine="${*}" |
| 96 | + sSourceFile="${sLine#* }" |
| 97 | + |
| 98 | + # Remove any quotes |
| 99 | + if [[ ${sSourceFile} == *\"* || ${sSourceFile} == *\'* ]]; then |
| 100 | + sSourceFile="${sSourceFile:1:${#sSourceFile}-2}" |
| 101 | + fi |
| 102 | + |
| 103 | + echo "${sSourceFile}" |
| 104 | + } |
| 105 | + |
| 106 | + get_file_path() { |
| 107 | + local sFile sFileName sPath |
| 108 | + |
| 109 | + readonly sFile="${1}" |
| 110 | + |
| 111 | + readonly sFileName=$(basename "${sFile}") |
| 112 | + readonly sPath="$(get_directory_path "$(dirname "${sFile}")")" |
| 113 | + |
| 114 | + echo "${sPath}/${sFileName}" |
| 115 | + } |
| 116 | + |
| 117 | + replace_line_with_file_content() { |
| 118 | + |
| 119 | + local sLine sSourceFile |
| 120 | + |
| 121 | + sLine="${*}" |
| 122 | + sSourceFile="$(get_file_from_string "${sLine}")" |
| 123 | + |
| 124 | + if [[ -f ${sSourceFile} && -f "../${sSourceFile}" ]]; then |
| 125 | + sSourceFile="../${sSourceFile}" |
| 126 | + fi |
| 127 | + |
| 128 | + while read -r; do |
| 129 | + # @NOTE: $REPLY is set by `read` |
| 130 | + if [[ ${REPLY:0:2} != '#!' ]]; then |
| 131 | + # Ignore shebang line |
| 132 | + echo "${REPLY}" |
| 133 | + #@TODO: For now we do not recurse into sourced files |
| 134 | + #elif [[ ${REPLY} =~ ^\s*source ]]; then |
| 135 | + # sLine=$(replace_line_with_file_content "${REPLY}") |
| 136 | + fi |
| 137 | + done <"${sSourceFile}" |
| 138 | + } |
| 139 | + |
| 140 | + # @TODO: Create separate "handle_params" function |
| 141 | + # @TODO: Add usage/help message and params |
| 142 | + |
| 143 | + if [[ $# -lt 1 || $1 == "" ]]; then |
| 144 | + echo 'One parameter expected: <source-file>' >&2 |
| 145 | + exit "${EXIT_NOT_ENOUGH_PARAMETERS}" |
| 146 | + else |
| 147 | + local sSourceDirectory sSourceFilePath sTrimmedLine |
| 148 | + |
| 149 | + readonly sSourceFilePath="$(get_file_path "${1}")" |
| 150 | + readonly sSourceDirectory=$(dirname "${sSourceFilePath}") |
| 151 | + |
| 152 | + pushd "${sSourceDirectory}" >/dev/null || exit "${EXIT_COULD_NOT_FIND_DIRECTORY}" |
| 153 | + |
| 154 | + while read -r; do |
| 155 | + # @NOTE: $REPLY is set by `read` |
| 156 | + sTrimmedLine="$(echo "${REPLY}" | xargs)" |
| 157 | + |
| 158 | + if [[ ${sTrimmedLine:0:7} == 'source ' && ${REPLY} != *\$* ]]; then |
| 159 | + replace_line_with_file_content "${REPLY}" |
| 160 | + else |
| 161 | + echo "${REPLY}" |
| 162 | + fi |
| 163 | + done <"${sSourceFilePath}" |
| 164 | + |
| 165 | + popd >/dev/null 2>&1 || true |
| 166 | + fi |
| 167 | +} |
| 168 | + |
| 169 | +if [[ ${BASH_SOURCE[0]} != "$0" ]]; then |
| 170 | + export -f inline_source |
| 171 | +else |
| 172 | + inline_source "${@-}" |
| 173 | + exit ${?} |
| 174 | +fi |
| 175 | + |
| 176 | +# EOF |
0 commit comments