Skip to content

Commit

Permalink
Support for version
Browse files Browse the repository at this point in the history
  • Loading branch information
shayonj committed Nov 3, 2024
1 parent ec84ad8 commit 087c113
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .github/workflows/release-and-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ jobs:

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
buildkitd-flags: --debug

- name: Login to DockerHub
uses: docker/login-action@v3
Expand All @@ -69,3 +71,7 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=${{ github.ref_name }}
COMMIT=${{ github.sha }}
DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
5 changes: 5 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ builds:
goarch:
- amd64
- arm64
ldflags:
- -s -w
- -X github.com/shayonj/pg_flo/cmd.version={{.Version}}
- -X github.com/shayonj/pg_flo/cmd.commit={{.Commit}}
- -X github.com/shayonj/pg_flo/cmd.date={{.Date}}
binary: pg_flo

archives:
Expand Down
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o pg_flo .
ARG VERSION=dev
ARG COMMIT=none
ARG DATE=unknown
RUN go build -v -ldflags="-s -w -X github.com/shayonj/pg_flo/cmd.version=${VERSION} -X github.com/shayonj/pg_flo/cmd.commit=${COMMIT} -X github.com/shayonj/pg_flo/cmd.date=${DATE}" -o pg_flo .

FROM alpine:latest
COPY --from=builder /app/pg_flo /usr/local/bin/
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![CI](https://github.com/shayonj/pg_flo/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/shayonj/pg_flo/actions/workflows/ci.yml)
[![Integration](https://github.com/shayonj/pg_flo/actions/workflows/integration.yml/badge.svg?branch=main)](https://github.com/shayonj/pg_flo/actions/workflows/integration.yml)
[![Release](https://img.shields.io/github/v/release/shayonj/pg_flo?sort=semver)](https://github.com/shayonj/pg_flo/releases/latest)
[![Docker Image](https://img.shields.io/docker/v/shayonj/pg_flo?label=docker&sort=semver)](https://hub.docker.com/r/shayonj/pg_flo/tags)

## Table of Contents

Expand Down
20 changes: 20 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ var (
Short: "Start the worker with webhook sink",
Run: runWorker,
}

// Version information set by goreleaser
version = "dev"
commit = "none"
date = "unknown"
)

func Execute() error {
Expand Down Expand Up @@ -134,6 +139,17 @@ func init() {
workerCmd.AddCommand(stdoutWorkerCmd, fileWorkerCmd, postgresWorkerCmd, webhookWorkerCmd)

rootCmd.AddCommand(replicatorCmd, workerCmd)

// Add version command
versionCmd := &cobra.Command{
Use: "version",
Short: "Print version information",
Run: func(cmd *cobra.Command, args []string) {

Check failure on line 147 in cmd/root.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive)
fmt.Printf("pg_flo version %s\n", formatVersion())
},
}

rootCmd.AddCommand(versionCmd)
}

func initConfig() {
Expand Down Expand Up @@ -352,3 +368,7 @@ func markPersistentFlagRequired(cmd *cobra.Command, flags ...string) {
}
}
}

func formatVersion() string {
return fmt.Sprintf("%s (commit: %s, built: %s)", version, commit, date)
}

0 comments on commit 087c113

Please sign in to comment.