load config values from viper if present in config (#468)

* init config flags in root cmd's PersistentPreRunE

* change Cfg.LookupPlugins to pointer to bool

* simplified logic in setting initial config flags from viper config
This commit is contained in:
dr.max 2019-12-19 02:24:31 -08:00 committed by Knative Prow Robot
parent 2b518d1e4e
commit b640219140
5 changed files with 30 additions and 8 deletions

View File

@ -58,12 +58,11 @@ Available plugins are those that are:
// List plugins by looking up in plugin directory and path
func listPlugins(cmd *cobra.Command, flags pluginListFlags) error {
pluginPath, err := homedir.Expand(commands.Cfg.PluginsDir)
if err != nil {
return err
}
if commands.Cfg.LookupPlugins {
if *commands.Cfg.LookupPlugins {
pluginPath = pluginPath + string(os.PathListSeparator) + os.Getenv("PATH")
}
@ -74,12 +73,12 @@ func listPlugins(cmd *cobra.Command, flags pluginListFlags) error {
if flags.verbose {
fmt.Fprintf(out, "The following plugins are available, using options:\n")
fmt.Fprintf(out, " - plugins dir: '%s'%s\n", commands.Cfg.PluginsDir, extraLabelIfPathNotExists(pluginPath))
fmt.Fprintf(out, " - lookup plugins in $PATH: '%t'\n", commands.Cfg.LookupPlugins)
fmt.Fprintf(out, " - lookup plugins in $PATH: '%t'\n", *commands.Cfg.LookupPlugins)
}
if len(pluginsFound) == 0 {
if flags.verbose {
fmt.Fprintf(out, "No plugins found in path %s.\n", pluginPath)
fmt.Fprintf(out, "No plugins found in path '%s'.\n", pluginPath)
} else {
fmt.Fprintln(out, "No plugins found.")
}

View File

@ -41,7 +41,7 @@ Please refer to the documentation and examples for more information about how wr
// AddPluginFlags plugins-dir and lookup-plugins to cmd
func AddPluginFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&commands.Cfg.PluginsDir, "plugins-dir", "~/.kn/plugins", "kn plugins directory")
cmd.Flags().BoolVar(&commands.Cfg.LookupPlugins, "lookup-plugins", false, "look for kn plugins in $PATH")
cmd.Flags().BoolVar(commands.Cfg.LookupPlugins, "lookup-plugins", false, "look for kn plugins in $PATH")
}
// BindPluginsFlagToViper bind and set default with viper for plugins flags

View File

@ -179,7 +179,7 @@ Eventing: Manage event subscriptions and channels. Connect up event sources.`,
rootCmd.PersistentFlags().StringVar(&params.KubeCfgPath, "kubeconfig", "", "kubectl config file (default is $HOME/.kube/config)")
rootCmd.Flags().StringVar(&Cfg.PluginsDir, "plugins-dir", "~/.kn/plugins", "kn plugins directory")
rootCmd.Flags().BoolVar(&Cfg.LookupPlugins, "lookup-plugins", false, "look for kn plugins in $PATH")
rootCmd.Flags().BoolVar(Cfg.LookupPlugins, "lookup-plugins", false, "look for kn plugins in $PATH")
viper.BindPFlag("plugins-dir", rootCmd.Flags().Lookup("plugins-dir"))
viper.BindPFlag("lookup-plugins", rootCmd.Flags().Lookup("lookup-plugins"))

View File

@ -38,12 +38,15 @@ import (
var CfgFile string
// Cfg is Kn's configuration values
var Cfg Config
var Cfg Config = Config{
PluginsDir: "",
LookupPlugins: newBoolP(false),
}
// Config contains the variables for the Kn config
type Config struct {
PluginsDir string
LookupPlugins bool
LookupPlugins *bool
}
// KnParams for creating commands. Useful for inserting mocks for testing.
@ -169,3 +172,11 @@ func (params *KnParams) GetClientConfig() (clientcmd.ClientConfig, error) {
}
return nil, fmt.Errorf("Config file '%s' can not be found", params.KubeCfgPath)
}
// Private
// Returns a pointer to bool, hard to do better in Golang
func newBoolP(b bool) *bool {
aBool := b
return &aBool
}

View File

@ -122,6 +122,7 @@ func NewKnCommand(params ...commands.KnParams) *cobra.Command {
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
initConfigFlags()
return flags.ReconcileBoolFlags(cmd.Flags())
},
}
@ -217,6 +218,17 @@ func initConfig() {
}
}
func initConfigFlags() {
if viper.IsSet("plugins-dir") {
commands.Cfg.PluginsDir = viper.GetString("plugins-dir")
}
// Always set the Cfg.LookupPlugins from viper value since default is false both ways
var aBool bool
aBool = viper.GetBool("lookup-plugins")
commands.Cfg.LookupPlugins = &aBool
}
func extractKnPluginFlags(args []string) (string, bool, error) {
pluginsDir := "~/.kn/plugins"
lookupPluginsInPath := false