diff --git a/scripts/.util/git.sh b/scripts/.util/git.sh index ffd2578e..71965bc4 100644 --- a/scripts/.util/git.sh +++ b/scripts/.util/git.sh @@ -7,15 +7,15 @@ set -o pipefail source "$(dirname "${BASH_SOURCE[0]}")/print.sh" function util::git::token::fetch() { - if [[ -z "${GIT_TOKEN:-""}" ]]; then - util::print::title "Fetching GIT_TOKEN" + if [[ -z "${GIT_TOKEN:-""}" ]]; then + util::print::title "Fetching GIT_TOKEN" - GIT_TOKEN="$( - lpass show Shared-CF\ Buildpacks/concourse-private.yml \ - | grep buildpacks-github-token \ - | cut -d ' ' -f 2 - )" - fi + GIT_TOKEN="$( + lpass show Shared-CF\ Buildpacks/concourse-private.yml \ + | grep buildpacks-github-token \ + | cut -d ' ' -f 2 + )" + fi - printf "%s" "${GIT_TOKEN}" + printf "%s" "${GIT_TOKEN}" } diff --git a/scripts/.util/tools.sh b/scripts/.util/tools.sh index b842b110..df51d0d0 100644 --- a/scripts/.util/tools.sh +++ b/scripts/.util/tools.sh @@ -7,13 +7,13 @@ set -o pipefail source "$(dirname "${BASH_SOURCE[0]}")/print.sh" function util::tools::path::export() { - local dir - dir="${1}" + local dir + dir="${1}" - if ! echo "${PATH}" | grep -q "${dir}"; then - PATH="${dir}:$PATH" - export PATH - fi + if ! echo "${PATH}" | grep -q "${dir}"; then + PATH="${dir}:$PATH" + export PATH + fi } function util::tools::jam::install () { @@ -119,6 +119,8 @@ function util::tools::packager::install () { *) util::print::error "unknown argument \"${1}\"" + ;; + esac done @@ -126,7 +128,7 @@ function util::tools::packager::install () { util::tools::path::export "${dir}" if [[ ! -f "${dir}/packager" ]]; then - util::print::title "Installing packager" - GOBIN="${dir}" go get -u github.com/cloudfoundry/libcfbuildpack/packager + util::print::title "Installing packager" + GOBIN="${dir}" go get -u github.com/cloudfoundry/libcfbuildpack/packager fi } diff --git a/scripts/build.sh b/scripts/build.sh index 827c1f68..2ebbfb0f 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,53 +1,88 @@ #!/usr/bin/env bash -set -e -set -u +set -eu set -o pipefail readonly PROGDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" readonly BUILDPACKDIR="$(cd "${PROGDIR}/.." && pwd)" function main() { - mkdir -p "${BUILDPACKDIR}/bin" + while [[ "${#}" != 0 ]]; do + case "${1}" in + --help|-h) + shift 1 + usage + exit 0 + ;; - if [[ -f "${BUILDPACKDIR}/run/main.go" ]]; then - pushd "${BUILDPACKDIR}/bin" > /dev/null || return - printf "%s" "Building run..." + "") + # skip if the argument is empty + shift 1 + ;; - GOOS=linux \ - go build \ - -ldflags="-s -w" \ - -o "run" \ - "${BUILDPACKDIR}/run" + *) + util::print::error "unknown argument \"${1}\"" + esac + done - echo "Success!" + mkdir -p "${BUILDPACKDIR}/bin" - for name in detect build; do - printf "%s" "Linking ${name}..." + run::build + cmd::build +} + +function usage() { + cat <<-USAGE +build.sh [OPTIONS] + +Builds the buildpack executables. + +OPTIONS + --help -h prints the command usage +USAGE +} - ln -sf "run" "${name}" +function run::build() { + if [[ -f "${BUILDPACKDIR}/run/main.go" ]]; then + pushd "${BUILDPACKDIR}/bin" > /dev/null || return + printf "%s" "Building run... " - echo "Success!" - done - popd > /dev/null || return - fi + GOOS=linux \ + go build \ + -ldflags="-s -w" \ + -o "run" \ + "${BUILDPACKDIR}/run" + + echo "Success!" + + for name in detect build; do + printf "%s" "Linking ${name}... " + + ln -sf "run" "${name}" + + echo "Success!" + done + popd > /dev/null || return + fi +} - if [[ -d "${BUILDPACKDIR}/cmd" ]]; then - local name - for src in "${BUILDPACKDIR}"/cmd/*; do - name="$(basename "${src}")" +function cmd::build() { + if [[ -d "${BUILDPACKDIR}/cmd" ]]; then + local name + for src in "${BUILDPACKDIR}"/cmd/*; do + name="$(basename "${src}")" - printf "%s" "Building ${name}..." + printf "%s" "Building ${name}... " - GOOS="linux" \ - go build \ - -ldflags="-s -w" \ - -o "${BUILDPACKDIR}/bin/${name}" \ - "${src}/main.go" + GOOS="linux" \ + go build \ + -ldflags="-s -w" \ + -o "${BUILDPACKDIR}/bin/${name}" \ + "${src}/main.go" - echo "Success!" - done - fi + echo "Success!" + done + fi } main "${@:-}" diff --git a/scripts/integration.sh b/scripts/integration.sh index 5757d853..993e60db 100755 --- a/scripts/integration.sh +++ b/scripts/integration.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash + set -eu set -o pipefail @@ -15,81 +16,110 @@ source "${PROGDIR}/.util/print.sh" source "${PROGDIR}/.util/git.sh" function main() { - if [[ ! -d "${BUILDPACKDIR}/integration" ]]; then - util::print::warn "** WARNING No Integration tests **" - fi - - tools::install - images::pull - token::fetch - tests::run + while [[ "${#}" != 0 ]]; do + case "${1}" in + --help|-h) + shift 1 + usage + exit 0 + ;; + + "") + # skip if the argument is empty + shift 1 + ;; + + *) + util::print::error "unknown argument \"${1}\"" + esac + done + + if [[ ! -d "${BUILDPACKDIR}/integration" ]]; then + util::print::warn "** WARNING No Integration tests **" + fi + + tools::install + images::pull + token::fetch + tests::run } -function tools::install() { - util::tools::pack::install \ - --directory "${BUILDPACKDIR}/.bin" +function usage() { + cat <<-USAGE +integration.sh [OPTIONS] - if [[ -f "${BUILDPACKDIR}/.packit" ]]; then - util::tools::jam::install \ - --directory "${BUILDPACKDIR}/.bin" +Runs the integration test suite. - else - util::tools::packager::install \ - --directory "${BUILDPACKDIR}/.bin" - fi +OPTIONS + --help -h prints the command usage +USAGE } -function images::pull() { - local builder - builder="" +function tools::install() { + util::tools::pack::install \ + --directory "${BUILDPACKDIR}/.bin" - if [[ -f "${BUILDPACKDIR}/integration.json" ]]; then - builder="$(jq -r .builder "${BUILDPACKDIR}/integration.json")" - fi + if [[ -f "${BUILDPACKDIR}/.packit" ]]; then + util::tools::jam::install \ + --directory "${BUILDPACKDIR}/.bin" - if [[ "${builder}" == "null" || -z "${builder}" ]]; then - builder="index.docker.io/paketobuildpacks/builder:base" - fi + else + util::tools::packager::install \ + --directory "${BUILDPACKDIR}/.bin" + fi +} - util::print::title "Pulling builder image..." - docker pull "${builder}" - - util::print::title "Setting default pack builder image..." - pack set-default-builder "${builder}" - - local run_image lifecycle_image - run_image="$( - docker inspect "${builder}" \ - | jq -r '.[0].Config.Labels."io.buildpacks.builder.metadata"' \ - | jq -r '.stack.runImage.image' - )" - lifecycle_image="index.docker.io/buildpacksio/lifecycle:$( - docker inspect "${builder}" \ - | jq -r '.[0].Config.Labels."io.buildpacks.builder.metadata"' \ - | jq -r '.lifecycle.version' - )" - - util::print::title "Pulling run image..." - docker pull "${run_image}" - - util::print::title "Pulling lifecycle image..." - docker pull "${lifecycle_image}" +function images::pull() { + local builder + builder="" + + if [[ -f "${BUILDPACKDIR}/integration.json" ]]; then + builder="$(jq -r .builder "${BUILDPACKDIR}/integration.json")" + fi + + if [[ "${builder}" == "null" || -z "${builder}" ]]; then + builder="index.docker.io/paketobuildpacks/builder:base" + fi + + util::print::title "Pulling builder image..." + docker pull "${builder}" + + util::print::title "Setting default pack builder image..." + pack set-default-builder "${builder}" + + local run_image lifecycle_image + run_image="$( + docker inspect "${builder}" \ + | jq -r '.[0].Config.Labels."io.buildpacks.builder.metadata"' \ + | jq -r '.stack.runImage.image' + )" + lifecycle_image="index.docker.io/buildpacksio/lifecycle:$( + docker inspect "${builder}" \ + | jq -r '.[0].Config.Labels."io.buildpacks.builder.metadata"' \ + | jq -r '.lifecycle.version' + )" + + util::print::title "Pulling run image..." + docker pull "${run_image}" + + util::print::title "Pulling lifecycle image..." + docker pull "${lifecycle_image}" } function token::fetch() { - GIT_TOKEN="$(util::git::token::fetch)" - export GIT_TOKEN + GIT_TOKEN="$(util::git::token::fetch)" + export GIT_TOKEN } function tests::run() { - util::print::title "Run Buildpack Runtime Integration Tests" - pushd "${BUILDPACKDIR}" > /dev/null - if GOMAXPROCS="${GOMAXPROCS:-4}" go test -count=1 -timeout 0 ./integration/... -v -run Integration; then - util::print::success "** GO Test Succeeded **" - else - util::print::error "** GO Test Failed **" - fi - popd > /dev/null + util::print::title "Run Buildpack Runtime Integration Tests" + pushd "${BUILDPACKDIR}" > /dev/null + if GOMAXPROCS="${GOMAXPROCS:-4}" go test -count=1 -timeout 0 ./integration/... -v -run Integration; then + util::print::success "** GO Test Succeeded **" + else + util::print::error "** GO Test Failed **" + fi + popd > /dev/null } main "${@:-}" diff --git a/scripts/package.sh b/scripts/package.sh index b2b2a917..7bffd67d 100755 --- a/scripts/package.sh +++ b/scripts/package.sh @@ -1,7 +1,6 @@ -#!/bin/bash +#!/usr/bin/env bash -set -e -set -u +set -eu set -o pipefail readonly ROOT_DIR="$(cd "$(dirname "${0}")/.." && pwd)" @@ -15,7 +14,7 @@ source "${ROOT_DIR}/scripts/.util/tools.sh" source "${ROOT_DIR}/scripts/.util/print.sh" function main { - local version + local version output while [[ "${#}" != 0 ]]; do case "${1}" in @@ -24,6 +23,17 @@ function main { shift 2 ;; + --output|-o) + output="${2}" + shift 2 + ;; + + --help|-h) + shift 1 + usage + exit 0 + ;; + "") # skip if the argument is empty shift 1 @@ -34,13 +44,32 @@ function main { esac done - if [[ "${version:-}" == "" ]]; then + if [[ -z "${version:-}" ]]; then + usage + echo util::print::error "--version is required" fi + if [[ -z "${output:-}" ]]; then + output="${BUILD_DIR}/buildpackage.cnb" + fi + repo::prepare buildpack::archive "${version}" - buildpackage::create + buildpackage::create "${output}" +} + +function usage() { + cat <<-USAGE +package.sh --version [OPTIONS] + +Packages the buildpack into a buildpackage .cnb file. + +OPTIONS + --help -h prints the command usage + --version -v specifies the version number to use when packaging the buildpack + --output -o location to output the packaged buildpackage artifact (default: ${ROOT_DIR}/build/buildpackage.cnb) +USAGE } function repo::prepare() { @@ -79,12 +108,15 @@ function buildpack::archive() { } function buildpackage::create() { + local output + output="${1}" + util::print::title "Packaging buildpack..." util::tools::pack::install --directory "${BIN_DIR}" pack \ - package-buildpack "${BUILD_DIR}/buildpackage.cnb" \ + package-buildpack "${output}" \ --config "${ROOT_DIR}/package.toml" \ --format file } diff --git a/scripts/unit.sh b/scripts/unit.sh index 8e52ca3d..bed0c27c 100755 --- a/scripts/unit.sh +++ b/scripts/unit.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash + set -eu set -o pipefail @@ -9,15 +10,48 @@ readonly BUILDPACKDIR="$(cd "${PROGDIR}/.." && pwd)" source "${PROGDIR}/.util/print.sh" function main() { - util::print::title "Run Buildpack Unit Tests" - - pushd "${BUILDPACKDIR}" > /dev/null - if go test ./... -v -run Unit; then - util::print::success "** GO Test Succeeded **" - else - util::print::error "** GO Test Failed **" - fi - popd > /dev/null + while [[ "${#}" != 0 ]]; do + case "${1}" in + --help|-h) + shift 1 + usage + exit 0 + ;; + + "") + # skip if the argument is empty + shift 1 + ;; + + *) + util::print::error "unknown argument \"${1}\"" + esac + done + + unit::run +} + +function usage() { + cat <<-USAGE +unit.sh [OPTIONS] + +Runs the unit test suite. + +OPTIONS + --help -h prints the command usage +USAGE +} + +function unit::run() { + util::print::title "Run Buildpack Unit Tests" + + pushd "${BUILDPACKDIR}" > /dev/null + if go test ./... -v -run Unit; then + util::print::success "** GO Test Succeeded **" + else + util::print::error "** GO Test Failed **" + fi + popd > /dev/null } main "${@:-}"