Skip to content

Commit

Permalink
Fix plugin install script & update goreleaser config
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilsbhat committed Sep 2, 2021
1 parent 5c94b31 commit bc91ddc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 118 deletions.
11 changes: 10 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,27 @@ builds:
- -s -w -X github.com/nikhilsbhat/helm-images/version.Version={{.Version}} -X github.com/nikhilsbhat/helm-images/version.Env={{.Env.BUILD_ENVIRONMENT}} -X github.com/nikhilsbhat/helm-images/version.BuildDate={{.Date}} -X github.com/nikhilsbhat/helm-images/version.Revision={{.Commit}} -X github.com/nikhilsbhat/helm-images/version.GoVersion={{.Env.GOVERSION}} -X github.com/nikhilsbhat/helm-images/version.Platform={{ .Os }}/{{ .Arch }}

archives:
- replacements:
- format: tar.gz
replacements:
darwin: Darwin
linux: Linux
386: i386
amd64: x86_64
wrap_in_directory: true
files:
- plugin.yaml
- LICENSE

checksum:
name_template: 'checksums.txt'

snapshot:
name_template: "{{ .Tag }}"

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ local.run: local.build ## Generates the artifact and start the service in the cu
./${APP_NAME}

publish: local.check ## Builds and publishes the app
GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} goreleaser release --rm-dist
GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} PLUGIN_PATH=${APP_DIR} goreleaser release --rm-dist

mock.publish: local.check ## Builds and publishes the app
GOVERSION=${GOVERSION} BUILD_ENVIRONMENT=${BUILD_ENVIRONMENT} PLUGIN_PATH=${APP_DIR} goreleaser release --skip-publish --rm-dist

lint: ## Lint's application for errors, it is a linters aggregator (https://github.com/golangci/golangci-lint).
if [ -z "${DEV}" ]; then golangci-lint run --color always ; else docker run --rm -v $(APP_DIR):/app -w /app golangci/golangci-lint:v1.31-alpine golangci-lint run --color always ; fi
Expand Down
146 changes: 30 additions & 116 deletions install-binary.sh
Original file line number Diff line number Diff line change
@@ -1,139 +1,53 @@
#!/usr/bin/env sh
#! /bin/bash -e

# Shamelessly copied from https://github.com/databus23/helm-diff
function download() {
osName=$(uname -s)
DOWNLOAD_URL=$(curl --silent "https://api.github.com/repos/nikhilsbhat/helm-images/releases/latest" | grep -o "browser_download_url.*\_${osName}_x86_64.tar.gz")

PROJECT_NAME="helm-images"
PROJECT_GH="nikhilsbhat/$PROJECT_NAME"
export GREP_COLOR="never"
DOWNLOAD_URL=${DOWNLOAD_URL//\"/}
DOWNLOAD_URL=${DOWNLOAD_URL/browser_download_url: /}

HELM_MAJOR_VERSION=$("${HELM_BIN}" version --client --short | awk -F '.' '{print $1}')
echo "download url: $DOWNLOAD_URL"
OUTPUT_BASENAME=helm-images
OUTPUT_BASENAME_WITH_POSTFIX=$HELM_PLUGIN_DIR/$OUTPUT_BASENAME.tar.gz

: ${HELM_PLUGIN_DIR:="$("${HELM_BIN}" home --debug=false)/plugins/helm-images"}

# Convert the HELM_PLUGIN_DIR to unix if cygpath is
# available. This is the case when using MSYS2 or Cygwin
# on Windows where helm returns a Windows path but we
# need a Unix path

if type cygpath >/dev/null 2>&1; then
HELM_PLUGIN_DIR=$(cygpath -u $HELM_PLUGIN_DIR)
fi

if [ "$SKIP_BIN_INSTALL" = "1" ]; then
echo "Skipping binary install"
exit
fi

# initArch discovers the architecture for this system.
initArch() {
ARCH=$(uname -m)
case $ARCH in
armv5*) ARCH="armv5" ;;
armv6*) ARCH="armv6" ;;
armv7*) ARCH="armv7" ;;
aarch64) ARCH="arm64" ;;
x86) ARCH="386" ;;
x86_64) ARCH="amd64" ;;
i686) ARCH="386" ;;
i386) ARCH="386" ;;
esac
}

