Categorygithub.com/go-srv/configreader
modulepackage
0.0.0-20210824071840-8a2cd5a4d463
Repository: https://github.com/go-srv/configreader.git
Documentation: pkg.go.dev

# 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

# Functions

AddConfigPath wraps the global ConfigReader instance.
AllowMerge wraps the global ConfigReader instance.
Debug print viper values.
DumpConfig wraps the global ConfigReader instance.
LoadConfig wraps the global ConfigReader instance.
LoadDefault loads the default value if it have default annotation It's just a suger function that happens ConfigReader could load default.
LoadFromFile loads configs by parsing the filepath and will load configs with several suffix.
New creates a ConfigReader instance.
No description provided by the author
ReadConfig wraps the global ConfigReader instance.
ReadFromFile wraps the global ConfigReader instance.
Reset the global ConfigReader instance, in the purpose for test.
SetConfigFile sets the config filename with path together.
SetConfigName wraps the global ConfigReader instance.
SetConfigPaths wraps the global ConfigReader instance.
SetEnvName set the env name to be search when merge configs.
SetEnvPrefix wraps the global ConfigReader instance.
SetFlagSet set the flagset to lookup.
SetFs wraps the global ConfigReader instance.

# Variables

ErrNotStruct is returned when value passed to LoadConfig/ReadConfig is not a struct.
ErrNotStructPointer is returned when value passed to LoadConfig/ReadConfig is not a pointer to a struct.
No description provided by the author

# Structs

ConfigReader wraps spf13/viper to read configs.

# Type aliases

FieldProcessor process one of the struct value, value means it is not a sub-struct.