github.com/pkumar631/talisman@v0.3.2/install.sh (about) 1 #!/bin/bash 2 set -euo pipefail 3 4 # we call run() at the end of the script to prevent inconsistent state in case 5 # user runs with curl|bash and curl fails in the middle of the download 6 # (https://www.seancassidy.me/dont-pipe-to-your-shell.html) 7 run() { 8 IFS=$'\n' 9 10 VERSION="v0.3.1" 11 GITHUB_URL="https://github.com/thoughtworks/talisman" 12 BINARY_BASE_URL="$GITHUB_URL/releases/download/$VERSION/talisman" 13 REPO_PRE_PUSH_HOOK=".git/hooks/pre-push" 14 DEFAULT_GLOBAL_TEMPLATE_DIR="$HOME/.git-templates" 15 16 EXPECTED_BINARY_SHA_LINUX_AMD64="6b36b74057f3878d564b944dcd42fa5c0c9bace5de80c7273850b6eb990599de" 17 EXPECTED_BINARY_SHA_LINUX_X86="891413d81f39a20b2ea64c23444351546ae97c168f182375dece4eec024c265c" 18 EXPECTED_BINARY_SHA_DARWIN_AMD64="315720cbda5fc11712fe3269778b6a57c796ffc5a6596990f208635c8b53edf0" 19 20 declare DOWNLOADED_BINARY 21 22 E_HOOK_ALREADY_PRESENT=1 23 E_CHECKSUM_MISMATCH=2 24 E_USER_CANCEL=3 25 E_HEADLESS=4 26 E_UNSUPPORTED_ARCH=5 27 E_DEPENDENCY_NOT_FOUND=6 28 29 echo_error() { 30 echo -ne $(tput setaf 1) >&2 31 echo "$1" >&2 32 echo -ne $(tput sgr0) >&2 33 } 34 35 binary_arch_suffix() { 36 declare ARCHITECTURE 37 if [[ "$(uname -s)" == "Linux" ]]; then 38 ARCHITECTURE="linux" 39 elif [[ "$(uname -s)" == "Darwin" ]]; then 40 ARCHITECTURE="darwin" 41 else 42 echo_error "Talisman currently only supports Linux and Darwin systems." 43 echo_error "If this is a problem for you, please open an issue: https://github.com/thoughtworks/talisman/issues/new" 44 exit $E_UNSUPPORTED_ARCH 45 fi 46 47 if [[ "$(uname -m)" = "x86_64" ]]; then 48 ARCHITECTURE="${ARCHITECTURE}_amd64" 49 elif [[ "$(uname -m)" =~ '^i.?86$' ]]; then 50 ARCHITECTURE="${ARCHITECTURE}_386" 51 else 52 echo_error "Talisman currently only supports x86 and x86_64 architectures." 53 echo_error "If this is a problem for you, please open an issue: https://github.com/thoughtworks/talisman/issues/new" 54 exit $E_UNSUPPORTED_ARCH 55 fi 56 57 echo $ARCHITECTURE 58 } 59 60 61 download_and_verify() { 62 if [[ ! -x "$(which curl 2>/dev/null)" ]]; then 63 echo_error "This script requires 'curl' to download the Talisman binary." 64 exit $E_DEPENDENCY_NOT_FOUND 65 fi 66 if [[ ! -x "$(which shasum 2>/dev/null)" ]]; then 67 echo_error "This script requires 'shasum' to verify the Talisman binary." 68 exit $E_DEPENDENCY_NOT_FOUND 69 fi 70 71 echo 'Downloading and verifying binary...' 72 echo 73 74 TMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t 'talisman') 75 trap 'rm -r $TMP_DIR' EXIT 76 chmod 0700 $TMP_DIR 77 78 ARCH_SUFFIX=$(binary_arch_suffix) 79 80 curl --location --silent "${BINARY_BASE_URL}_${ARCH_SUFFIX}" > $TMP_DIR/talisman 81 82 DOWNLOAD_SHA=$(shasum -b -a256 $TMP_DIR/talisman | cut -d' ' -f1) 83 84 declare EXPECTED_BINARY_SHA 85 case "$ARCH_SUFFIX" in 86 linux_386) 87 EXPECTED_BINARY_SHA="$EXPECTED_BINARY_SHA_LINUX_X86" 88 ;; 89 linux_amd64) 90 EXPECTED_BINARY_SHA="$EXPECTED_BINARY_SHA_LINUX_AMD64" 91 ;; 92 darwin_amd64) 93 EXPECTED_BINARY_SHA="$EXPECTED_BINARY_SHA_DARWIN_AMD64" 94 ;; 95 esac 96 97 if [[ ! "$DOWNLOAD_SHA" == "$EXPECTED_BINARY_SHA" ]]; then 98 echo_error "Uh oh... SHA256 checksum did not verify. Binary download must have been corrupted in some way." 99 echo_error "Expected SHA: $EXPECTED_BINARY_SHA" 100 echo_error "Download SHA: $DOWNLOAD_SHA" 101 exit $E_CHECKSUM_MISMATCH 102 fi 103 104 DOWNLOADED_BINARY="$TMP_DIR/talisman" 105 } 106 107 install_to_repo() { 108 if [[ -x "$REPO_PRE_PUSH_HOOK" ]]; then 109 echo_error "Oops, it looks like you already have a pre-push hook installed at '$REPO_PRE_PUSH_HOOK'." 110 echo_error "Talisman is not compatible with other hooks right now, sorry." 111 echo_error "If this is a problem for you, please open an issue: https://github.com/thoughtworks/talisman/issues/new" 112 exit $E_HOOK_ALREADY_PRESENT 113 fi 114 115 download_and_verify 116 117 mkdir -p $(dirname $REPO_PRE_PUSH_HOOK) 118 cp $DOWNLOADED_BINARY $REPO_PRE_PUSH_HOOK 119 chmod +x $REPO_PRE_PUSH_HOOK 120 121 echo -ne $(tput setaf 2) 122 echo "Talisman successfully installed to '$REPO_PRE_PUSH_HOOK'." 123 echo -ne $(tput sgr0) 124 } 125 126 install_to_git_templates() { 127 if [[ ! -t 1 ]]; then 128 echo_error "Headless install to system templates is not supported." 129 echo_error "If you would like this feature, please open an issue: https://github.com/thoughtworks/talisman/issues/new" 130 exit $E_HEADLESS 131 fi 132 133 TEMPLATE_DIR=$(git config --global init.templatedir) || true 134 135 echo "Not running from inside a git repository... installing as a" 136 echo "git template." 137 echo 138 echo "If you meant to install to a specific repo, 'cd' into that" 139 echo "repo and run this script again." 140 echo 141 echo "Installing as a template will automatically add Talisman to" 142 echo "any new repo that you 'init' or 'clone'." 143 echo 144 145 if [[ "$TEMPLATE_DIR" == "" ]]; then 146 echo "No git template directory is configured. Let's add one." 147 echo "(this will override any system git templates and modify your git config file)" 148 echo 149 read -u1 -p "Git template directory: ($DEFAULT_GLOBAL_TEMPLATE_DIR) " TEMPLATE_DIR 150 echo 151 TEMPLATE_DIR=${TEMPLATE_DIR:-$DEFAULT_GLOBAL_TEMPLATE_DIR} 152 git config --global init.templatedir $TEMPLATE_DIR 153 else 154 echo "You already have a git template directory configured." 155 echo 156 read -u1 -p "Add Talisman to '$TEMPLATE_DIR/hooks?' (Y/n) " USE_EXISTING 157 echo 158 159 case "$USE_EXISTING" in 160 Y|y|"") ;; # okay, continue 161 *) 162 echo_error "Not installing Talisman." 163 echo_error "If you were trying to install into a single git repo, re-run this command from that repo." 164 echo_error "You can always download/compile manually from our Github page: $GITHUB_URL" 165 exit $E_USER_CANCEL 166 ;; 167 esac 168 fi 169 170 # Support '~' in path 171 TEMPLATE_DIR=${TEMPLATE_DIR/#\~/$HOME} 172 173 if [ -f "$TEMPLATE_DIR/hooks/pre-push" ]; then 174 echo_error "Oops, it looks like you already have a pre-push hook installed at '$TEMPLATE_DIR/hooks/pre-push'." 175 echo_error "Talisman is not compatible with other hooks right now, sorry." 176 echo_error "If this is a problem for you, please open an issue: https://github.com/thoughtworks/talisman/issues/new" 177 exit $E_HOOK_ALREADY_PRESENT 178 fi 179 180 mkdir -p "$TEMPLATE_DIR/hooks" 181 182 download_and_verify 183 184 cp $DOWNLOADED_BINARY "$TEMPLATE_DIR/hooks/pre-push" 185 chmod +x "$TEMPLATE_DIR/hooks/pre-push" 186 187 echo -ne $(tput setaf 2) 188 echo "Talisman successfully installed." 189 echo -ne $(tput sgr0) 190 } 191 192 if [ ! -d "./.git" ]; then 193 install_to_git_templates 194 else 195 install_to_repo 196 fi 197 } 198 199 run $0 $@