Skip to content

Commit 03eacaf

Browse files
committedApr 28, 2020
feat(opm): add version command
Provide an `opm version` command that can be tied into the release process.
1 parent bf0c8f1 commit 03eacaf

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
 

‎cmd/opm/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/operator-framework/operator-registry/cmd/opm/alpha"
1010
"github.com/operator-framework/operator-registry/cmd/opm/index"
1111
"github.com/operator-framework/operator-registry/cmd/opm/registry"
12+
"github.com/operator-framework/operator-registry/cmd/opm/version"
1213
)
1314

1415
func main() {
@@ -26,6 +27,7 @@ func main() {
2627

2728
rootCmd.AddCommand(registry.NewOpmRegistryCmd(), alpha.NewCmd())
2829
index.AddCommand(rootCmd)
30+
version.AddCommand(rootCmd)
2931

3032
rootCmd.Flags().Bool("debug", false, "enable debug logging")
3133
if err := rootCmd.Flags().MarkHidden("debug"); err != nil {

‎cmd/opm/version/version.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var (
11+
// opmVersion is the constant representing the version of the opm binary
12+
opmVersion = "unknown"
13+
// gitCommit is a constant representing the source version that
14+
// generated this build. It should be set during build via -ldflags.
15+
gitCommit string
16+
// buildDate in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
17+
buildDate string
18+
)
19+
20+
type Version struct {
21+
OpmVersion string `json:"opmVersion"`
22+
GitCommit string `json:"gitCommit"`
23+
BuildDate string `json:"buildDate"`
24+
GoOs string `json:"goOs"`
25+
GoArch string `json:"goArch"`
26+
}
27+
28+
func getVersion() Version {
29+
return Version{
30+
OpmVersion: opmVersion,
31+
GitCommit: gitCommit,
32+
BuildDate: buildDate,
33+
GoOs: runtime.GOOS,
34+
GoArch: runtime.GOARCH,
35+
}
36+
}
37+
38+
func (v Version) Print() {
39+
fmt.Printf("Version: %#v\n", v)
40+
}
41+
42+
func AddCommand(parent *cobra.Command) {
43+
cmd := &cobra.Command{
44+
Use: "version",
45+
Short: "Print the opm version",
46+
Long: `Print the opm version`,
47+
Example: `kubebuilder version`,
48+
Run: runVersion,
49+
}
50+
51+
parent.AddCommand(cmd)
52+
}
53+
54+
func runVersion(_ *cobra.Command, _ []string) {
55+
getVersion().Print()
56+
}

0 commit comments

Comments
 (0)
Please sign in to comment.