From 2249086507d1275bc6b3cf124f5ff59ad8edb29a Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Fri, 7 May 2021 13:05:26 +0300 Subject: [PATCH] feat: add more logs, add configuration via config --- main.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 616f2e8..9e9b746 100644 --- a/main.go +++ b/main.go @@ -41,9 +41,12 @@ var rootCmd = &cobra.Command{ Long: "Tool to monitor missed blocks for Cosmos-chain validators", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if ConfigPath == "" { + log.Trace().Msg("Config file not provided") return nil } + log.Trace().Msg("Config file provided") + viper.SetConfigFile(ConfigPath) if err := viper.ReadInConfig(); err != nil { log.Info().Err(err).Msg("Error reading config file") @@ -62,11 +65,51 @@ var rootCmd = &cobra.Command{ } }) + setBechPrefixes(cmd) + return nil }, Run: Execute, } +func setBechPrefixes(cmd *cobra.Command) { + if flag, err := cmd.Flags().GetString("bech-account-prefix"); flag != "" && err == nil { + AccountPrefix = flag + } else { + AccountPrefix = Prefix + } + + if flag, err := cmd.Flags().GetString("bech-account-pubkey-prefix"); flag != "" && err == nil { + AccountPubkeyPrefix = flag + } else { + AccountPubkeyPrefix = Prefix + "pub" + } + + if flag, err := cmd.Flags().GetString("bech-validator-prefix"); flag != "" && err == nil { + ValidatorPrefix = flag + } else { + ValidatorPrefix = Prefix + "valoper" + } + + if flag, err := cmd.Flags().GetString("bech-validator-pubkey-prefix"); flag != "" && err == nil { + ValidatorPubkeyPrefix = flag + } else { + ValidatorPubkeyPrefix = Prefix + "valoperpub" + } + + if flag, err := cmd.Flags().GetString("bech-consensus-node-prefix"); flag != "" && err == nil { + ConsensusNodePrefix = flag + } else { + ConsensusNodePrefix = Prefix + "valcons" + } + + if flag, err := cmd.Flags().GetString("bech-consensus-node-pubkey-prefix"); flag != "" && err == nil { + ConsensusNodePubkeyPrefix = flag + } else { + ConsensusNodePubkeyPrefix = Prefix + "valconspub" + } +} + func Execute(cmd *cobra.Command, args []string) { logLevel, err := zerolog.ParseLevel(LogLevel) if err != nil { @@ -124,6 +167,7 @@ func Execute(cmd *cobra.Command, args []string) { } func main() { + rootCmd.PersistentFlags().StringVar(&ConfigPath, "config", "", "Config file path") rootCmd.PersistentFlags().StringVar(&Denom, "denom", "uxprt", "Cosmos coin denom") rootCmd.PersistentFlags().StringVar(&ListenAddress, "listen-address", ":9300", "The address this exporter would listen on") rootCmd.PersistentFlags().StringVar(&NodeAddress, "node", "localhost:9090", "RPC node address") @@ -132,10 +176,12 @@ func main() { // some networks, like Iris, have the different prefixes for address, validator and consensus node rootCmd.PersistentFlags().StringVar(&Prefix, "bech-prefix", "persistence", "Bech32 global prefix") - rootCmd.PersistentFlags().StringVar(&ValidatorPrefix, "bech-validator-prefix", Prefix+"valoper", "Bech32 validator prefix") - rootCmd.PersistentFlags().StringVar(&ValidatorPubkeyPrefix, "bech-validator-pubkey-prefix", Prefix+"valoperpub", "Bech32 pubkey validator prefix") - rootCmd.PersistentFlags().StringVar(&ConsensusNodePrefix, "bech-consensus-node-prefix", Prefix+"valcons", "Bech32 consensus node prefix") - rootCmd.PersistentFlags().StringVar(&ConsensusNodePubkeyPrefix, "bech-consensus-node-pubkey-prefix", Prefix+"valconspub", "Bech32 pubkey consensus node prefix") + rootCmd.PersistentFlags().StringVar(&AccountPrefix, "bech-account-prefix", "", "Bech32 account prefix") + rootCmd.PersistentFlags().StringVar(&AccountPubkeyPrefix, "bech-account-pubkey-prefix", "", "Bech32 pubkey account prefix") + rootCmd.PersistentFlags().StringVar(&ValidatorPrefix, "bech-validator-prefix", "", "Bech32 validator prefix") + rootCmd.PersistentFlags().StringVar(&ValidatorPubkeyPrefix, "bech-validator-pubkey-prefix", "", "Bech32 pubkey validator prefix") + rootCmd.PersistentFlags().StringVar(&ConsensusNodePrefix, "bech-consensus-node-prefix", "", "Bech32 consensus node prefix") + rootCmd.PersistentFlags().StringVar(&ConsensusNodePubkeyPrefix, "bech-consensus-node-pubkey-prefix", "", "Bech32 pubkey consensus node prefix") if err := rootCmd.Execute(); err != nil { log.Fatal().Err(err).Msg("Could not start application")