Skip to content

Commit 1502c16

Browse files
committed
Print version information when using --version flag
Signed-off-by: Caleb Xu <[email protected]>
1 parent 7ceecad commit 1502c16

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ARG OS
1212
# Build the preflight binary
1313
COPY . /go/src/preflight-trigger
1414
WORKDIR /go/src/preflight-trigger
15-
RUN make build
15+
RUN make build RELEASE_TAG=${release_tag}
1616

1717

1818
FROM registry.access.redhat.com/ubi9/ubi-micro:latest

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ export CONTAINER_ENGINE ?= podman
88
export BIN_NAME
99
export REGISTRY ?= quay.io
1010
export REGISTRY_NAMESPACE ?= opdev
11+
VERSION=$(shell git rev-parse HEAD)
1112
export RELEASE_TAG ?= "0.0.0"
1213

1314

1415
.PHONY: binary-build
1516
binary-build:
16-
$(CONTAINER_ENGINE) run --rm -v $(PWD):/usr/src/$(BIN_NAME) -w /usr/src/$(BIN_NAME) -e GOOS=linux -e GOARCH=amd64 docker.io/library/golang:alpine go build -o $(BIN_NAME)
17+
$(CONTAINER_ENGINE) run --rm -v $(PWD):/usr/src/$(BIN_NAME) -w /usr/src/$(BIN_NAME) -e GOOS=linux -e GOARCH=amd64 docker.io/library/golang:alpine go build -ldflags "-X github.com/redhat-openshift-ecosystem/preflight-trigger/cmd.commit=$(VERSION) -X github.com/redhat-openshift-ecosystem/preflight-trigger/cmd.version=$(RELEASE_TAG)" -o $(BIN_NAME)
1718

1819
.PHONY: image-build
1920
image-build:
@@ -25,7 +26,7 @@ image-push:
2526

2627
.PHONY: build
2728
build:
28-
CGO_ENABLED=0 go build -o $(BIN_NAME) main.go
29+
CGO_ENABLED=0 go build -ldflags "-X github.com/redhat-openshift-ecosystem/preflight-trigger/cmd.commit=$(VERSION) -X github.com/redhat-openshift-ecosystem/preflight-trigger/cmd.version=$(RELEASE_TAG)" -o $(BIN_NAME) main.go
2930
@ls | grep -e '^preflight-trigger$$' &> /dev/null
3031

3132
.PHONY: vet

cmd/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ var CommandFlags FlagsData
5858

5959
// rootCmd represents the base command when called without any subcommands
6060
var rootCmd = &cobra.Command{
61-
Use: "preflight-trigger",
62-
Short: "Create on-demand preflight jobs in openshift-ci system",
63-
Long: ``,
61+
Use: "preflight-trigger",
62+
Short: "Create on-demand preflight jobs in openshift-ci system",
63+
Long: ``,
64+
Version: Version.String(),
6465
}
6566

6667
// Execute adds all child commands to the root command and sets flags appropriately.

cmd/version.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cmd
2+
3+
import "fmt"
4+
5+
var (
6+
projectName = "github.com/redhat-openshift-ecosystem/preflight-trigger"
7+
version = "unknown"
8+
commit = "unknown"
9+
)
10+
11+
var Version = VersionContext{
12+
Name: projectName,
13+
Version: version,
14+
Commit: commit,
15+
}
16+
17+
type VersionContext struct {
18+
Name string `json:"name"`
19+
Version string `json:"version"`
20+
Commit string `json:"commit"`
21+
}
22+
23+
func (vc *VersionContext) String() string {
24+
return fmt.Sprintf("%s <commit: %s>", vc.Version, vc.Commit)
25+
}

0 commit comments

Comments
 (0)