Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Commit

Permalink
initial broken release
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed May 10, 2017
1 parent 8c0915e commit e2bee07
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
15 changes: 15 additions & 0 deletions 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.


112 changes: 112 additions & 0 deletions 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

86 changes: 86 additions & 0 deletions 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() {



}

0 comments on commit e2bee07

Please sign in to comment.