|
| 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