cfg

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: MIT Imports: 12 Imported by: 3

README

Simple config reading for Go using reflection

Usage

Sample config file:

# A comment
filesize = 10MB
log_level = Debug
log_file = /var/log/my.log

Quotes are not trimmed. Whitespaces before and after the value are trimmed.

Unmarshal config into struct

type Settings struct {
    //ServerPort config value; if key not found in config the default 8080 is used
    ServerPort int        `cfg:"server_port" default:"8080"`
    //Filesize returns bytes of specified filesize 
    Filesize cfg.Filesize `cfg:"filesize"`
    //Log the nested struct
    Log
}

type Log struct {
    //File config key without default value; if not found an error is returned
    File    string   `cfg:"log_file"`	
    //Level type LogLevel unmarshals the value "info" or "debug" into type LogLevel
    Level   LogLevel `cfg:"log_level"`	
}

// LogLevel custom int type which holds the log level
type LogLevel int

//Possible log levels
const (    
    //Info logs everything with severity "Info"
    Info = iota
    //Debug logs everything with severity "Debug"
    Debug
)

// Unmarshal the custom unmarshaler for the log level
func (lm *LogLevel) Unmarshal(value string) error {		
    if strings.ToLower(value) == "info" {
            *lm = LogLevel(Info)
            return nil
    } else if strings.ToLower(value) == "debug" {
            *lm = LogLevel(Debug)
            return nil
    }

    return fmt.Errorf("unexpected config value '%s' for log level", value)
}

func main() {
    // create a new ConfigFiles structure
    c := cfg.ConfigFiles{}
    // search for a config 'myconfig.conf' in folder '/etc' it fails if file is not found.
    c.AddConfig("/etc", myconfig.conf, true)	
    
    settings := new(Settings)		
    // merges the config values into the settings struct
    def, err := c.MergeConfigsInto(settings)	

    if err != nil {
         panic(err)
    }

    for k, v := range def {				// applied defaults
        fmt.Printf("using default value %s for key %s\n", v.Value, k)
    }

    fmt.Println(settings.ServerPort)
    fmt.Println(settings.Filesize)
    fmt.Println(settings.Log.File)
    fmt.Println(settings.Log.Level)
}

Documentation

Index

Constants

View Source
const (
	B = 1 << (iota * 10)
	KB
	MB
	GB
	TB
)

Variables

This section is empty.

Functions

func LoadConfigInto

func LoadConfigInto(file string, dest interface{}) (map[string]Default, error)

LoadConfigInto loads a single config into struct returns the applied default values

Types

type ConfigFiles

type ConfigFiles struct {
	Files []File
}

ConfigFiles represents multiple file containing the config keys and values

func (*ConfigFiles) AddConfig

func (c *ConfigFiles) AddConfig(path, name string, required bool)

AddConfig adds a config file

func (ConfigFiles) MergeConfigsInto

func (c ConfigFiles) MergeConfigsInto(dest interface{}) (map[string]Default, error)

MergeConfigsInto merges multiple configs files into a struct returns the applied default values

type CustomType

type CustomType interface {
	Unmarshal(value string) error
}

CustomType can be implemented to unmarshal in a custom format

type Default

type Default struct {
	Value string
	// contains filtered or unexported fields
}

Default represents a default value for a field

type File

type File struct {
	Name     string
	Path     string
	Required bool
}

File represents a file Required if an error should be thrown if file is absent

type FileSize

type FileSize uint64

FileSize implements Unmarshal for parsing a file size config value. e.g. 10MB

func (FileSize) HumanReadable added in v1.0.4

func (fs FileSize) HumanReadable() string

HumanReadable returns a human readable form of the filesize e.g 12.5 MB, 1.0 GB

func (*FileSize) Unmarshal

func (fs *FileSize) Unmarshal(value string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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