github.com/wangyougui/gf/v2@v2.6.5/os/gcfg/gcfg.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  // Package gcfg provides reading, caching and managing for configuration.
     8  package gcfg
     9  
    10  import (
    11  	"context"
    12  
    13  	"github.com/wangyougui/gf/v2/container/gvar"
    14  	"github.com/wangyougui/gf/v2/errors/gcode"
    15  	"github.com/wangyougui/gf/v2/errors/gerror"
    16  	"github.com/wangyougui/gf/v2/internal/command"
    17  	"github.com/wangyougui/gf/v2/internal/intlog"
    18  	"github.com/wangyougui/gf/v2/internal/utils"
    19  	"github.com/wangyougui/gf/v2/os/genv"
    20  )
    21  
    22  // Config is the configuration management object.
    23  type Config struct {
    24  	adapter Adapter
    25  }
    26  
    27  const (
    28  	DefaultInstanceName   = "config" // DefaultName is the default instance name for instance usage.
    29  	DefaultConfigFileName = "config" // DefaultConfigFile is the default configuration file name.
    30  )
    31  
    32  // New creates and returns a Config object with default adapter of AdapterFile.
    33  func New() (*Config, error) {
    34  	adapterFile, err := NewAdapterFile()
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return &Config{
    39  		adapter: adapterFile,
    40  	}, nil
    41  }
    42  
    43  // NewWithAdapter creates and returns a Config object with given adapter.
    44  func NewWithAdapter(adapter Adapter) *Config {
    45  	return &Config{
    46  		adapter: adapter,
    47  	}
    48  }
    49  
    50  // Instance returns an instance of Config with default settings.
    51  // The parameter `name` is the name for the instance. But very note that, if the file "name.toml"
    52  // exists in the configuration directory, it then sets it as the default configuration file. The
    53  // toml file type is the default configuration file type.
    54  func Instance(name ...string) *Config {
    55  	var instanceName = DefaultInstanceName
    56  	if len(name) > 0 && name[0] != "" {
    57  		instanceName = name[0]
    58  	}
    59  	return localInstances.GetOrSetFuncLock(instanceName, func() interface{} {
    60  		adapterFile, err := NewAdapterFile()
    61  		if err != nil {
    62  			intlog.Errorf(context.Background(), `%+v`, err)
    63  			return nil
    64  		}
    65  		if instanceName != DefaultInstanceName {
    66  			adapterFile.SetFileName(instanceName)
    67  		}
    68  		return NewWithAdapter(adapterFile)
    69  	}).(*Config)
    70  }
    71  
    72  // SetAdapter sets the adapter of current Config object.
    73  func (c *Config) SetAdapter(adapter Adapter) {
    74  	c.adapter = adapter
    75  }
    76  
    77  // GetAdapter returns the adapter of current Config object.
    78  func (c *Config) GetAdapter() Adapter {
    79  	return c.adapter
    80  }
    81  
    82  // Available checks and returns the configuration service is available.
    83  // The optional parameter `pattern` specifies certain configuration resource.
    84  //
    85  // It returns true if configuration file is present in default AdapterFile, or else false.
    86  // Note that this function does not return error as it just does simply check for backend configuration service.
    87  func (c *Config) Available(ctx context.Context, resource ...string) (ok bool) {
    88  	return c.adapter.Available(ctx, resource...)
    89  }
    90  
    91  // Get retrieves and returns value by specified `pattern`.
    92  // It returns all values of current Json object if `pattern` is given empty or string ".".
    93  // It returns nil if no value found by `pattern`.
    94  //
    95  // It returns a default value specified by `def` if value for `pattern` is not found.
    96  func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
    97  	var (
    98  		err   error
    99  		value interface{}
   100  	)
   101  	value, err = c.adapter.Get(ctx, pattern)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	if value == nil {
   106  		if len(def) > 0 {
   107  			return gvar.New(def[0]), nil
   108  		}
   109  		return nil, nil
   110  	}
   111  	return gvar.New(value), nil
   112  }
   113  
   114  // GetWithEnv returns the configuration value specified by pattern `pattern`.
   115  // If the configuration value does not exist, then it retrieves and returns the environment value specified by `key`.
   116  // It returns the default value `def` if none of them exists.
   117  //
   118  // Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE.
   119  func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
   120  	value, err := c.Get(ctx, pattern)
   121  	if err != nil && gerror.Code(err) != gcode.CodeNotFound {
   122  		return nil, err
   123  	}
   124  	if value == nil {
   125  		if v := genv.Get(utils.FormatEnvKey(pattern)); v != nil {
   126  			return v, nil
   127  		}
   128  		if len(def) > 0 {
   129  			return gvar.New(def[0]), nil
   130  		}
   131  		return nil, nil
   132  	}
   133  	return value, nil
   134  }
   135  
   136  // GetWithCmd returns the configuration value specified by pattern `pattern`.
   137  // If the configuration value does not exist, then it retrieves and returns the command line option specified by `key`.
   138  // It returns the default value `def` if none of them exists.
   139  //
   140  // Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable.
   141  func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) {
   142  	value, err := c.Get(ctx, pattern)
   143  	if err != nil && gerror.Code(err) != gcode.CodeNotFound {
   144  		return nil, err
   145  	}
   146  	if value == nil {
   147  		if v := command.GetOpt(utils.FormatCmdKey(pattern)); v != "" {
   148  			return gvar.New(v), nil
   149  		}
   150  		if len(def) > 0 {
   151  			return gvar.New(def[0]), nil
   152  		}
   153  		return nil, nil
   154  	}
   155  	return value, nil
   156  }
   157  
   158  // Data retrieves and returns all configuration data as map type.
   159  func (c *Config) Data(ctx context.Context) (data map[string]interface{}, err error) {
   160  	return c.adapter.Data(ctx)
   161  }
   162  
   163  // MustGet acts as function Get, but it panics if error occurs.
   164  func (c *Config) MustGet(ctx context.Context, pattern string, def ...interface{}) *gvar.Var {
   165  	v, err := c.Get(ctx, pattern, def...)
   166  	if err != nil {
   167  		panic(err)
   168  	}
   169  	if v == nil {
   170  		return nil
   171  	}
   172  	return v
   173  }
   174  
   175  // MustGetWithEnv acts as function GetWithEnv, but it panics if error occurs.
   176  func (c *Config) MustGetWithEnv(ctx context.Context, pattern string, def ...interface{}) *gvar.Var {
   177  	v, err := c.GetWithEnv(ctx, pattern, def...)
   178  	if err != nil {
   179  		panic(err)
   180  	}
   181  	return v
   182  }
   183  
   184  // MustGetWithCmd acts as function GetWithCmd, but it panics if error occurs.
   185  func (c *Config) MustGetWithCmd(ctx context.Context, pattern string, def ...interface{}) *gvar.Var {
   186  	v, err := c.GetWithCmd(ctx, pattern, def...)
   187  	if err != nil {
   188  		panic(err)
   189  	}
   190  	return v
   191  }
   192  
   193  // MustData acts as function Data, but it panics if error occurs.
   194  func (c *Config) MustData(ctx context.Context) map[string]interface{} {
   195  	v, err := c.Data(ctx)
   196  	if err != nil {
   197  		panic(err)
   198  	}
   199  	return v
   200  }