# initOS discovers the operating system for this system.
initOS() {
OS=$(uname | tr '[:upper:]' '[:lower:]')

case "$OS" in
# Msys support
msys*) OS='windows' ;;
# Minimalist GNU for Windows
mingw*) OS='windows' ;;
darwin) OS='macos' ;;
esac
}

# verifySupported checks that the os/arch combination is supported for
# binary builds.
verifySupported() {
supported="linux-amd64\nfreebsd-amd64\nmacos-amd64\nwindows-amd64"
if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then
echo "No prebuild binary for ${OS}-${ARCH}."
exit 1
fi

if ! type "curl" >/dev/null && ! type "wget" >/dev/null; then
echo "Either curl or wget is required"
if [ -z "$DOWNLOAD_URL" ]; then
echo "Unsupported OS / architecture: ${osName}"
exit 1
fi
}

# getDownloadURL checks the latest available version.
getDownloadURL() {
version=$(git -C "$HELM_PLUGIN_DIR" describe --tags --exact-match 2>/dev/null || :)
if [ -n "$version" ]; then
DOWNLOAD_URL="https://github.com/$PROJECT_GH/releases/download/$version/helm-images-$OS.tgz"
# echo "$DOWNLOAD_URL"
if [[ -n $(command -v curl) ]]; then
curl -L $DOWNLOAD_URL -o $OUTPUT_BASENAME_WITH_POSTFIX
else
# Use the GitHub API to find the download url for this project.
url="https://api.github.com/repos/$PROJECT_GH/releases/latest"
if type "curl" >/dev/null; then
DOWNLOAD_URL=$(curl -s $url | grep $OS | awk '/\"browser_download_url\":/{gsub( /[,\"]/,"", $2); print $2}')
elif type "wget" >/dev/null; then
DOWNLOAD_URL=$(wget -q -O - $url | grep $OS | awk '/\"browser_download_url\":/{gsub( /[,\"]/,"", $2); print $2}')
fi
echo "Need curl"
exit -1
fi
}

# downloadFile downloads the latest binary package and also the checksum
# for that binary.
downloadFile() {
PLUGIN_TMP_FILE="/tmp/${PROJECT_NAME}.tgz"
echo "Downloading $DOWNLOAD_URL"
if type "curl" >/dev/null; then
curl -L "$DOWNLOAD_URL" -o "$PLUGIN_TMP_FILE"
elif type "wget" >/dev/null; then
wget -q -O "$PLUGIN_TMP_FILE" "$DOWNLOAD_URL"
fi
echo $OUTPUT_BASENAME_WITH_POSTFIX
}

# installFile verifies the SHA256 for the file, then unpacks and
# installs it.
installFile() {
function install_plugin() {
HELM_TMP="/tmp/$PROJECT_NAME"
mkdir -p "$HELM_TMP"
tar xf "$PLUGIN_TMP_FILE" -C "$HELM_TMP"
HELM_TMP_BIN="$HELM_TMP/images/bin/images"
HELM_TMP_BIN="$HELM_TMP/diff/bin/diff"
echo "Preparing to install into ${HELM_PLUGIN_DIR}"
mkdir -p "$HELM_PLUGIN_DIR/bin"
cp "$HELM_TMP_BIN" "$HELM_PLUGIN_DIR/bin"
}

# fail_trap is executed if an error occurs.
fail_trap() {
result=$?
if [ "$result" != "0" ]; then
echo "Failed to install $PROJECT_NAME"
printf '\tFor support, go to https://github.com/nikhilsbhat/helm-images.\n'
fi
exit $result
}

# testVersion tests the installed client to make sure it is working.
testVersion() {
set +e
echo "$PROJECT_NAME installed into $HELM_PLUGIN_DIR/$PROJECT_NAME"
"${HELM_PLUGIN_DIR}/bin/images" -h
set -e
}
function install() {
echo "Installing helm-images..."

# Execution
local artifact_path=$(download)
echo "$artifact_path"
# rm -rf bin && mkdir bin && tar -xvf $OUTPUT_BASENAME_WITH_POSTFIX -C bin >/dev/null && rm -f $OUTPUT_BASENAME_WITH_POSTFIX

echo "helm-images is installed."
echo
echo "See https://github.com/nikhilsbhat/helm-images for help getting started."
}

#Stop execution on any error
trap "fail_trap" EXIT
set -e
initArch
initOS
verifySupported
getDownloadURL
downloadFile
installFile
testVersion
install "$@"

0 comments on commit bc91ddc

Please sign in to comment.