github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/spf13/cobra"
     8  	"github.com/spf13/viper"
     9  )
    10  
    11  /*
    12     Creation Time: 2020 - Nov - 11
    13     Created by:  (ehsan)
    14     Maintainers:
    15        1.  Ehsan N. Moosa (E2)
    16     Auditor: Ehsan N. Moosa (E2)
    17     Copyright Ronak Software Group 2020
    18  */
    19  
    20  type Config struct {
    21  	v *viper.Viper
    22  }
    23  
    24  var (
    25  	globalConfig *Config
    26  )
    27  
    28  func init() {
    29  	globalConfig = New()
    30  }
    31  
    32  func Global() *Config {
    33  	return globalConfig
    34  }
    35  
    36  func New() *Config {
    37  	return &Config{
    38  		v: viper.NewWithOptions(
    39  			viper.EnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")),
    40  		),
    41  	}
    42  }
    43  
    44  func (c *Config) ReadFile(configFileName string, configSearchPaths ...string) error {
    45  	for _, p := range configSearchPaths {
    46  		c.v.AddConfigPath(p)
    47  	}
    48  	if configFileName == "" {
    49  		configFileName = "config"
    50  	}
    51  
    52  	c.v.SetConfigName(configFileName)
    53  
    54  	return c.v.ReadInConfig()
    55  }
    56  
    57  func ReadFile(configFileName string, configSearchPaths ...string) error {
    58  	return globalConfig.ReadFile(configFileName, configSearchPaths...)
    59  }
    60  
    61  func (c *Config) AllSettings() map[string]interface{} {
    62  	return c.v.AllSettings()
    63  }
    64  
    65  func AllSettings() map[string]interface{} {
    66  	return globalConfig.AllSettings()
    67  }
    68  
    69  // SetEnvPrefix defines a prefix that ENVIRONMENT variables will use.
    70  // // E.g. if your prefix is "spf", the env registry will look for env
    71  // // variables that start with "SPF_".
    72  func (c *Config) SetEnvPrefix(prefix string) {
    73  	c.v.SetEnvPrefix(prefix)
    74  	c.v.AutomaticEnv()
    75  }
    76  func SetEnvPrefix(prefix string) {
    77  	globalConfig.SetEnvPrefix(prefix)
    78  }
    79  
    80  // BindCmdFlags binds a full flag set to the configuration, using each flag's long
    81  // name as the config key.
    82  func (c *Config) BindCmdFlags(cmd *cobra.Command) error {
    83  	return c.v.BindPFlags(cmd.Flags())
    84  }
    85  func BindCmdFlags(cmd *cobra.Command) error {
    86  	return globalConfig.BindCmdFlags(cmd)
    87  }
    88  
    89  // SetCmdFlags sets the flags defined by FlagOption to the command 'cmd'.
    90  func SetCmdFlags(cmd *cobra.Command, opts ...FlagOption) {
    91  	for _, fo := range opts {
    92  		fo(cmd.Flags())
    93  	}
    94  }
    95  
    96  // SetCmdPersistentFlags sets the persistent flags defined by FlagOption to the command 'cmd'.
    97  func SetCmdPersistentFlags(cmd *cobra.Command, opts ...FlagOption) {
    98  	for _, fo := range opts {
    99  		fo(cmd.PersistentFlags())
   100  	}
   101  }
   102  
   103  // Set sets the value for the key in the override register.
   104  // Set is case-insensitive for a key.
   105  // Will be used instead of values obtained via
   106  // flags, config file, ENV, default, or key/value store.
   107  func (c *Config) Set(key string, val interface{}) {
   108  	c.v.Set(key, val)
   109  }
   110  
   111  func Set(key string, val interface{}) {
   112  	globalConfig.Set(key, val)
   113  }
   114  
   115  func (c *Config) GetBool(key string) bool {
   116  	return c.v.GetBool(key)
   117  }
   118  func GetBool(key string) bool {
   119  	return globalConfig.GetBool(key)
   120  }
   121  
   122  func (c *Config) GetString(key string) string {
   123  	return c.v.GetString(key)
   124  }
   125  func GetString(key string) string {
   126  	return globalConfig.GetString(key)
   127  }
   128  
   129  func (c *Config) GetInt64(key string) int64 {
   130  	return c.v.GetInt64(key)
   131  }
   132  func GetInt64(key string) int64 {
   133  	return globalConfig.GetInt64(key)
   134  }
   135  
   136  func (c *Config) GetUint64(key string) uint64 {
   137  	return c.v.GetUint64(key)
   138  }
   139  func GetUint64(key string) uint64 {
   140  	return globalConfig.GetUint64(key)
   141  }
   142  
   143  func (c *Config) GetInt32(key string) int32 {
   144  	return c.v.GetInt32(key)
   145  }
   146  func GetInt32(key string) int32 {
   147  	return globalConfig.GetInt32(key)
   148  }
   149  
   150  func (c *Config) GetUint32(key string) uint32 {
   151  	return c.v.GetUint32(key)
   152  }
   153  func GetUint32(key string) uint32 {
   154  	return globalConfig.GetUint32(key)
   155  }
   156  
   157  func (c *Config) GetInt(key string) int {
   158  	return c.v.GetInt(key)
   159  }
   160  func GetInt(key string) int {
   161  	return globalConfig.GetInt(key)
   162  }
   163  
   164  func (c *Config) GetUint(key string) uint {
   165  	return c.v.GetUint(key)
   166  }
   167  func GetUint(key string) uint {
   168  	return globalConfig.GetUint(key)
   169  }
   170  
   171  func (c *Config) GetIntSlice(key string) []int {
   172  	return c.v.GetIntSlice(key)
   173  }
   174  func GetIntSlice(key string) []int {
   175  	return globalConfig.GetIntSlice(key)
   176  }
   177  
   178  func (c *Config) GetInt32Slice(key string) []int32 {
   179  	ints := c.v.GetIntSlice(key)
   180  	int32s := make([]int32, 0, len(ints))
   181  	for _, i := range ints {
   182  		int32s = append(int32s, int32(i))
   183  	}
   184  
   185  	return int32s
   186  }
   187  func GetInt32Slice(key string) []int32 {
   188  	return globalConfig.GetInt32Slice(key)
   189  }
   190  
   191  func (c *Config) GetInt64Slice(key string) []int64 {
   192  	ints := c.v.GetIntSlice(key)
   193  	int64s := make([]int64, 0, len(ints))
   194  	for _, i := range ints {
   195  		int64s = append(int64s, int64(i))
   196  	}
   197  
   198  	return int64s
   199  }
   200  func GetInt64Slice(key string) []int64 {
   201  	return globalConfig.GetInt64Slice(key)
   202  }
   203  
   204  func (c *Config) GetStringSlice(key string) []string {
   205  	return c.v.GetStringSlice(key)
   206  }
   207  func GetStringSlice(key string) []string {
   208  	return globalConfig.GetStringSlice(key)
   209  }
   210  
   211  func (c *Config) GetDuration(key string) time.Duration {
   212  	return c.v.GetDuration(key)
   213  }
   214  func GetDuration(key string) time.Duration {
   215  	return globalConfig.GetDuration(key)
   216  }
   217  
   218  func (c *Config) GetTime(key string) time.Time {
   219  	return c.v.GetTime(key)
   220  }
   221  func GetTime(key string) time.Time {
   222  	return globalConfig.GetTime(key)
   223  }