From 76714aca84b58d4ead972344805107d90df8fd56 Mon Sep 17 00:00:00 2001 From: andylibrian Date: Sat, 21 Dec 2019 11:09:36 +0700 Subject: [PATCH] implemented options command #5036 This is to fix help command showing invalid "minikube options". It happened because minikube reuses kubectl templates which expects options cmd to be there. --- cmd/minikube/cmd/options.go | 68 +++++++++++++++++++++++++++++++++++++ cmd/minikube/cmd/root.go | 3 +- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 cmd/minikube/cmd/options.go diff --git a/cmd/minikube/cmd/options.go b/cmd/minikube/cmd/options.go new file mode 100644 index 0000000000..4b45bf85a8 --- /dev/null +++ b/cmd/minikube/cmd/options.go @@ -0,0 +1,68 @@ +/* +Copyright 2019 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cmd + +import ( + "bytes" + "fmt" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" + + "k8s.io/minikube/pkg/minikube/out" +) + +// optionsCmd represents the options command +var optionsCmd = &cobra.Command{ + Use: "options", + Short: "Show a list of global command-line options (applies to all commands).", + Long: "Show a list of global command-line options (applies to all commands).", + Run: runOptions, +} + +// runOptions handles the executes the flow of "minikube stop" +func runOptions(cmd *cobra.Command, args []string) { + out.String("The following options can be passed to any command:\n\n") + for _, flagName := range viperWhiteList { + f := pflag.Lookup(flagName) + out.String(flagUsage(f)) + } +} + +func flagUsage(flag *pflag.Flag) string { + x := new(bytes.Buffer) + + if flag.Hidden { + return "" + } + + format := "--%s=%s: %s\n" + + if flag.Value.Type() == "string" { + format = "--%s='%s': %s\n" + } + + if len(flag.Shorthand) > 0 { + format = " -%s, " + format + } else { + format = " %s " + format + } + + fmt.Fprintf(x, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage) + + return x.String() +} diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 0b95c7f165..2a1d012b29 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -54,9 +54,9 @@ var dirs = [...]string{ } var viperWhiteList = []string{ - "v", "alsologtostderr", "log_dir", + "v", } // RootCmd represents the base command when called without any subcommands @@ -214,6 +214,7 @@ func init() { logsCmd, updateCheckCmd, versionCmd, + optionsCmd, }, }, }