Skip to content

Commit

Permalink
Goreleaser enhancements
Browse files Browse the repository at this point in the history
make now generally leans on goreleaser instead of a separate build process

version string has much more interesting data in it.

better testing

.gitignore ignores  `dist`

CI for goreleaser config
  • Loading branch information
tempusfrangit committed Dec 14, 2023
1 parent c336304 commit 4d2d008
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 72 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/ci-goreleaser-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI goreleaser config

on:
push:
paths:
- '.goreleaser.yaml'
branches:
- main
tags:
- *
pull_request:
paths:
- '.goreleaser.yaml'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != format('refs/heads/{0}', github.event.repository.default_branch) }}

jobs:
test:
name: Test Goreleaser Config
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: goreleaser/goreleaser-action@v4
with:
args: check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pget
dist
11 changes: 9 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
project_name: pget
before:
hooks:
- go mod tidy
Expand All @@ -9,12 +10,16 @@ builds:
goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64
main: ./main.go
ignore:
- goos: windows
goarch: arm64
ldflags:
- "-s -w -X github.com/replicate/pget/pkg/version.Version={{.Version}} -X github.com/replicate/pget/pkg/version.CommitHash={{.ShortCommit}} -X github.com/replicate/pget/pkg/version.BuildTime={{.Date}}"
- "-s -w -X github.com/replicate/pget/pkg/version.Version={{.Version}} -X github.com/replicate/pget/pkg/version.CommitHash={{.ShortCommit}} -X github.com/replicate/pget/pkg/version.BuildTime={{.Date}} -X github.com/replicate/pget/pkg/version.Prerelease={{.Prerelease}} -X github.com/replicate/pget/pkg/version.OS={{.Os}} -X github.com/replicate/pget/pkg/version.Arch={{if eq .Arch \"amd64\"}}x86_64{{else if eq .Arch \"386\"}}i386{{else}}{{.Arch}}{{end}} -X github.com/replicate/pget/pkg/version.Snapshot={{.IsSnapshot}} -X github.com/replicate/pget/pkg/version.Branch={{.Branch}}"
archives:
- format: binary
name_template: >-
Expand All @@ -26,7 +31,9 @@ archives:
checksum:
name_template: "checksums.txt"
snapshot:
name_template: "{{ .Tag }}-next"
name_template: "{{ incminor .Version }}-devbuild"
universal_binaries:
- replace: false
changelog:
sort: asc
filters:
Expand Down
60 changes: 31 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,39 @@ BINDIR = $(PREFIX)/bin
INSTALL := install -m 0755
INSTALL_PROGRAM := $(INSTALL)


CHECKSUM_CMD := shasum -a 256
CHECKSUM_FILE := sha256sum.txt

GO := go
GOOS := $(shell $(GO) env GOOS)
GOARCH := $(shell $(GO) env GOARCH)
GORELEASER := goreleaser

GIT_TAG := $(shell git describe --tags --abbrev=0 2>/dev/null)
GIT_TAG_COMMIT := $(shell git rev-list -n 1 $(GIT_TAG) 2>/dev/null | cut -c1-7)
GIT_COMMIT := $(shell git rev-parse --short HEAD)
GIT_DIRTY := $(shell git diff --quiet && echo 0 || echo 1)
BUILD_TIME := $(shell date +%Y-%m-%dT%H:%M:%S%z)

ifeq ($(GIT_DIRTY),1)
VERSION := "development-$(GIT_COMMIT)-uncomitted-changes"
else ifeq ($(strip $(GIT_COMMIT)), $(strip $(GIT_TAG_COMMIT)))
VERSION := $(GIT_TAG)
else
VERSION := "development-$(GIT_COMMIT)"
endif

LD_FLAGS := -ldflags "-extldflags '-static' -X github.com/replicate/pget/pkg/version.Version=$(VERSION) -X github.com/replicate/pget/pkg/version.CommitHash=$(GIT_COMMIT) -X github.com/replicate/pget/pkg/version.BuildTime=$(BUILD_TIME) -w"
SINGLE_TARGET=--single-target

default: all

.PHONY: all
all: clean pget checksum

pget:
CGO_ENABLED=0 $(GO) build -o $@ \
$(LD_FLAGS) \
main.go
all: clean build

.PHONY: install
install: pget
install: build
$(INSTALL_PROGRAM) -d $(DESTDIR)$(BINDIR)
$(INSTALL_PROGRAM) pget $(DESTDIR)$(BINDIR)/pget

.PHONY: checksum
checksum: pget
$(CHECKSUM_CMD) pget | tee $(CHECKSUM_FILE)


