diff --git a/.github/workflows/ci-goreleaser-config.yml b/.github/workflows/ci-goreleaser-config.yml new file mode 100644 index 0000000..5b61e5e --- /dev/null +++ b/.github/workflows/ci-goreleaser-config.yml @@ -0,0 +1,28 @@ +name: goreleaser config test + +on: + push: + paths: + - '.goreleaser.yaml' + branches: + - main + tags: + - * + pull_request: + paths: + - '.goreleaser.yaml' + workflow_dispatch: + +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 diff --git a/.gitignore b/.gitignore index fc58573..e0549ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ pget +dist diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 50d8d97..974ec52 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,3 +1,4 @@ +project_name: pget before: hooks: - go mod tidy @@ -14,7 +15,7 @@ builds: - arm64 main: ./main.go 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: >- @@ -26,7 +27,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: diff --git a/Makefile b/Makefile index a84b1a4..ebf9322 100644 --- a/Makefile +++ b/Makefile @@ -7,50 +7,26 @@ 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 @@ -58,7 +34,12 @@ uninstall: .PHONY: clean clean: $(GO) clean - rm -f replicate + rm -rf dist + rm -f pget + + +.PHONY: test-all +test-all: test lint .PHONY: test test: @@ -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 diff --git a/cmd/root/root.go b/cmd/root/root.go index c27af41..8aea6a1 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -92,12 +92,12 @@ func GetCommand() *cobra.Command { } // defaultPidFilePath returns the default path for the PID file. Notably modern OS X variants -// have permissioning difficulties in /var/run etc. +// have permissions difficulties in /var/run etc. func defaultPidFilePath() string { - // If we're on OS X or Windows, use the user's home directory + // If we're on OS X, use the user's home directory // Otherwise, use /run path := "/run/pget.pid" - if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { + if runtime.GOOS == "darwin" { path = os.Getenv("HOME") + "/.pget.pid" } return path diff --git a/pkg/cli/pid.go b/pkg/cli/pid.go index f8eb0c4..0c1fa8e 100644 --- a/pkg/cli/pid.go +++ b/pkg/cli/pid.go @@ -1,3 +1,5 @@ +//go:build !windows + package cli import ( diff --git a/pkg/version/info.go b/pkg/version/info.go index fa97e7d..becf4bb 100644 --- a/pkg/version/info.go +++ b/pkg/version/info.go @@ -1,8 +1,9 @@ package version -import ( - "fmt" - "strings" +import "fmt" + +const ( + snapshotString = "snapshot" ) var ( @@ -10,17 +11,36 @@ var ( 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 } diff --git a/pkg/version/info_test.go b/pkg/version/info_test.go new file mode 100644 index 0000000..ee5fcb7 --- /dev/null +++ b/pkg/version/info_test.go @@ -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) + } + }) + } +} diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go deleted file mode 100644 index e58cedf..0000000 --- a/pkg/version/version_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package version - -import ( - "testing" -) - -func TestGetVersion(t *testing.T) { - defer func() { - Version = "" - CommitHash = "" - BuildTime = "" - }() - - testCases := []struct { - name string - version string - commit string - expected string - }{ - {"development", "development", "", "development"}, - {"tagged release", "v1.0.0", "deadbeef", "v1.0.0()"}, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - Version = tc.version - actual := GetVersion() - if actual != tc.expected { - t.Errorf("expected %s, got %s", tc.expected, actual) - } - }) - } -}