Skip to content

Commit fe75323

Browse files
committed
Add version command
Adds a `version` command that can be used to display the current binary version. Example: ./ingress2gateway version ingress2gateway version: v0.4.0 Built with Go version: go1.24.2 * This is implemented via a combination of ldflags (to propogate the git tag info) and debug.BuildInfo (to get the golang version). * This does not modify the CurrentVersion var in the i2gw package, but plan is to migrate that in a follow-up.
1 parent 728bd8c commit fe75323

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

Makefile

+14-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
# Enable Go modules.
1919
export GO111MODULE=on
2020

21+
# CMDPKG is the package path for cmd. This allows us to propogate the git info
22+
# to the binary via LDFLAGS.
23+
CMDPKG := $(shell go list .)/cmd
24+
25+
# Get the version string from git describe.
26+
# --tags: Use annotated tags.
27+
# --always: Fallback to commit hash if no tag is found.
28+
# --dirty: Append -dirty if the working directory has uncommitted changes.
29+
GIT_VERSION_STRING := $(shell git describe --tags --always --dirty 2>/dev/null)
30+
31+
# Construct the LDFLAGS string to inject the version
32+
LDFLAGS := -ldflags="-X '$(CMDPKG).Version=$(GIT_VERSION_STRING)'"
33+
2134
# Print the help menu.
2235
.PHONY: help
2336
help:
@@ -45,7 +58,7 @@ test: vet;$(info $(M)...Begin to run tests.) @ ## Run tests.
4558
# Build the binary
4659
.PHONY: build
4760
build: vet;$(info $(M)...Build the binary.) @ ## Build the binary.
48-
go build -o ingress2gateway .
61+
go build $(LDFLAGS) -o ingress2gateway .
4962

5063
# Run static analysis.
5164
.PHONY: verify

cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func getKubeconfig() {
4848
func Execute() {
4949
rootCmd := newRootCmd()
5050
rootCmd.AddCommand(newPrintCommand())
51+
rootCmd.AddCommand(versionCmd)
5152
err := rootCmd.Execute()
5253
if err != nil {
5354
os.Exit(1)

cmd/version.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"fmt"
21+
"runtime/debug"
22+
23+
"github.com/spf13/cobra"
24+
)
25+
26+
// Version holds the version string (injected by ldflags during build).
27+
// It will be populated by `git describe --tags --always --dirty`.
28+
// Examples: "v0.4.0", "v0.4.0-5-gabcdef", "v0.4.0-5-gabcdef-dirty"
29+
var Version = "dev" // Default value if not built with linker flags
30+
31+
// versionCmd represents the version command
32+
var versionCmd = &cobra.Command{
33+
Use: "version",
34+
Short: "Print the version number of ingress2gateway",
35+
Long: "Prints the build version details for ingress2gateway, including Git status information and Go version",
36+
Run: func(_ *cobra.Command, _ []string) {
37+
printVersion() // Call the helper function
38+
},
39+
}
40+
41+
// printVersion formats and prints the version information.
42+
func printVersion() {
43+
fmt.Printf("ingress2gateway version: %s\n", Version)
44+
45+
// Print the golang version if it's available
46+
buildInfo, ok := debug.ReadBuildInfo()
47+
if ok && buildInfo != nil {
48+
fmt.Printf("Built with Go version: %s\n", buildInfo.GoVersion)
49+
}
50+
}

0 commit comments

Comments
 (0)