diff --git a/README.md b/README.md index 9dfa063..ec32427 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,17 @@ # go-downloader The inverse of go-releaser. + +reads a gorelease.yaml file and generates a posix shell +script to download it that works on any OS (with exception of windows). + + +Useful in CI/CD systems such as travis-ci.org + +* Much faster then 'go get' +* Make sure your local environment (macOS) and the CI are using the exact same versions. + +## Yes, it's true. + +It's a go program that reads a YAML file that uses a template to make a bash file. + + diff --git a/download-hugo.sh b/download-hugo.sh new file mode 100755 index 0000000..c561619 --- /dev/null +++ b/download-hugo.sh @@ -0,0 +1,112 @@ +#!/bin/sh +set -e + +# Hugo binary downloader. +# +# Downloads the appropriate binary for macOS or linux +# to speed up your CI buids and to make sure your local Hugo +# is the same as the Hugo your CI/CD uses. +# +# Assumes curl is installed and `tar` supports the `-C` flag +# (change directory before executing) +# +# Why: +# +# 1. go get is slow +# +# "go get ...hugo..." can take up to 30 seconds on travis.ci. +# but downloading directly from GitHub releases takes 0.3 seconds. +# +# 2. making sure the local version is the same as travis.ci +# +# Best practice is to make sure your local development version of Hugo +# is the same as what your CI/CD system (e.g. travis, circle-ci, etc) +# is using. This makes sure everyone is using the exact same version. +# +# FYI: URLs on GitHub look like this: +# https://github.com/spf13/hugo/releases/download/v0.20.7/hugo_0.20.7_Linux-64bit.tar.gz +# + +# Set the version on the command line +# as the first arg. +# +# ./download-hugo.sh spf13/hugo 0.20.0 +# +PATH=$1 +VERSION=$2 +OWNER=$(dirname $PATH) +REPO=$(basename $PATH) +if [ -z "${OWNER}" or -z "${VERSION}" + echo "" + echo "Hugo binary downloader" + echo "" + echo "Usage:" + echo " $0: [owner/repo] [version]" + echo " where [owner/repo] is the GitHub.com identifier. + echo " where [version] is from the matching releases page. + echo "" + echo " for example " + echo " $0 spf13/hugo 0.20.7 (see https://github.com/spf13/hugo/releases)" + echo "" + exit 1 +fi + + +# strip leading "v" if any. +# tarball doesn't use "v", but URL does. +VERSION=${VERSION#v} + +BINDIR="./bin" +mkdir -p ${BINDIR} +HUGO=${BINDIR}/hugo + +# default TMPDIR to /tmp +# oddly not set on travis +TMPDIR=${TMPDIR:-/tmp} + +# Do we need to get hugo? +# +# +if [ -f ${HUGO} ]; then + if ${HUGO} version | grep -q ${VERSION}; then + echo "Hugo ${VERSION} installed" + exit 0 + fi +fi + +# What OS? +# +# Translate what the OS claims it is to what +# Hugo GitHub releases uses +# +OS=$(uname -s) +case ${OS} in +Darwin) + OS=macOS + ;; +esac + +# Are we 64-bits or 32-bits? +# +# +CPU=$(uname -m) +case ${CPU} in +x86_64) + CPU=64bit + ;; +i386) + CPU=32bit + ;; +*) echo "unknown CPU: ${HOSTCPU}" + exit 1 +esac + +TARBALL=hugo_${VERSION}_${OS}-${CPU}.tar.gz +REPO=spf13/hugo +URL=https://github.com/${REPO}/releases/download/v${VERSION}/${TARBALL} +echo "Downloading ${TARBALL}" +curl -sSL -o ${TMPDIR}/${TARBALL} ${URL} +tar -C ${TMPDIR} -xzf ${TMPDIR}/${TARBALL} +cp ${TMPDIR}/hugo ${HUGO} +${HUGO} version + diff --git a/main.go b/main.go new file mode 100644 index 0000000..b2c496b --- /dev/null +++ b/main.go @@ -0,0 +1,86 @@ +package main + +import ( + "text/template" + "fmt" + + "github.com/goreleaser/goreleaser/config" +) + +// converts the given name template to it's equivalent in shell +// except for the default goreleaser templates, templates with +// conditionals will return an error +// +// {{ .Binary }} ---> ${BINARY}, etc. +// +func nameTplInShell(target buildTarget) (string, error) { + // TODO: error on conditionals + if target == "{{ .Binary }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" { + prefix = "if [ ! -z \"${ARM}\" ]; then ARM=\"v$ARM\"; fi" + target = "{{ .Binary }}_{{ .Os }}_{{ .Arch }}{{ .Arm }}" + var varmap = map[string]string{ + "Os": "${OS}", + "Arch": "${ARCH}", + "Arm": "${ARM}", + "Version": "${VERSION}", + "Tag": "${TAG}", + "Binary": ${BINARY}", + } + + var out bytes.Buffer + t, err := template.New("name").Parse(target) + if err != nil { + return "", err + } + err = t.Execute(&out, varmap) + return out.String(), err +} + +var tplsrc = `#!/bin/sh +set -e + +BINARY={{ .Binary }} + +VERSION=$1 +if [ -z "${VERSION}" ]; then + echo "" + echo "Usage: $0 [version]" + echo "" + exit 1 +fi + +OS=$(uname -s) +ARCH=$(uname -m) +VERSION=${VERSION#v} +BINDIR=${BINDIR:-./bin} +EXE=${BINDIR}/${BINARY} +TMPDIR=${TMPDIR:-/tmp} + +case ${OS} in + +esac + +case ${ARCH} in + +esac + +if [ ! -d "${BINDIR}" ]; then + mkdir -p ${BINDIR} +fi + +NAME={{ .Name }} + +TARBALL=${NAME}.tar.gz +REPO=spf13/hugo +URL=https://github.com/${REPO}/releases/download/v${VERSION}/${TARBALL} +echo "Downloading ${TARBALL}" +curl -sSL -o ${TMPDIR}/${TARBALL} ${URL} +tar -C ${TMPDIR} -xzf ${TMPDIR}/${TARBALL} +cp ${TMPDIR}/hugo ${HUGO} +` + +func main() { + + + +}