|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Shamelessly copied from https://github.com/technosophos/helm-template |
| 4 | + |
| 5 | +PROJECT_NAME="helm-diff" |
| 6 | +PROJECT_GH="databus23/$PROJECT_NAME" |
| 7 | + |
| 8 | +: ${HELM_PLUGIN_PATH:="$(helm home)/plugins/helm-diff"} |
| 9 | + |
| 10 | +if [[ $SKIP_BIN_INSTALL == "1" ]]; then |
| 11 | + echo "Skipping binary install" |
| 12 | + exit |
| 13 | +fi |
| 14 | + |
| 15 | +# initArch discovers the architecture for this system. |
| 16 | +initArch() { |
| 17 | + ARCH=$(uname -m) |
| 18 | + case $ARCH in |
| 19 | + armv5*) ARCH="armv5";; |
| 20 | + armv6*) ARCH="armv6";; |
| 21 | + armv7*) ARCH="armv7";; |
| 22 | + aarch64) ARCH="arm64";; |
| 23 | + x86) ARCH="386";; |
| 24 | + x86_64) ARCH="amd64";; |
| 25 | + i686) ARCH="386";; |
| 26 | + i386) ARCH="386";; |
| 27 | + esac |
| 28 | +} |
| 29 | + |
| 30 | +# initOS discovers the operating system for this system. |
| 31 | +initOS() { |
| 32 | + OS=$(echo `uname`|tr '[:upper:]' '[:lower:]') |
| 33 | + |
| 34 | + case "$OS" in |
| 35 | + # Minimalist GNU for Windows |
| 36 | + mingw*) OS='windows';; |
| 37 | + darwin) OS='macos';; |
| 38 | + esac |
| 39 | +} |
| 40 | + |
| 41 | +# verifySupported checks that the os/arch combination is supported for |
| 42 | +# binary builds. |
| 43 | +verifySupported() { |
| 44 | + local supported="linux-amd64\nmacos-amd64" |
| 45 | + if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then |
| 46 | + echo "No prebuild binary for ${OS}-${ARCH}." |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + |
| 50 | + if ! type "curl" > /dev/null && ! type "wget" > /dev/null; then |
| 51 | + echo "Either curl or wget is required" |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +# getDownloadURL checks the latest available version. |
| 57 | +getDownloadURL() { |
| 58 | + # Use the GitHub API to find the latest version for this project. |
| 59 | + local latest_url="https://api.github.com/repos/$PROJECT_GH/releases/latest" |
| 60 | + if type "curl" > /dev/null; then |
| 61 | + DOWNLOAD_URL=$(curl -s $latest_url | grep $OS | awk '/\"browser_download_url\":/{gsub( /[,\"]/,"", $2); print $2}') |
| 62 | + elif type "wget" > /dev/null; then |
| 63 | + DOWNLOAD_URL=$(wget -q -O - $latest_url | awk '/\"browser_download_url\":/{gsub( /[,\"]/,"", $2); print $2}') |
| 64 | + fi |
| 65 | +} |
| 66 | + |
| 67 | +# downloadFile downloads the latest binary package and also the checksum |
| 68 | +# for that binary. |
| 69 | +downloadFile() { |
| 70 | + PLUGIN_TMP_FILE="/tmp/${PROJECT_NAME}.tgz" |
| 71 | + echo "Downloading $DOWNLOAD_URL" |
| 72 | + if type "curl" > /dev/null; then |
| 73 | + curl -L "$DOWNLOAD_URL" -o "$PLUGIN_TMP_FILE" |
| 74 | + elif type "wget" > /dev/null; then |
| 75 | + wget -q -O "$PLUGIN_TMP_FILE" "$DOWNLOAD_URL" |
| 76 | + fi |
| 77 | +} |
| 78 | + |
| 79 | +# installFile verifies the SHA256 for the file, then unpacks and |
| 80 | +# installs it. |
| 81 | +installFile() { |
| 82 | + HELM_TMP="/tmp/$PROJECT_NAME" |
| 83 | + mkdir -p "$HELM_TMP" |
| 84 | + tar xf "$PLUGIN_TMP_FILE" -C "$HELM_TMP" |
| 85 | + HELM_TMP_BIN="$HELM_TMP/diff" |
| 86 | + echo "Preparing to install into ${HELM_PLUGIN_PATH}" |
| 87 | + cp "$HELM_TMP_BIN" "$HELM_PLUGIN_PATH" |
| 88 | +} |
| 89 | + |
| 90 | +# fail_trap is executed if an error occurs. |
| 91 | +fail_trap() { |
| 92 | + result=$? |
| 93 | + if [ "$result" != "0" ]; then |
| 94 | + echo "Failed to install $PROJECT_NAME" |
| 95 | + echo "\tFor support, go to https://github.com/databus23/helm-diff." |
| 96 | + fi |
| 97 | + exit $result |
| 98 | +} |
| 99 | + |
| 100 | +# testVersion tests the installed client to make sure it is working. |
| 101 | +testVersion() { |
| 102 | + set +e |
| 103 | + echo "$PROJECT_NAME installed into $HELM_PLUGIN_PATH/$PROJECT_NAME" |
| 104 | + $HELM_PLUGIN_PATH/diff -h |
| 105 | + set -e |
| 106 | +} |
| 107 | + |
| 108 | +# Execution |
| 109 | + |
| 110 | +#Stop execution on any error |
| 111 | +trap "fail_trap" EXIT |
| 112 | +set -e |
| 113 | +initArch |
| 114 | +initOS |
| 115 | +verifySupported |
| 116 | +getDownloadURL |
| 117 | +downloadFile |
| 118 | +installFile |
| 119 | +testVersion |
0 commit comments