Add defaults subcommand to `minikube config`

`minikube config defaults PROPERTY_NAME` will print out all acceptable default values for that property.

Currently only works for `minikube config defaults driver` which looks like this on Mac:

```
$ minikube config defaults driver
* virtualbox
* parallels
* vmwarefusion
* hyperkit
* vmware
* docker
* podman
```

This will be useful for clients that depend on minikube, like IDE extensions.
pull/8143/head
Priya Wadhwa 2020-05-13 14:15:08 -07:00
parent 677c2e47d8
commit 31525fe822
2 changed files with 86 additions and 9 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/localpath"
) )
@ -32,21 +33,23 @@ type setFn func(string, string) error
// Setting represents a setting // Setting represents a setting
type Setting struct { type Setting struct {
name string name string
set func(config.MinikubeConfig, string, string) error set func(config.MinikubeConfig, string, string) error
setMap func(config.MinikubeConfig, string, map[string]interface{}) error setMap func(config.MinikubeConfig, string, map[string]interface{}) error
validations []setFn validDefaults func() []string
callbacks []setFn validations []setFn
callbacks []setFn
} }
// These are all the settings that are configurable // These are all the settings that are configurable
// and their validation and callback fn run on Set // and their validation and callback fn run on Set
var settings = []Setting{ var settings = []Setting{
{ {
name: "driver", name: "driver",
set: SetString, set: SetString,
validations: []setFn{IsValidDriver}, validDefaults: driver.SupportedDrivers,
callbacks: []setFn{RequiresRestartMsg}, validations: []setFn{IsValidDriver},
callbacks: []setFn{RequiresRestartMsg},
}, },
{ {
name: "vm-driver", name: "vm-driver",

View File

@ -0,0 +1,74 @@
/*
Copyright 2016 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 config
import (
"errors"
"fmt"
"strings"
"github.com/spf13/cobra"
)
var configDefaultsCommand = &cobra.Command{
Use: "defaults PROPERTY_NAME",
Short: "Lists all valid default values for PROPERTY_NAME",
Long: `list displays all valid default settings for PROPERTY_NAME
Acceptable fields: ` + "\n\n" + fieldsWithDefaults(),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
cmd.SilenceErrors = true
return errors.New("not enough arguments.\nusage: minikube config list PROPERTY_NAME")
}
if len(args) > 1 {
cmd.SilenceErrors = true
return fmt.Errorf("too many arguments (%d)\nusage: minikube config list PROPERTY_NAME", len(args))
}
property := args[0]
return listDefaults(property)
},
}
func listDefaults(property string) error {
setting, err := findSetting(property)
if err != nil {
return err
}
if setting.validDefaults == nil {
return fmt.Errorf("%s is not a valid option for the `defaults` command; to see valid options run `minikube config defaults -h`", property)
}
defaults := setting.validDefaults()
for _, d := range defaults {
fmt.Printf("* %s\n", d)
}
return nil
}
func fieldsWithDefaults() string {
fields := []string{}
for _, s := range settings {
if s.validDefaults != nil {
fields = append(fields, " * "+s.name)
}
}
return strings.Join(fields, "\n")
}
func init() {
ConfigCmd.AddCommand(configDefaultsCommand)
}