From a1cc3188bb6e0598de68c72f61f6a0fc9e1d2922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Mon, 30 Mar 2020 22:16:10 +0200 Subject: [PATCH 1/2] Implement options for the minikube version command Add --short and --output options, just like kubectl --- cmd/minikube/cmd/version.go | 41 ++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/cmd/minikube/cmd/version.go b/cmd/minikube/cmd/version.go index 00c61efd888a..c56ca7cdea8f 100644 --- a/cmd/minikube/cmd/version.go +++ b/cmd/minikube/cmd/version.go @@ -17,20 +17,55 @@ limitations under the License. package cmd import ( + "encoding/json" "github.com/spf13/cobra" + "gopkg.in/yaml.v2" + "k8s.io/minikube/pkg/minikube/exit" "k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/version" ) +var ( + versionOutput string + shortVersion bool +) + var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version of minikube", Long: `Print the version of minikube.`, Run: func(command *cobra.Command, args []string) { - out.Ln("minikube version: %v", version.GetVersion()) + minikubeVersion := version.GetVersion() gitCommitID := version.GetGitCommitID() - if gitCommitID != "" { - out.Ln("commit: %v", gitCommitID) + data := map[string]string{ + "minikubeVersion": minikubeVersion, + "commit": gitCommitID, + } + switch versionOutput { + case "": + out.Ln("minikube version: %v", minikubeVersion) + if !shortVersion && gitCommitID != "" { + out.Ln("commit: %v", gitCommitID) + } + case "json": + json, err := json.Marshal(data) + if err != nil { + exit.WithError("version json failure", err) + } + out.Ln(string(json)) + case "yaml": + yaml, err := yaml.Marshal(data) + if err != nil { + exit.WithError("version yaml failure", err) + } + out.Ln(string(yaml)) + default: + exit.WithCodeT(exit.BadUsage, "error: --output must be 'yaml' or 'json'") } }, } + +func init() { + versionCmd.Flags().StringVarP(&versionOutput, "output", "o", "", "One of 'yaml' or 'json'.") + versionCmd.Flags().BoolVar(&shortVersion, "short", false, "Print just the version number.") +} From 28ec78a5cb0dc34258ec67bb03d909aeae9656a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Str=C3=B6mberg?= Date: Wed, 1 Apr 2020 05:54:23 -0700 Subject: [PATCH 2/2] lint - add newline after stdlib imports --- cmd/minikube/cmd/version.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/minikube/cmd/version.go b/cmd/minikube/cmd/version.go index c56ca7cdea8f..478a7aab920b 100644 --- a/cmd/minikube/cmd/version.go +++ b/cmd/minikube/cmd/version.go @@ -18,6 +18,7 @@ package cmd import ( "encoding/json" + "github.com/spf13/cobra" "gopkg.in/yaml.v2" "k8s.io/minikube/pkg/minikube/exit"