From 7cabca2ade43baa20ff3ae17f11e5cf8042b96b6b05efc661719666e41b42c8c Mon Sep 17 00:00:00 2001 From: Max Jonas Werner Date: Thu, 9 Apr 2026 09:25:10 +0200 Subject: [PATCH] Add version command --- cmd/cmd.go | 3 ++ cmd/version/version.go | 88 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 cmd/version/version.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 3252303..35b39fb 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -6,6 +6,7 @@ import ( "os" "time" + "gitea.e13.dev/e13/kubectl-unready/cmd/version" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -35,6 +36,8 @@ func NewUnreadyCmd(ctx context.Context) *cobra.Command { cmd.Flags().BoolVarP(&allNamespacesFlag, "all-namespaces", "A", false, "If present, list unready pods across all namespaces.") + cmd.AddCommand(version.NewVersionCmd(ctx)) + return cmd } diff --git a/cmd/version/version.go b/cmd/version/version.go new file mode 100644 index 0000000..dc6ce0b --- /dev/null +++ b/cmd/version/version.go @@ -0,0 +1,88 @@ +package version + +import ( + "context" + "encoding/json/v2" + "fmt" + "os" + "runtime" + "runtime/debug" + "strconv" + + "github.com/spf13/cobra" +) + +func NewVersionCmd(ctx context.Context) *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "print the plugin's version", + RunE: func(cmd *cobra.Command, args []string) error { + if err := json.MarshalWrite(os.Stdout, Get()); err != nil { + return err + } + fmt.Println() + return nil + }, + } +} + +// GitTreeState represents the state of the Git repository's working tree +// at the time of compilation. It provides a type-safe way to indicate whether +// the source tree was clean, modified, or in an unknown state. +type GitTreeState string + +func (g GitTreeState) String() string { + return string(g) +} + +// These constants define all possible values for the GitTreeState contained in Info. +const ( + GitTreeStateModified GitTreeState = "modified" + GitTreeStateClean GitTreeState = "clean" + GitTreeStateUnknown GitTreeState = "unknown" +) + +// Info carries the information gathered and returned by GetVersion(). +type Info struct { + Version string + GitCommit string + GitCommitTime string + GitTreeState GitTreeState + GoVersion string +} + +// Get returns the version of the application derived directly from the runtime's debug information. +func Get() Info { + var res Info + bi, ok := debug.ReadBuildInfo() + if !ok { + res.Version = "unknown" + } + + res.Version = bi.Main.Version + for _, bs := range bi.Settings { + switch bs.Key { + case "vcs.revision": + res.GitCommit = bs.Value + case "vcs.modified": + mod, err := strconv.ParseBool(bs.Value) + if err != nil { + res.GitTreeState = GitTreeStateUnknown + continue + } + if mod { + res.GitTreeState = GitTreeStateModified + continue + } + res.GitTreeState = GitTreeStateClean + case "vcs.time": + res.GitCommitTime = bs.Value + default: + // we don't care about other settings for now + } + } + + res.GoVersion = runtime.Version() + + return res +}