github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/config/remote/config.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package remote
    19  
    20  import (
    21  	"fmt"
    22  	"math/big"
    23  	"strings"
    24  
    25  	"github.com/mysteriumnetwork/node/config"
    26  
    27  	"github.com/rs/zerolog/log"
    28  	"github.com/spf13/cast"
    29  	"github.com/urfave/cli/v2"
    30  )
    31  
    32  // Config - remote config struct
    33  type Config struct {
    34  	client Fetcher
    35  	config map[string]interface{}
    36  }
    37  
    38  // Fetcher interface represents anything that
    39  // is able to fetch a config.
    40  type Fetcher interface {
    41  	FetchConfig() (map[string]interface{}, error)
    42  }
    43  
    44  // NewConfig - new remote config instance
    45  func NewConfig(client Fetcher) (*Config, error) {
    46  	cfg := &Config{
    47  		client: client,
    48  	}
    49  	return cfg, cfg.RefreshRemoteConfig()
    50  }
    51  
    52  // RefreshRemoteConfig - will fetch latest config
    53  func (rc *Config) RefreshRemoteConfig() error {
    54  	config, err := rc.client.FetchConfig()
    55  	if err != nil {
    56  		return err
    57  	}
    58  	rc.config = config
    59  	return nil
    60  }
    61  
    62  // Get returns stored remote config value as-is.
    63  func (rc *Config) Get(key string) interface{} {
    64  	segments := strings.Split(strings.ToLower(key), ".")
    65  	value := config.SearchMap(rc.config, segments)
    66  	log.Debug().Msgf("Returning remote config value %v:%v", key, value)
    67  	return value
    68  }
    69  
    70  // GetStringByFlag shorthand for getting current configuration value for cli.BoolFlag.
    71  func (rc *Config) GetStringByFlag(flag cli.StringFlag) string {
    72  	return rc.GetString(flag.Name)
    73  }
    74  
    75  // GetString returns config value as string.
    76  func (rc *Config) GetString(key string) string {
    77  	return cast.ToString(rc.Get(key))
    78  }
    79  
    80  // GetBoolByFlag shorthand for getting current configuration value for cli.BoolFlag.
    81  func (rc *Config) GetBoolByFlag(flag cli.BoolFlag) bool {
    82  	return rc.GetBool(flag.Name)
    83  }
    84  
    85  // GetBool returns config value as bool.
    86  func (rc *Config) GetBool(key string) bool {
    87  	return cast.ToBool(rc.Get(key))
    88  }
    89  
    90  // GetBigIntByFlag shorthand for getting and parsing a configuration value for cli.StringFlag that's a big.Int.
    91  func (rc *Config) GetBigIntByFlag(flag cli.StringFlag) *big.Int {
    92  	return rc.GetBigInt(flag.Name)
    93  }
    94  
    95  // GetBigInt returns config value as big.Int.
    96  func (rc *Config) GetBigInt(key string) *big.Int {
    97  	b, _ := new(big.Int).SetString(rc.GetString(key), 10)
    98  	return b
    99  }
   100  
   101  // GetStringSliceByFlag shorthand for getting and parsing a configuration value for cli.StringFlag that's a []string.
   102  func (rc *Config) GetStringSliceByFlag(flag cli.StringSliceFlag) []string {
   103  	return rc.GetStringSlice(flag.Name)
   104  }
   105  
   106  // GetStringSlice returns config value as []string.
   107  func (rc *Config) GetStringSlice(key string) []string {
   108  	return cast.ToStringSlice(rc.Get(key))
   109  }
   110  
   111  // GetInt64ByFlag shorthand for getting and parsing a configuration value for cli.StringFlag that's a int64.
   112  func (rc *Config) GetInt64ByFlag(flag cli.Int64Flag) int64 {
   113  	return rc.GetInt64(flag.Name)
   114  }
   115  
   116  // GetInt64 returns config value as int64.
   117  func (rc *Config) GetInt64(key string) int64 {
   118  	return cast.ToInt64(rc.Get(key))
   119  }
   120  
   121  // GetHermesID returns the current hermes id.
   122  func (rc *Config) GetHermesID() (string, error) {
   123  	chid := rc.GetInt64ByFlag(config.FlagChainID)
   124  	if chid == rc.GetInt64ByFlag(config.FlagChain1ChainID) {
   125  		return rc.GetStringByFlag(config.FlagChain1HermesAddress), nil
   126  	}
   127  
   128  	if chid == rc.GetInt64ByFlag(config.FlagChain2ChainID) {
   129  		return rc.GetStringByFlag(config.FlagChain2HermesAddress), nil
   130  	}
   131  
   132  	return "", fmt.Errorf("no hermes specified for chain %v", chid)
   133  }