README
¶
ConfigReader
Introduction
Configreader is a package wraps spf13/viper to read, merge config files, environments values, application flags, and default values defined in the tags of the struct.
Usage
Tags Of Struct
- key defines the name of the field in a config file
- default defines the default of the field, if there is no value provided in the file/env/flags, the default value will be used
- flag defines the flag name for the field
- env defines the environment variable name for the field, a default env previs
APP_
will be added - required defines if the field is required, if the field is required, and there is no value provided, an error will occured
- validation defines simple methods to validate the value of the field.
TODO: explain the tag details here
Usages
import "github.com/go-srv/configreader"
type Config struct {
Host string `key:"host" flag:"host" env:"host" required:"true" validation:"range:[8:255]"`
Port int `key:"port" required:"true" flag:"port" env:"port" validation:"range:[80:65535]"`
LogLevel string `key:"loglevel" default:"info" validation:"in:[error, warning, info, verbose]"`
}
func main() {
c := Config{}
configreader.LoadFromFile("/path/to/config/file.ext", &c)
}
More usages please refer to the test file.
TODO: add more usages here
Documentation
¶
Index ¶
- Variables
- func AddConfigPath(path string)
- func AllowMerge(allow bool)
- func Debug()
- func DumpConfig(filename string, confPtr interface{}) error
- func LoadConfig(confPtr interface{}) error
- func LoadDefault(structPtr interface{}) error
- func LoadFromFile(filePath string, confPtr interface{}) error
- func PrintConfig(structPtr interface{})
- func ReadConfig(in io.Reader, confType string, confPtr interface{}) error
- func ReadFromFile(filePath string, confPtr interface{}) error
- func Reset()
- func SetConfigFile(filePath string)
- func SetConfigName(configName string)
- func SetConfigPaths(paths []string)
- func SetEnvName(env string)
- func SetEnvPrefix(in string)
- func SetFlagSet(flag *pflag.FlagSet)
- func SetFs(fs afero.Fs)
- type ConfigReader
- func (c *ConfigReader) AddConfigPath(path string)
- func (c *ConfigReader) AllowMerge(allow bool)
- func (c *ConfigReader) Debug()
- func (c *ConfigReader) DumpConfig(filename string, confPtr interface{}) error
- func (c *ConfigReader) LoadConfig(confPtr interface{}) error
- func (c *ConfigReader) LoadFromFile(filePath string, confPtr interface{}) error
- func (c *ConfigReader) ReadConfig(in io.Reader, confType string, confPtr interface{}) error
- func (c *ConfigReader) ReadFromFile(filename string, confPtr interface{}) error
- func (c *ConfigReader) SetConfigFile(filePath string)
- func (c *ConfigReader) SetConfigName(configName string)
- func (c *ConfigReader) SetConfigPaths(paths []string)
- func (c *ConfigReader) SetEnvName(env string)
- func (c *ConfigReader) SetEnvPrefix(in string)
- func (c *ConfigReader) SetFlagSet(flag *pflag.FlagSet)
- func (c *ConfigReader) SetFs(fs afero.Fs)
- type FieldProcessor
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotStruct is returned when value passed to LoadConfig/ReadConfig is not a struct. ErrNotStruct = xerrors.New("value does not appear to be a struct") // ErrNotStructPointer is returned when value passed to LoadConfig/ReadConfig is not a pointer to a struct. ErrNotStructPointer = xerrors.New("value passed was not a struct pointer") )
var SupportedExts = []string{"json", "yaml", "yml"}
Functions ¶
func AddConfigPath ¶
func AddConfigPath(path string)
AddConfigPath wraps the global ConfigReader instance
func DumpConfig ¶
DumpConfig wraps the global ConfigReader instance
func LoadConfig ¶
func LoadConfig(confPtr interface{}) error
LoadConfig wraps the global ConfigReader instance
func LoadDefault ¶
func LoadDefault(structPtr interface{}) error
LoadDefault loads the default value if it have default annotation It's just a suger function that happens ConfigReader could load default
func LoadFromFile ¶
LoadFromFile loads configs by parsing the filepath and will load configs with several suffix
func PrintConfig ¶
func PrintConfig(structPtr interface{})
func ReadConfig ¶
ReadConfig wraps the global ConfigReader instance
func ReadFromFile ¶
ReadFromFile wraps the global ConfigReader instance
func SetConfigFile ¶
func SetConfigFile(filePath string)
SetConfigFile sets the config filename with path together
func SetConfigName ¶
func SetConfigName(configName string)
SetConfigName wraps the global ConfigReader instance
func SetConfigPaths ¶
func SetConfigPaths(paths []string)
SetConfigPaths wraps the global ConfigReader instance
func SetEnvName ¶
func SetEnvName(env string)
SetEnvName set the env name to be search when merge configs
func SetEnvPrefix ¶
func SetEnvPrefix(in string)
SetEnvPrefix wraps the global ConfigReader instance
Types ¶
type ConfigReader ¶
type ConfigReader struct {
// contains filtered or unexported fields
}
ConfigReader wraps spf13/viper to read configs
func (*ConfigReader) AddConfigPath ¶
func (c *ConfigReader) AddConfigPath(path string)
AddConfigPath adds one path to search config files
func (*ConfigReader) AllowMerge ¶
func (c *ConfigReader) AllowMerge(allow bool)
AllowMerge allow ConfigReader to read env and local configs to override base configs
func (*ConfigReader) DumpConfig ¶
func (c *ConfigReader) DumpConfig(filename string, confPtr interface{}) error
DumpConfig dumps the merged config to filepath
func (*ConfigReader) LoadConfig ¶
func (c *ConfigReader) LoadConfig(confPtr interface{}) error
LoadConfig loads configs from file and set values into the confPtr
func (*ConfigReader) LoadFromFile ¶
func (c *ConfigReader) LoadFromFile(filePath string, confPtr interface{}) error
LoadFromFile loads configs by parsing the filepath and will load configs with several suffix
func (*ConfigReader) ReadConfig ¶
func (c *ConfigReader) ReadConfig(in io.Reader, confType string, confPtr interface{}) error
ReadConfig read configs from io.Reader and set values into confPtr
func (*ConfigReader) ReadFromFile ¶
func (c *ConfigReader) ReadFromFile(filename string, confPtr interface{}) error
ReadFromFile read configs from the special file and set values into confPtr
func (*ConfigReader) SetConfigFile ¶
func (c *ConfigReader) SetConfigFile(filePath string)
SetConfigFile sets the config filename with path together
func (*ConfigReader) SetConfigName ¶
func (c *ConfigReader) SetConfigName(configName string)
SetConfigName sets the config file name (exclude type suffix) to search for
func (*ConfigReader) SetConfigPaths ¶
func (c *ConfigReader) SetConfigPaths(paths []string)
SetConfigPaths set a list of paths to search config files
func (*ConfigReader) SetEnvName ¶
func (c *ConfigReader) SetEnvName(env string)
SetEnvName set the env name to be search when merge configs
func (*ConfigReader) SetEnvPrefix ¶
func (c *ConfigReader) SetEnvPrefix(in string)
SetEnvPrefix sets the prefix of environment e.g. key is 'addr', prefix set to 'app', then env value is 'APP_ADDR'
func (*ConfigReader) SetFlagSet ¶
func (c *ConfigReader) SetFlagSet(flag *pflag.FlagSet)
SetFlagSet set the flagset to lookup
func (*ConfigReader) SetFs ¶
func (c *ConfigReader) SetFs(fs afero.Fs)
SetFs set the filesystem to read config files from
type FieldProcessor ¶
type FieldProcessor func(fullFieldKey string, structField reflect.StructField, structRef reflect.Value) error
FieldProcessor process one of the struct value, value means it is not a sub-struct