From 5e582b87d6968cfe48904e32612fff135e6670f5 Mon Sep 17 00:00:00 2001 From: Danny Gershman Date: Wed, 17 May 2023 08:41:10 -0400 Subject: [PATCH] Adds json response of version info (#1262) --- cmd/root.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 483f7fd8..e1470bf1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -58,6 +58,11 @@ type RootFlags struct { version bool } +type VersionInfo struct { + K3d string `json:"k3d"` + K3s string `json:"k3s"` +} + var flags = RootFlags{} func NewCmdK3d() *cobra.Command { @@ -207,20 +212,46 @@ func initRuntime() { } func NewCmdVersion() *cobra.Command { + type VersionOutputFormat string + + const ( + VersionOutputFormatJson VersionOutputFormat = "json" + ) + cmd := &cobra.Command{ Use: "version", Short: "Show k3d and default k3s version", Long: "Show k3d and default k3s version", Run: func(cmd *cobra.Command, args []string) { - printVersion() + output, _ := cmd.Flags().GetString("output") + + if output == string(VersionOutputFormatJson) { + printJsonVersion() + } else { + printVersion() + } }, Args: cobra.NoArgs, } + cmd.Flags().StringP("output", "o", "", "This will return version information as a different format. Only json is supported") + cmd.AddCommand(NewCmdVersionLs()) return cmd +} +func printJsonVersion() { + versionInfo := VersionInfo{ + K3d: version.GetVersion(), + K3s: version.K3sVersion, + } + versionJson, err := json.Marshal(versionInfo) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(string(versionJson)) } func printVersion() {