Implement options for the minikube version command

Add --short and --output options, just like kubectl
pull/7325/head
Anders F Björklund 2020-03-30 22:16:10 +02:00
parent cc731bb010
commit a1cc3188bb
1 changed files with 38 additions and 3 deletions

View File

@ -17,20 +17,55 @@ limitations under the License.
package cmd package cmd
import ( import (
"encoding/json"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/version" "k8s.io/minikube/pkg/version"
) )
var (
versionOutput string
shortVersion bool
)
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
Short: "Print the version of minikube", Short: "Print the version of minikube",
Long: `Print the version of minikube.`, Long: `Print the version of minikube.`,
Run: func(command *cobra.Command, args []string) { Run: func(command *cobra.Command, args []string) {
out.Ln("minikube version: %v", version.GetVersion()) minikubeVersion := version.GetVersion()
gitCommitID := version.GetGitCommitID() gitCommitID := version.GetGitCommitID()
if gitCommitID != "" { data := map[string]string{
out.Ln("commit: %v", gitCommitID) "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.")
}