configreader

package module
v0.0.0-...-8a2cd5a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2021 License: MIT Imports: 19 Imported by: 1

README

ConfigReader

Build codecov

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

Constants

This section is empty.

Variables

View Source
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")
)
View Source
var SupportedExts = []string{"json", "yaml", "yml"}

Functions

func AddConfigPath

func AddConfigPath(path string)

AddConfigPath wraps the global ConfigReader instance

func AllowMerge

func AllowMerge(allow bool)

AllowMerge wraps the global ConfigReader instance

func Debug

func Debug()

Debug print viper values

func DumpConfig

func DumpConfig(filename string, confPtr interface{}) error

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

func LoadFromFile(filePath string, confPtr interface{}) error

LoadFromFile loads configs by parsing the filepath and will load configs with several suffix

func PrintConfig

func PrintConfig(structPtr interface{})

func ReadConfig

func ReadConfig(in io.Reader, confType string, confPtr interface{}) error

ReadConfig wraps the global ConfigReader instance

func ReadFromFile

func ReadFromFile(filePath string, confPtr interface{}) error

ReadFromFile wraps the global ConfigReader instance

func Reset

func Reset()

Reset the global ConfigReader instance, in the purpose for test

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

func SetFlagSet

func SetFlagSet(flag *pflag.FlagSet)

SetFlagSet set the flagset to lookup

func SetFs

func SetFs(fs afero.Fs)

SetFs wraps the global ConfigReader instance

Types

type ConfigReader

type ConfigReader struct {
	// contains filtered or unexported fields
}

ConfigReader wraps spf13/viper to read configs

func New

func New() *ConfigReader

New creates a ConfigReader instance

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) Debug

func (c *ConfigReader) Debug()

Debug print viper values

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL