Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update bootstrap #177

Merged
merged 8 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions internal/build-bootstrapper/assets/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ GITHUB_DOWNLOAD=https://github.com/WillAbides/bindown/releases/download

usage() {
this=$1
cat <<EOT
cat << EOT
Usage: $this [-b bindir] [-d]

Usage: $this [-b] bindir [-d]
-b sets bindir or installation directory, Defaults to ./bin
-d turns on debug logging

Expand All @@ -30,6 +28,14 @@ parse_args() {
shift $((OPTIND - 1))
}

bindown_name() {
if [ "$OS" = "windows" ]; then
echo bindown.exe
else
echo bindown
fi
}

execute() {
tmpdir=$(mktemp -d)
echo "$CHECKSUMS" > "${tmpdir}/checksums.txt"
Expand All @@ -39,15 +45,17 @@ execute() {
srcdir="${tmpdir}"
(cd "${tmpdir}" && untar "${TARBALL}")
test ! -d "${BINDIR}" && install -d "${BINDIR}"
binexe="bindown"
if [ "$OS" = "windows" ]; then
binexe="${binexe}.exe"
fi
install "${srcdir}/${binexe}" "${BINDIR}/"
log_info "installed ${BINDIR}/${binexe}"
install "${srcdir}/$(bindown_name)" "${BINDIR}/"
log_info "installed ${BINDIR}/$(bindown_name)"
rm -rf "${tmpdir}"
}

already_installed() {
VERSION="$1"
[ -f "${BINDIR}/$(bindown_name)" ] &&
"${BINDIR}/$(bindown_name)" version 2> /dev/null | grep -q "$VERSION"
}

OS=$(uname_os)
ARCH=$(uname_arch)

Expand All @@ -57,6 +65,12 @@ uname_arch_check "$ARCH"
parse_args "$@"

VERSION=${TAG#v}

if already_installed "$VERSION"; then
log_debug "bindown ${VERSION} is already installed"
exit 0
fi

NAME=bindown_${VERSION}_${OS}_${ARCH}
TARBALL=${NAME}.${FORMAT}
TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
Expand Down
10 changes: 5 additions & 5 deletions internal/build-bootstrapper/assets/shlib.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cat /dev/null <<EOF
cat /dev/null << EOF
------------------------------------------------------------------------
https://github.com/client9/shlib - portable posix shell functions
Public domain - http://unlicense.org
Expand All @@ -7,7 +7,7 @@ but credit (and pull requests) appreciated.
------------------------------------------------------------------------
EOF
is_command() {
command -v "$1" >/dev/null
command -v "$1" > /dev/null
}
echoerr() {
echo "$@" 1>&2
Expand Down Expand Up @@ -175,7 +175,7 @@ hash_sha256() {
hash=$(sha256sum "$TARGET") || return 1
echo "$hash" | cut -d ' ' -f 1
elif is_command shasum; then
hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
hash=$(shasum -a 256 "$TARGET" 2> /dev/null) || return 1
echo "$hash" | cut -d ' ' -f 1
elif is_command openssl; then
hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
Expand All @@ -193,7 +193,7 @@ hash_sha256_verify() {
return 1
fi
BASENAME=${TARGET##*/}
want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
want=$(grep "${BASENAME}" "${checksums}" 2> /dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
if [ -z "$want" ]; then
log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
return 1
Expand All @@ -204,7 +204,7 @@ hash_sha256_verify() {
return 1
fi
}
cat /dev/null <<EOF
cat /dev/null << EOF
------------------------------------------------------------------------
End of functions from https://github.com/client9/shlib
------------------------------------------------------------------------
Expand Down
67 changes: 15 additions & 52 deletions internal/build-bootstrapper/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"embed"
"flag"
"fmt"
"os"
"io"
"net/http"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -15,69 +16,31 @@ import (
//go:embed assets/*
var assets embed.FS

func execBindown(repoRoot string, arg ...string) error {
bootstrapCmd := exec.Command("script/bootstrap-bindown.sh", "-b", "bin/bootstrapped")
bootstrapCmd.Dir = repoRoot
err := bootstrapCmd.Run()
if err != nil {
return err
}
bindownCmd := exec.Command("bin/bootstrapped/bindown", arg...)
bindownCmd.Dir = repoRoot
return bindownCmd.Run()
}

func build(tag, repoRoot string) (_ string, errOut error) {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return "", err
}
defer func() {
rmErr := os.RemoveAll(tmpDir)
if errOut == nil {
errOut = rmErr
}
}()
err = execBindown(repoRoot, "install", "shfmt")
if err != nil {
return "", err
}
err = execBindown(repoRoot, "install", "shellcheck")
bindownCmd := exec.Command("script/bindown", "install", "shfmt", "shellcheck")
bindownCmd.Dir = repoRoot
err := bindownCmd.Run()
if err != nil {
return "", err
}
err = execBindown(
repoRoot,
"dependency",
"add",
"bindown-checksums",
"bindown-checksums",
"--var",
fmt.Sprintf("tag=%s", tag),
"--skipchecksums",
checksumsURL := fmt.Sprintf(
`https://github.com/WillAbides/bindown/releases/download/%s/checksums.txt`,
tag,
)
resp, err := http.Get(checksumsURL)
if err != nil {
return "", err
}
defer func() {
removeErr := execBindown(repoRoot, "dependency", "remove", "bindown-checksums")
if errOut != nil {
errOut = removeErr
err = resp.Body.Close()
if errOut == nil {
errOut = err
}
}()
checksumsDir := filepath.Join(tmpDir, "checksums")
err = execBindown(
repoRoot,
"extract",
"bindown-checksums",
"--allow-missing-checksum",
"--output",
checksumsDir,
)
if err != nil {
return "", err
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("got status %d from %s", resp.StatusCode, checksumsURL)
}
checksums, err := os.ReadFile(filepath.Join(checksumsDir, "checksums.txt"))
checksums, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down
11 changes: 7 additions & 4 deletions internal/build-bootstrapper/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"runtime"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,11 +19,15 @@ func TestBuild(t *testing.T) {
}
origConfig, err := os.ReadFile(filepath.FromSlash("../../bindown.yml"))
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(
t,
os.WriteFile(filepath.FromSlash("../../bindown.yml"), origConfig, 0o644),
)
})
got, err := build("v4.0.0", filepath.FromSlash("../../"))
require.NoError(t, err)
want, err := os.ReadFile(filepath.FromSlash("testdata/want.txt"))
require.NoError(t, err)
require.Empty(t, cmp.Diff(string(want), got))
err = os.WriteFile(filepath.FromSlash("../../bindown.yml"), origConfig, 0o644)
require.NoError(t, err)
require.Equal(t, string(want), got)
}
30 changes: 22 additions & 8 deletions internal/build-bootstrapper/testdata/want.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ usage() {
this=$1
cat << EOT
Usage: $this [-b bindir] [-d]

Usage: $this [-b] bindir [-d]
-b sets bindir or installation directory, Defaults to ./bin
-d turns on debug logging

Expand All @@ -267,6 +265,14 @@ parse_args() {
shift $((OPTIND - 1))
}

bindown_name() {
if [ "$OS" = "windows" ]; then
echo bindown.exe
else
echo bindown
fi
}

execute() {
tmpdir=$(mktemp -d)
echo "$CHECKSUMS" > "${tmpdir}/checksums.txt"
Expand All @@ -276,15 +282,17 @@ execute() {
srcdir="${tmpdir}"
(cd "${tmpdir}" && untar "${TARBALL}")
test ! -d "${BINDIR}" && install -d "${BINDIR}"
binexe="bindown"
if [ "$OS" = "windows" ]; then
binexe="${binexe}.exe"
fi
install "${srcdir}/${binexe}" "${BINDIR}/"
log_info "installed ${BINDIR}/${binexe}"
install "${srcdir}/$(bindown_name)" "${BINDIR}/"
log_info "installed ${BINDIR}/$(bindown_name)"
rm -rf "${tmpdir}"
}

already_installed() {
VERSION="$1"
[ -f "${BINDIR}/$(bindown_name)" ] &&
"${BINDIR}/$(bindown_name)" version 2> /dev/null | grep -q "$VERSION"
}

OS=$(uname_os)
ARCH=$(uname_arch)

Expand All @@ -294,6 +302,12 @@ uname_arch_check "$ARCH"
parse_args "$@"

VERSION=${TAG#v}

if already_installed "$VERSION"; then
log_debug "bindown ${VERSION} is already installed"
exit 0
fi

NAME=bindown_${VERSION}_${OS}_${ARCH}
TARBALL=${NAME}.${FORMAT}
TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
Expand Down
Loading