Skip to content

Commit bd9aed0

Browse files
AlexsJonesskyerusbeeme1mrtoddbaert
authored
feat: Version (#141)
* wip Signed-off-by: Alex Jones <[email protected]> * fumped Signed-off-by: Alex Jones <[email protected]> * needs testing Signed-off-by: Alex Jones <[email protected]> * feat: added support for environment variables (#142) * Added support for environment variables. Documented configurability. Signed-off-by: Skye Gill <[email protected]> * Prefix environment variables with FLAGD_ Signed-off-by: Skye Gill <[email protected]> Signed-off-by: Skye Gill <[email protected]> Signed-off-by: Alex Jones <[email protected]> * docs: add install instructions (#143) * add install instructions s Signed-off-by: Michael Beemer <[email protected]> * Update README.md Co-authored-by: Todd Baert <[email protected]> Signed-off-by: Michael Beemer <[email protected]> * add note about systemd Signed-off-by: Michael Beemer <[email protected]> * move configuration options link Signed-off-by: Michael Beemer <[email protected]> Signed-off-by: Michael Beemer <[email protected]> Co-authored-by: Todd Baert <[email protected]> Signed-off-by: Alex Jones <[email protected]> * Create CODEOWNERS Signed-off-by: Alex Jones <[email protected]> * addressing concerns Signed-off-by: Alex Jones <[email protected]> * fumpt Signed-off-by: Alex Jones <[email protected]> Signed-off-by: Alex Jones <[email protected]> Signed-off-by: Skye Gill <[email protected]> Signed-off-by: Michael Beemer <[email protected]> Co-authored-by: Skye Gill <[email protected]> Co-authored-by: Michael Beemer <[email protected]> Co-authored-by: Todd Baert <[email protected]>
1 parent 182e388 commit bd9aed0

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

.github/workflows/release.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ jobs:
4040
- name: Set up Docker Buildx
4141
id: buildx
4242
uses: docker/setup-buildx-action@master
43-
43+
- name: Get current date
44+
id: date
45+
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
4446
- name: Build
4547
uses: docker/build-push-action@v2
4648
with:
@@ -51,6 +53,10 @@ jobs:
5153
push: true
5254
tags: ${{ steps.meta.outputs.tags }}
5355
labels: ${{ steps.meta.outputs.labels }}
56+
build-args: |
57+
VERSION=${{ steps.meta.outputs.tags }}
58+
COMMIT=${{ github.sha }}
59+
DATE=${{ steps.date.outputs.date }}
5460
goreleaser:
5561
runs-on: ubuntu-latest
5662
steps:

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ FROM --platform=$BUILDPLATFORM golang:1.18-alpine AS builder
44
WORKDIR /workspace
55
ARG TARGETOS
66
ARG TARGETARCH
7+
ARG VERSION
8+
ARG COMMIT
9+
ARG DATE
710
# Copy the Go Modules manifests
811
COPY go.mod go.mod
912
COPY go.sum go.sum
@@ -17,7 +20,7 @@ COPY cmd/ cmd/
1720
COPY pkg/ pkg/
1821

1922
# Build
20-
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -o flagd main.go
23+
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -ldflags "-X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=${DATE}" -o flagd main.go
2124

2225
# Use distroless as minimal base image to package the manager binary
2326
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
IMG ?= flagd:latest
22
PHONY: .docker-build .build .run .mockgen
33
PREFIX=/usr/local
4+
45
guard-%:
56
@ if [ "${${*}}" = "" ]; then \
67
echo "Environment variable $* not set"; \
78
exit 1; \
89
fi
910
docker-build:
10-
docker buildx build --platform="linux/ppc64le,linux/s390x,linux/amd64,linux/arm64" -t ${IMG} .
11+
docker buildx build --build-arg=VERSION="$$(git describe --tags --abbrev=0)" --build-arg=COMMIT="$$(git rev-parse --short HEAD)" --build-arg DATE="$$(date +%Y%m%d)" --platform="linux/ppc64le,linux/s390x,linux/amd64,linux/arm64" -t ${IMG} . --load
1112
docker-push:
12-
docker buildx build --push --platform="linux/ppc64le,linux/s390x,linux/amd64,linux/arm64" -t ${IMG} .
13+
docker buildx build --push --build-arg=VERSION="$$(git describe --tags --abbrev=0)" --build-arg=COMMIT="$$(git rev-parse --short HEAD)" --build-arg DATE="$$(date +%Y%m%d)" --platform="linux/ppc64le,linux/s390x,linux/amd64,linux/arm64" -t ${IMG} .
1314
build:
14-
go build -o flagd
15+
go build -ldflags "-X main.version=dev -X main.commit=$$(git rev-parse --short HEAD) -X main.date=$$(date +%Y%m%d)" -o flagd
1516
test:
1617
go test -cover ./...
1718
run:

cmd/root.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import (
88
"github.com/spf13/viper"
99
)
1010

11-
var cfgFile string
11+
var (
12+
cfgFile string
13+
Version string
14+
Commit string
15+
Date string
16+
)
1217

1318
var rootCmd = &cobra.Command{
1419
Use: "flagd",
@@ -22,7 +27,10 @@ var rootCmd = &cobra.Command{
2227

2328
// Execute adds all child commands to the root command and sets flags appropriately.
2429
// This is called by main.main(). It only needs to happen once to the rootCmd.
25-
func Execute() {
30+
func Execute(version string, commit string, date string) {
31+
Version = version
32+
Commit = commit
33+
Date = date
2634
err := rootCmd.Execute()
2735
if err != nil {
2836
os.Exit(1)

cmd/version.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
// versionCmd represents the version command
14+
var versionCmd = &cobra.Command{
15+
Use: "version",
16+
Short: "Print the version number of FlagD",
17+
Long: ``,
18+
Run: func(cmd *cobra.Command, args []string) {
19+
fmt.Printf("flagd %s (%s) built at %s\n", Version, Commit, Date)
20+
},
21+
}
22+
23+
func init() {
24+
rootCmd.AddCommand(versionCmd)
25+
}

main.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package main
22

3-
import "github.com/open-feature/flagd/cmd"
3+
import (
4+
"github.com/open-feature/flagd/cmd"
5+
)
6+
7+
var (
8+
version = "dev"
9+
commit = "HEAD"
10+
date = "unknown"
11+
)
412

513
func main() {
6-
cmd.Execute()
14+
cmd.Execute(version, commit, date)
715
}

0 commit comments

Comments
 (0)