diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..751b818 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,48 @@ +package version + +import ( + "fmt" + "runtime" +) + +var ( + gitMajor string // major version, always numeric + gitMinor string // minor version, numeric possibly followed by "+" + gitVersion string // semantic version, derived by build scripts + gitCommit string // sha1 from git, output of $(git rev-parse HEAD) + gitTreeState string // state of git tree, either "clean" or "dirty" + buildDate string // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') +) + +// Info defines the version. +type Info struct { + Major string `json:"major,omitempty"` + Minor string `json:"minor,omitempty"` + GitVersion string `json:"gitVersion,omitempty"` + GitCommit string `json:"gitCommit,omitempty"` + GitTreeState string `json:"gitTreeState,omitempty"` + BuildDate string `json:"buildDate,omitempty"` + GoVersion string `json:"goVersion,omitempty"` + Compiler string `json:"compiler,omitempty"` + Platform string `json:"platform,omitempty"` +} + +// Get returns metadata and information regarding the version. +func Get() Info { + return Info{ + Major: gitMajor, + Minor: gitMinor, + GitVersion: gitVersion, + GitCommit: gitCommit, + GitTreeState: gitTreeState, + BuildDate: buildDate, + GoVersion: runtime.Version(), + Compiler: runtime.Compiler, + Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), + } +} + +// String returns info as a human-friendly version string. +func (info Info) String() string { + return info.GitVersion +} diff --git a/pkg/cmd/lineage/lineage.go b/pkg/cmd/lineage/lineage.go index e76830e..ff4763b 100644 --- a/pkg/cmd/lineage/lineage.go +++ b/pkg/cmd/lineage/lineage.go @@ -114,6 +114,7 @@ func New(streams genericclioptions.IOStreams) *cobra.Command { o.ConfigFlags.AddFlags(cmd.Flags()) o.PrintFlags.AddFlags(cmd.Flags()) addLogFlags(cmd.Flags()) + addVersionFlag(cmd) return cmd } @@ -202,6 +203,7 @@ func (o *CmdOptions) Validate() error { return errors.New("client config, client must be provided") } + klog.V(4).Infof("Version: %s", getVersion()) klog.V(4).Infof("Namespace: %s", o.Namespace) klog.V(4).Infof("Resource: %s", o.Resource) klog.V(4).Infof("ResourceName: %s", o.ResourceName) diff --git a/pkg/cmd/lineage/version.go b/pkg/cmd/lineage/version.go new file mode 100644 index 0000000..705d13a --- /dev/null +++ b/pkg/cmd/lineage/version.go @@ -0,0 +1,17 @@ +package lineage + +import ( + "fmt" + + "github.com/spf13/cobra" + "github.com/tohjustin/kube-lineage/internal/version" +) + +func getVersion() string { + return fmt.Sprintf("%#v", version.Get()) +} + +func addVersionFlag(c *cobra.Command) { + c.Version = getVersion() + c.SetVersionTemplate("{{printf \"%s\" .Version}}\n") +}