From 8f550464b8be9abf6c7d292217640f366444cc81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Br=C3=BCderl?= Date: Sun, 30 Sep 2018 22:40:00 +0200 Subject: [PATCH] use cobra.OnInitialize to ensure that pflags are parsed --- cmd/kaf/kaf.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/kaf/kaf.go b/cmd/kaf/kaf.go index 090c32e4..ad18a983 100644 --- a/cmd/kaf/kaf.go +++ b/cmd/kaf/kaf.go @@ -63,17 +63,23 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kaf/config)") rootCmd.PersistentFlags().StringSliceVarP(&brokersFlag, "brokers", "b", nil, "Comma separated list of broker ip:port pairs") + cobra.OnInitialize(onInit) +} + +func onInit() { config, _ = kaf.ReadConfig() - // Flag has highest priority + // Flag is highest priority override if brokersFlag != nil { currentCluster = &kaf.Cluster{ Brokers: brokersFlag, } } else { + // If no override from flag is set, get current cluster from config file if cluster := config.ActiveCluster(); cluster != nil { currentCluster = cluster } else { + // Default to localhost:9092 with no security currentCluster = &kaf.Cluster{ Brokers: []string{"localhost:9092"}, }