From b640219140f55d037187b485747dfb8c091511f3 Mon Sep 17 00:00:00 2001 From: "dr.max" Date: Thu, 19 Dec 2019 02:24:31 -0800 Subject: [PATCH] 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 --- pkg/kn/commands/plugin/list.go | 7 +++---- pkg/kn/commands/plugin/plugin.go | 2 +- pkg/kn/commands/testing_helper.go | 2 +- pkg/kn/commands/types.go | 15 +++++++++++++-- pkg/kn/core/root.go | 12 ++++++++++++ 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/pkg/kn/commands/plugin/list.go b/pkg/kn/commands/plugin/list.go index 9bbbfdb3e..a317c9a8a 100644 --- a/pkg/kn/commands/plugin/list.go +++ b/pkg/kn/commands/plugin/list.go @@ -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.") } diff --git a/pkg/kn/commands/plugin/plugin.go b/pkg/kn/commands/plugin/plugin.go index d6beaac4d..580dca4ac 100644 --- a/pkg/kn/commands/plugin/plugin.go +++ b/pkg/kn/commands/plugin/plugin.go @@ -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 diff --git a/pkg/kn/commands/testing_helper.go b/pkg/kn/commands/testing_helper.go index 528e3a007..665a309cc 100644 --- a/pkg/kn/commands/testing_helper.go +++ b/pkg/kn/commands/testing_helper.go @@ -179,7 +179,7 @@ Eventing: Manage event subscriptions and channels. Connect up event sources.`, rootCmd.PersistentFlags().StringVar(¶ms.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")) diff --git a/pkg/kn/commands/types.go b/pkg/kn/commands/types.go index 1729ad6a3..b846cc3f9 100644 --- a/pkg/kn/commands/types.go +++ b/pkg/kn/commands/types.go @@ -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 +} diff --git a/pkg/kn/core/root.go b/pkg/kn/core/root.go index cec1ec3b9..82e924751 100644 --- a/pkg/kn/core/root.go +++ b/pkg/kn/core/root.go @@ -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