Skip to content

Commit

Permalink
load config values from viper if present in config (#468)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
maximilien authored and knative-prow-robot committed Dec 19, 2019
1 parent 2b518d1 commit b640219
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
7 changes: 3 additions & 4 deletions pkg/kn/commands/plugin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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.")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/testing_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
15 changes: 13 additions & 2 deletions pkg/kn/commands/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
},
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b640219

Please sign in to comment.