.PHONY: uninstall
uninstall:
rm -f $(DESTDIR)$(BINDIR)/pget

.PHONY: clean
clean:
$(GO) clean
rm -f replicate
rm -rf dist
rm -f pget


.PHONY: test-all
test-all: test lint

.PHONY: test
test:
Expand All @@ -77,3 +58,24 @@ format:
.PHONY: tidy
tidy:
go mod tidy

.PHONY: check-goreleaser
check-goreleaser:
@command -v goreleaser >/dev/null 2>&1 || { echo >&2 "goreleaser is required but not installed. Aborting. Run 'make install-goreleaser' to install"; exit 1; }

.PHONY: install-goreleaser
install-goreleaser:
@command -v goreleaser >/dev/null 2>&1 || { \
echo >&2 "goreleaser is required but not installed. Installing..."; \
curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh; \
}

.PHONY: build
build: pget

.PHONY: build-all
build-all: SINGLE_TARGET:=
build-all: clean pget

pget: check-goreleaser
$(GORELEASER) build --snapshot --rm-dist $(SINGLE_TARGET) -o ./pget
36 changes: 28 additions & 8 deletions pkg/version/info.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
package version

import (
"fmt"
"strings"
import "fmt"

const (
snapshotString = "snapshot"
)

var (
// Version Build Time Injected information
Version string
CommitHash string
BuildTime string
Prerelease string
Snapshot string
OS string
Arch string
Branch string
)

// GetVersion returns the version information in a human consumable way. This is intended to be used
// when the user requests the version information or in the case of the User-Agent.
func GetVersion() string {
if strings.HasPrefix(Version, "development") {
// This is a development build
return Version
return makeVersionString(Version, CommitHash, BuildTime, Prerelease, Snapshot, OS, Arch, Branch)
}

func makeVersionString(version, commitHash, buildtime, prerelease, snapshot, os, arch, branch string) (versionString string) {
versionString = fmt.Sprintf("%s(%s)", version, commitHash)
if prerelease != "" {
versionString = fmt.Sprintf("%s-%s", versionString, prerelease)
} else if snapshot != "" {
versionString = fmt.Sprintf("%s-%s", versionString, snapshotString)
}

if branch != "" && branch != "Main" && branch != "HEAD" {
versionString = fmt.Sprintf("%s[%s]", versionString, branch)
}

if os != "" && arch != "" {
versionString = fmt.Sprintf("%s/%s-%s", versionString, os, arch)
} else if os != "" {
versionString = fmt.Sprintf("%s/%s", versionString, os)
}
// This should be a tagged release
return fmt.Sprintf("%s(%s)", Version, CommitHash)

return versionString
}
86 changes: 86 additions & 0 deletions pkg/version/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package version

import (
"testing"
)

func Test_makeVersionString(t *testing.T) {
type args struct {
version string
commitHash string
buildtime string
prerelease string
snapshot string
os string
arch string
branch string
}
tests := []struct {
name string
args args
expected string
}{
{
name: "Typical Development",
args: args{
version: "1.0.0",
commitHash: "abc123",
os: "darwin",
arch: "amd64",
branch: "Branch1",
},
expected: "1.0.0(abc123)[Branch1]/darwin-amd64",
},
{
name: "With prerelease and snapshot",
args: args{
version: "1.0.0",
commitHash: "abc123",
prerelease: "alpha",
snapshot: "20221130",
os: "darwin",
arch: "amd64",
branch: "Branch1",
},
expected: "1.0.0(abc123)-alpha[Branch1]/darwin-amd64",
},
{
name: "No os or arch",
args: args{
version: "1.0.0",
commitHash: "abc123",
branch: "Branch1",
},
expected: "1.0.0(abc123)[Branch1]",
},
{
name: "Branch Main",
args: args{
version: "1.0.0",
commitHash: "abc123",
os: "darwin",
arch: "amd64",
branch: "Main",
},
expected: "1.0.0(abc123)/darwin-amd64",
},
{
name: "Branch HEAD",
args: args{
version: "1.0.0",
commitHash: "abc123",
os: "darwin",
arch: "amd64",
branch: "HEAD",
},
expected: "1.0.0(abc123)/darwin-amd64",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := makeVersionString(tt.args.version, tt.args.commitHash, tt.args.buildtime, tt.args.prerelease, tt.args.snapshot, tt.args.os, tt.args.arch, tt.args.branch); got != tt.expected {
t.Errorf("makeVersionString() = %v, expected %v", got, tt.expected)
}
})
}
}
33 changes: 0 additions & 33 deletions pkg/version/version_test.go

This file was deleted.

0 comments on commit 4d2d008

Please sign in to comment.