diff --git a/.goreleaser.yml b/.goreleaser.yml index 70c0015..ce4360d 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -13,9 +13,13 @@ builds: goos: [darwin] goarch: [arm64, amd64] +# The name has no version, so that the link of the latest release works in +# the curl command of the README: +# https://github.com/frinfo702/rexdiff/releases/latest/download/rexdiff-darwin-arm64.tar.gz +# update/update.go builds the same name. archives: - formats: [tar.gz] - name_template: "{{ .ProjectName }}-v{{ .Version }}-{{ .Os }}-{{ .Arch }}" + name_template: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}" checksum: name_template: sha256sums.txt diff --git a/README.md b/README.md index 9189fcb..30f6b5e 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,16 @@ brew trust frinfo702/rexdiff brew install --cask rexdiff ``` +With curl: + +```sh +curl -fsSL https://raw.githubusercontent.com/frinfo702/rexdiff/main/install.sh | sh +``` + +The script downloads the release for this Mac, checks its checksum, and puts +the binary in `~/.local/bin`, or in `REXDIFF_BIN_DIR`. It also says how to add +the directory to `PATH` if it is not there. + With Go: ```sh diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..40d4921 --- /dev/null +++ b/install.sh @@ -0,0 +1,54 @@ +#!/bin/sh +# install.sh installs the latest rexdiff release for this Mac: +# +# curl -fsSL https://raw.githubusercontent.com/frinfo702/rexdiff/main/install.sh | sh +# +# It puts the binary in ~/.local/bin, or in REXDIFF_BIN_DIR, and checks the +# checksum of the archive. + +set -eu + +repo=frinfo702/rexdiff +base="https://github.com/$repo/releases/latest/download" +dir=${REXDIFF_BIN_DIR:-"$HOME/.local/bin"} + +fail() { + printf 'install.sh: %s\n' "$*" >&2 + exit 1 +} + +[ "$(uname -s)" = Darwin ] || fail "rexdiff runs on macOS only" + +case $(uname -m) in +arm64) arch=arm64 ;; +x86_64) arch=amd64 ;; +*) fail "unsupported machine: $(uname -m)" ;; +esac + +name="rexdiff-darwin-$arch.tar.gz" +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT HUP INT TERM + +curl -fsSL "$base/$name" -o "$tmp/$name" || + fail "cannot download $base/$name" +curl -fsSL "$base/sha256sums.txt" -o "$tmp/sha256sums.txt" || + fail "cannot download $base/sha256sums.txt" + +want=$(awk -v name="$name" '$2 == name { print $1 }' "$tmp/sha256sums.txt") +[ -n "$want" ] || fail "the checksums have no $name" +got=$(shasum -a 256 "$tmp/$name" | awk '{ print $1 }') +[ "$want" = "$got" ] || fail "$name does not match its checksum" + +tar -xzf "$tmp/$name" -C "$tmp" rexdiff || fail "cannot unpack $name" +mkdir -p "$dir" +install -m 0755 "$tmp/rexdiff" "$dir/.rexdiff-new" +mv -f "$dir/.rexdiff-new" "$dir/rexdiff" +printf 'installed %s\n' "$dir/rexdiff" + +case ":$PATH:" in +*":$dir:"*) ;; +*) + printf 'add %s to PATH, for example in ~/.zshrc:\n' "$dir" + printf ' export PATH="%s:$PATH"\n' "$dir" + ;; +esac diff --git a/update/update.go b/update/update.go index 76e0a08..6920b33 100644 --- a/update/update.go +++ b/update/update.go @@ -258,7 +258,7 @@ func lastLine(out []byte) string { // checksums of r, and puts its binary in place of the file that u.bin // links to. func (u *Updater) download(ctx context.Context, r Release) error { - name := fmt.Sprintf("rexdiff-%s-%s-%s.tar.gz", r.Tag, runtime.GOOS, runtime.GOARCH) + name := fmt.Sprintf("rexdiff-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH) archiveURL, sumsURL := assetURL(r, name), assetURL(r, "sha256sums.txt") if archiveURL == "" || sumsURL == "" { return fmt.Errorf("release %s has no %s or sha256sums.txt", r.Tag, name) diff --git a/update/update_test.go b/update/update_test.go index 815aefb..9453834 100644 --- a/update/update_test.go +++ b/update/update_test.go @@ -216,7 +216,7 @@ func archive(t *testing.T, content string) []byte { // TestDownload checks the download of a release: the checksum, the binary // in the archive, and the replacement of the target file of a link. func TestDownload(t *testing.T) { - name := fmt.Sprintf("rexdiff-v0.2.0-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH) + name := fmt.Sprintf("rexdiff-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH) good := archive(t, "new binary") sum := sha256.Sum256(good) files := map[string][]byte{