Преглед на файлове

feat: add PizzaSQL release identity

Danilo Fragoso преди 1 ден
родител
ревизия
cdd12f1df8
променени са 5 файла, в които са добавени 148 реда и са изтрити 1 реда
  1. 64 0
      .github/workflows/ci.yml
  2. 1 0
      .gitignore
  3. 11 1
      Makefile
  4. 6 0
      main.go
  5. 66 0
      pkg/version/version_test.go

+ 64 - 0
.github/workflows/ci.yml

@@ -0,0 +1,64 @@
+name: CI
+
+on:
+  push:
+    branches: [main, master]
+  pull_request:
+  workflow_dispatch:
+
+permissions:
+  contents: read
+
+env:
+  GOPROXY: https://proxy.golang.org
+
+jobs:
+  verify:
+    name: Build, test, and verify FreeBSD ARM64 artifacts
+    runs-on: ubuntu-latest
+    steps:
+      - name: Checkout
+        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
+        with:
+          fetch-depth: 0
+
+      - name: Install Go
+        uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
+        with:
+          go-version: '1.25.x'
+
+      - name: Check formatting
+        run: test -z "$(gofmt -l .)"
+
+      - name: Run tests
+        run: make test
+
+      - name: Build native binary
+        run: make build
+
+      - name: Verify version output
+        run: ./bin/pizzasql -version
+
+      - name: Cross-compile release targets
+        run: |
+          make build-linux-amd64
+          make build-freebsd-amd64
+          make build-freebsd-arm64
+
+      - name: Stage artifacts and checksums
+        run: |
+          version="$(make -s version)"
+          mkdir -p dist
+          cp bin/pizzasql                 "dist/pizzasql-${version}-linux-amd64"
+          cp bin/pizzasql-freebsd-amd64   "dist/pizzasql-${version}-freebsd-amd64"
+          cp bin/pizzasql-freebsd-arm64   "dist/pizzasql-${version}-freebsd-arm64"
+          (cd dist && sha256sum ./* > SHA256SUMS)
+          cat dist/SHA256SUMS
+
+      - name: Upload verification artifacts
+        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
+        with:
+          name: pizzasql-${{ github.sha }}
+          path: dist/
+          retention-days: 14
+          if-no-files-found: error

+ 1 - 0
.gitignore

@@ -3,5 +3,6 @@
 testdata
 .db
 *.log
+dist/
 /pizzasql_*
 # runtime state lives in /tmp/pizzasql/runtime.json — no local files to ignore

+ 11 - 1
Makefile

@@ -1,4 +1,4 @@
-.PHONY: build test test-v test-cover bench clean fmt lint sqllogictest sqllogictest-basic sqllogictest-download build-sqllogictest install uninstall
+.PHONY: build build-freebsd-amd64 build-freebsd-arm64 test test-v test-cover bench clean fmt lint version sqllogictest sqllogictest-basic sqllogictest-download build-sqllogictest install uninstall
 
 PREFIX ?= /usr/local
 
@@ -25,6 +25,16 @@ uninstall:
 build-linux-amd64:
 	GOOS=linux GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./bin/pizzasql-linux-amd64 ./main.go
 
+build-freebsd-amd64:
+	GOOS=freebsd GOARCH=amd64 go build -ldflags "$(LDFLAGS)" -o ./bin/pizzasql-freebsd-amd64 ./main.go
+
+build-freebsd-arm64:
+	GOOS=freebsd GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o ./bin/pizzasql-freebsd-arm64 ./main.go
+
+# Print the version that a tag build would embed (git describe, commit-hash fallback).
+version:
+	@echo "$(GIT_VERSION)"
+
 # Run all tests
 test:
 	go test ./...

+ 6 - 0
main.go

@@ -62,6 +62,7 @@ var (
 	ignoreErrors = flag.Bool("ignore-errors", false, "Continue import on errors")
 	exportFormat = flag.String("format", "", "Export/import format: sql, csv (auto-detect from extension)")
 	createTable  = flag.Bool("create-table", false, "Create table if not exists (CSV import)")
+	showVersion  = flag.Bool("version", false, "Print version and exit")
 )
 
 var kvManager *kvmanager.Manager
@@ -70,6 +71,11 @@ var startPprofServerHook func() *http.Server
 func main() {
 	flag.Parse()
 
+	if *showVersion {
+		fmt.Println(version.String())
+		return
+	}
+
 	// Warn if other pizzasql instances are running; prompt to continue.
 	if err := pizzaruntime.CheckExistingInstances(*forceYes); err != nil {
 		fmt.Fprintf(os.Stderr, "%v\n", err)

+ 66 - 0
pkg/version/version_test.go

@@ -0,0 +1,66 @@
+package version
+
+import (
+	"strings"
+	"testing"
+)
+
+func TestString(t *testing.T) {
+	tests := []struct {
+		name    string
+		version string
+		commit  string
+		date    string
+		dirty   string
+		want    string
+	}{
+		{name: "semver without date", version: "1.2.3", commit: "abcdef123456", dirty: "", want: "1.2.3"},
+		{name: "dev falls back to commit", version: "dev", commit: "abcdef123456", dirty: "", want: "abcdef123456"},
+		{name: "dev without commit", version: "dev", commit: "unknown", dirty: "", want: "dev"},
+		{name: "empty version falls back to commit", version: "", commit: "abcdef123456", dirty: "", want: "abcdef123456"},
+		{name: "dirty suffix", version: "1.2.3", commit: "abcdef123456", dirty: "-dirty", want: "1.2.3-dirty"},
+		{name: "dirty suffix not duplicated", version: "1.2.3-dirty", commit: "abcdef123456", dirty: "-dirty", want: "1.2.3-dirty"},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			Version, Commit, Date, Dirty = tt.version, tt.commit, tt.date, tt.dirty
+			if got := String(); got != tt.want {
+				t.Fatalf("String() = %q, want %q", got, tt.want)
+			}
+		})
+	}
+}
+
+func TestStringIncludesSanitizedDate(t *testing.T) {
+	Version, Commit, Date, Dirty = "1.2.3", "abcdef123456", "2024-01-02T03:04:05+00:00", ""
+	got := String()
+	if !strings.HasPrefix(got, "1.2.3_") {
+		t.Fatalf("String() = %q, want a date suffix after the version", got)
+	}
+	if strings.ContainsAny(got, ": ") {
+		t.Fatalf("String() = %q, want dates without separators", got)
+	}
+}
+
+func TestNormalizeDateRemovesSeparators(t *testing.T) {
+	for _, in := range []string{
+		"2024-01-02T03:04:05+00:00",
+		"2024-01-02 03:04:05",
+		"  2024-01-02T03:04:05Z  ",
+	} {
+		got := normalizeDate(in)
+		if strings.ContainsAny(got, ": ") {
+			t.Fatalf("normalizeDate(%q) = %q, want no spaces or colons", in, got)
+		}
+	}
+}
+
+func TestShortCommit(t *testing.T) {
+	if got := shortCommit("abcdef1234567890"); got != "abcdef123456" {
+		t.Fatalf("shortCommit(long) = %q, want 12 characters", got)
+	}
+	if got := shortCommit("abc"); got != "abc" {
+		t.Fatalf("shortCommit(short) = %q, want unchanged", got)
+	}
+}