|
| 1 | +# shellcheck shell=bash |
| 2 | +# Version: 0.1.2 |
| 3 | + |
| 4 | +# @name Hookah lib.sh |
| 5 | +# @brief Hookah: An elegantly minimal solution for Git hooks |
| 6 | +# @description Hookah streamlines the process of managing Git hooks. This file is a |
| 7 | +# library of functions that can easily be used by hooks written in Bash. Use it by |
| 8 | +# prepending your hook script with the following |
| 9 | +# |
| 10 | +# ```bash |
| 11 | +# #!/usr/bin/env bash |
| 12 | +# |
| 13 | +# source "${0%/*}/.hookah/lib.sh" |
| 14 | +# hookah.init |
| 15 | +# ``` |
| 16 | +# |
| 17 | +# Learn more about it [on GitHub](https://github.com/hyperupcall/hookah) |
| 18 | + |
| 19 | +if [ -z "$BASH_VERSION" ]; then |
| 20 | + printf '%s\n' "Error: lib.sh: This script is only compatible with Bash. Exiting" >&2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +# @description Initiates the environment, sets up stacktrace printing on the 'ERR' trap, |
| 25 | +# and sets the directory to the root of the Git repository |
| 26 | +# @noargs |
| 27 | +hookah.init() { |
| 28 | + set -Eeo pipefail |
| 29 | + shopt -s dotglob extglob globasciiranges globstar lastpipe shift_verbose |
| 30 | + export LANG='C' LC_CTYPE='C' LC_NUMERIC='C' LC_TIME='C' LC_COLLATE='C' \ |
| 31 | + LC_MONETARY='C' LC_MESSAGES='C' LC_PAPER='C' LC_NAME='C' LC_ADDRESS='C' \ |
| 32 | + LC_TELEPHONE='C' LC_MEASUREMENT='C' LC_IDENTIFICATION='C' LC_ALL='C' |
| 33 | + trap '__hookah_trap_err' 'ERR' |
| 34 | + |
| 35 | + while [ ! -d '.git' ] && [ "$PWD" != / ]; do |
| 36 | + if ! cd ..; then |
| 37 | + __hookah_internal_die "Failed to cd to nearest Git repository" |
| 38 | + fi |
| 39 | + done |
| 40 | + if [ "$PWD" = / ]; then |
| 41 | + __hookah_internal_die "Failed to cd to nearest Git repository" |
| 42 | + fi |
| 43 | + |
| 44 | + # Prevent any possibilities of 'stdin in is not a tty' |
| 45 | + if ! exec </dev/tty; then |
| 46 | + __hookah_internal_warn "Failed to redirect tty to standard input" |
| 47 | + fi |
| 48 | + |
| 49 | + __hookah_internal_info "Running ${BASH_SOURCE[1]##*/}" |
| 50 | +} |
| 51 | + |
| 52 | +# @description Prints a command before running it |
| 53 | +# @arg $@ Command to execute |
| 54 | +hookah.run() { |
| 55 | + __hookah_exec "$*" |
| 56 | + "$@" |
| 57 | +} |
| 58 | + |
| 59 | +# @description Prints a command before running it. But, if the command fails, do not abort execution |
| 60 | +# @arg $@ Command to execute |
| 61 | +hookah.run_allow_fail() { |
| 62 | + if ! hookah.run "$@"; then |
| 63 | + hookah.die 'Command failed' |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# @description Prints `$1` formatted as an error and the stacktrace to standard error, |
| 68 | +# then exits with code 1 |
| 69 | +# @arg $1 string Text to print |
| 70 | +hookah.die() { |
| 71 | + if [ -n "$1" ]; then |
| 72 | + __hookah_internal_error "$1. Exiting" 'Hookah' |
| 73 | + else |
| 74 | + __hookah_internal_error 'Exiting' 'Hookah' |
| 75 | + fi |
| 76 | + |
| 77 | + exit 1 |
| 78 | +} |
| 79 | + |
| 80 | +# @description Prints `$1` formatted as a warning to standard error |
| 81 | +# @arg $1 string Text to print |
| 82 | +hookah.warn() { |
| 83 | + __hookah_internal_warn "$1" 'Hookah' |
| 84 | +} |
| 85 | + |
| 86 | +# @description Prints `$1` formatted as information to standard output |
| 87 | +# @arg $1 string Text to print |
| 88 | +hookah.info() { |
| 89 | + __hookah_internal_info "$1" 'Hookah' |
| 90 | +} |
| 91 | + |
| 92 | +# @description Scans environment variables to determine if script is in a CI environment |
| 93 | +# @exitcode 0 If in CI |
| 94 | +# @exitcode 1 If not in CI |
| 95 | +# @set REPLY Current provider for CI |
| 96 | +hookah.is_ci() { |
| 97 | + unset -v REPLY; REPLY= |
| 98 | + |
| 99 | + # List from 'https://github.com/watson/ci-info/blob/master/vendors.json' |
| 100 | + if [[ -v 'APPVEYOR' ]]; then |
| 101 | + REPLY='AppVeyor' |
| 102 | + elif [[ -v 'SYSTEM_TEAMFOUNDATIONCOLLECTIONURI' ]]; then |
| 103 | + REPLY='Azure Pipelines' |
| 104 | + elif [[ -v 'AC_APPCIRCLE' ]]; then |
| 105 | + REPLY='Appcircle' |
| 106 | + elif [[ -v 'bamboo_planKey' ]]; then |
| 107 | + REPLY='Bamboo' |
| 108 | + elif [[ -v 'BITBUCKET_COMMIT' ]]; then |
| 109 | + REPLY='Bitbucket Pipelines' |
| 110 | + elif [[ -v 'BITRISE_IO' ]]; then |
| 111 | + REPLY='Bitrise' |
| 112 | + elif [[ -v 'BUDDY_WORKSPACE_ID' ]]; then |
| 113 | + REPLY='Buddy' |
| 114 | + elif [[ -v 'BUILDKITE' ]]; then |
| 115 | + REPLY='Buildkite' |
| 116 | + elif [[ -v 'CIRCLECI' ]]; then |
| 117 | + REPLY='CircleCI' |
| 118 | + elif [[ -v 'CIRRUS_CI' ]]; then |
| 119 | + REPLY='Cirrus CI' |
| 120 | + elif [[ -v 'CODEBUILD_BUILD_ARN' ]]; then |
| 121 | + REPLY='AWS CodeBuild' |
| 122 | + elif [[ -v 'CF_BUILD_ID' ]]; then |
| 123 | + REPLY='Codefresh' |
| 124 | + elif [[ -v '[object Object]' ]]; then |
| 125 | + REPLY='Codeship' |
| 126 | + elif [[ -v 'DRONE' ]]; then |
| 127 | + REPLY='Drone' |
| 128 | + elif [[ -v 'DSARI' ]]; then |
| 129 | + REPLY='dsari' |
| 130 | + elif [[ -v 'EAS_BUILD' ]]; then |
| 131 | + REPLY='Expo Application Services' |
| 132 | + elif [[ -v 'GITHUB_ACTIONS' ]]; then |
| 133 | + REPLY='GitHub Actions' |
| 134 | + elif [[ -v 'GITLAB_CI' ]]; then |
| 135 | + REPLY='GitLab CI' |
| 136 | + elif [[ -v 'GO_PIPELINE_LABEL' ]]; then |
| 137 | + REPLY='GoCD' |
| 138 | + elif [[ -v 'LAYERCI' ]]; then |
| 139 | + REPLY='LayerCI' |
| 140 | + elif [[ -v 'HUDSON_URL' ]]; then |
| 141 | + REPLY='Hudson' |
| 142 | + elif [[ -v 'JENKINS_URL,BUILD_ID' ]]; then |
| 143 | + REPLY='Jenkins' |
| 144 | + elif [[ -v 'MAGNUM' ]]; then |
| 145 | + REPLY='Magnum CI' |
| 146 | + elif [[ -v 'NETLIFY' ]]; then |
| 147 | + REPLY='Netlify CI' |
| 148 | + elif [[ -v 'NEVERCODE' ]]; then |
| 149 | + REPLY='Nevercode' |
| 150 | + elif [[ -v 'RENDER' ]]; then |
| 151 | + REPLY='Render' |
| 152 | + elif [[ -v 'SAILCI' ]]; then |
| 153 | + REPLY='Sail CI' |
| 154 | + elif [[ -v 'SEMAPHORE' ]]; then |
| 155 | + REPLY='Semaphore' |
| 156 | + elif [[ -v 'SCREWDRIVER' ]]; then |
| 157 | + REPLY='Screwdriver' |
| 158 | + elif [[ -v 'SHIPPABLE' ]]; then |
| 159 | + REPLY='Shippable' |
| 160 | + elif [[ -v 'TDDIUM' ]]; then |
| 161 | + REPLY='Solano CI' |
| 162 | + elif [[ -v 'STRIDER' ]]; then |
| 163 | + REPLY='Strider CD' |
| 164 | + elif [[ -v 'TASK_ID,RUN_ID' ]]; then |
| 165 | + REPLY='TaskCluster' |
| 166 | + elif [[ -v 'TEAMCITY_VERSION' ]]; then |
| 167 | + REPLY='TeamCity' |
| 168 | + elif [[ -v 'TRAVIS' ]]; then |
| 169 | + REPLY='Travis CI' |
| 170 | + elif [[ -v 'NOW_BUILDER' ]]; then |
| 171 | + REPLY='Vercel' |
| 172 | + elif [[ -v 'APPCENTER_BUILD_ID' ]]; then |
| 173 | + REPLY='Visual Studio App Center' |
| 174 | + else |
| 175 | + return 1 |
| 176 | + fi |
| 177 | +} |
| 178 | + |
| 179 | +# @description Test whether color should be outputed |
| 180 | +# @exitcode 0 if should print color |
| 181 | +# @exitcode 1 if should not print color |
| 182 | +# @internal |
| 183 | +__hookah_is_color() { |
| 184 | + if [[ -v NO_COLOR || $TERM == dumb ]]; then |
| 185 | + return 1 |
| 186 | + else |
| 187 | + return 0 |
| 188 | + fi |
| 189 | +} |
| 190 | + |
| 191 | +# @internal |
| 192 | +__hookah_exec() { |
| 193 | + if __hookah_is_color; then |
| 194 | + printf "\033[1mHookah \033[1m[exec]:\033[0m %s\n" "$*" |
| 195 | + else |
| 196 | + printf "Hookah [exec]: %s\n" "$*" |
| 197 | + fi |
| 198 | +} |
| 199 | + |
| 200 | +# @internal |
| 201 | +__hookah_internal_die() { |
| 202 | + __hookah_internal_error "$1" |
| 203 | + exit 1 |
| 204 | +} |
| 205 | + |
| 206 | +# @internal |
| 207 | +__hookah_internal_error() { |
| 208 | + local str="${2:-"Hookah (internal)"}" |
| 209 | + |
| 210 | + if __hookah_is_color; then |
| 211 | + printf "\033[1;31m\033[1m$str \033[1m[error]:\033[0m %s\n" "$1" |
| 212 | + else |
| 213 | + printf "$str [error]: %s\n" "$1" |
| 214 | + fi |
| 215 | +} >&2 |
| 216 | + |
| 217 | +# @internal |
| 218 | +__hookah_internal_warn() { |
| 219 | + local str="${2:-"Hookah (internal)"}" |
| 220 | + |
| 221 | + if __hookah_is_color; then |
| 222 | + printf "\033[1;33m\033[1m$str \033[1m[warn]:\033[0m %s\n" "$1" |
| 223 | + else |
| 224 | + printf "$str [warn]: %s\n" "$1" |
| 225 | + fi |
| 226 | +} >&2 |
| 227 | + |
| 228 | +# @internal |
| 229 | +__hookah_internal_info() { |
| 230 | + local str="${2:-"Hookah (internal)"}" |
| 231 | + |
| 232 | + if __hookah_is_color; then |
| 233 | + printf "\033[0;36m\033[1m$str \033[1m[info]:\033[0m %s\n" "$1" |
| 234 | + else |
| 235 | + printf "$str [info]: %s\n" "$1" |
| 236 | + fi |
| 237 | +} |
| 238 | + |
| 239 | +# @internal |
| 240 | +__hookah_trap_err() { |
| 241 | + local error_code=$? |
| 242 | + |
| 243 | + __hookah_internal_error "Your hook did not exit successfully (exit code $error_code)" |
| 244 | + |
| 245 | + exit $error_code |
| 246 | +} |
0 commit comments