go-confkey: github.com/choria-io/go-confkey Index | Examples | Files

package confkey

import "github.com/choria-io/go-confkey"

Package confkey looks for tags on a structure and set values based on the tag rather than the struct item names

Features

Defaults are supported and can be fetched from the shell environment

The tags can specify some formating like comma splits and other commonly seen patterns in config files.

Conversion of []string, ints, strings, time.Duration and booleans are support

Validations can be done on a struct basis using the github.com/choria-io/go-validators package

A sample structure might look like this, the package contains utilities to set values, apply defaults and perform validations

type Config struct {
    Loglevel string        `confkey:"loglevel" default:"warn" validate:"enum=debug,info,warn,error"`
    Mode     string        `confkey:"mode" default:"server" validate:"enum=server,client"`
    Servers  []string      `confkey:"servers" type:"comma_split" environment:"SERVERS"`
    Path     []string      `confkey:"path" type:"path_split" default:"/bin:/usr/bin"`
    I        time.Duration `confkey:"interval" type:"duration" default:"1h"`
}

The utilities here will let you parse any config file that might have keys like loglevel etc and map the string values read from the text file onto the structure

Code:

package main

import (
    "fmt"
    "os"
    "strings"

    confkey "github.com/choria-io/go-confkey"
)

type Config struct {
    Loglevel string   `confkey:"loglevel" default:"warn" validate:"enum=debug,info,warn,error"`
    Mode     string   `confkey:"mode" default:"server" validate:"enum=server,client"`
    Servers  []string `confkey:"servers" type:"comma_split" environment:"SERVERS"`
    Path     []string `confkey:"path" type:"path_split" default:"/bin:/usr/bin"` // can also be colon_split to always split on :
}

func main() {
    c := &Config{}

    err := confkey.SetStructDefaults(c)
    if err != nil {
        panic(err)
    }

    fmt.Println("Defaults:")
    fmt.Printf("  loglevel: %s\n", c.Loglevel)
    fmt.Printf("  mode: %s\n", c.Mode)
    fmt.Printf("  path: %s\n", strings.Join(c.Path, ","))
    fmt.Println("")

    // here you would read your config file, but lets just fake it
    // and set specific values

    // every call to SetStructFieldWithKey validates what gets set
    err = confkey.SetStructFieldWithKey(c, "loglevel", "error")
    if err != nil {
        panic(err)
    }

    err = confkey.SetStructFieldWithKey(c, "mode", "client")
    if err != nil {
        panic(err)
    }

    // even though we are setting it, if the ENV is set it overrides
    os.Setenv("SERVERS", "s1:1024, s2:1024")
    err = confkey.SetStructFieldWithKey(c, "servers", "s:1024")
    if err != nil {
        panic(err)
    }

    fmt.Println("Loaded:")
    fmt.Printf("  loglevel: %s\n", c.Loglevel)
    fmt.Printf("  mode: %s\n", c.Mode)
    fmt.Printf("  servers: %s\n", strings.Join(c.Servers, ","))
    fmt.Println("")

    // getting a string by name
    fmt.Println("Retrieved:")
    fmt.Printf("  loglevel: %s\n", confkey.StringFieldWithKey(c, "loglevel"))
    fmt.Printf("  servers: %s\n", strings.Join(confkey.StringListWithKey(c, "servers"), ","))
    fmt.Println("")

    // but you can also validate the entire struct if you like, perhaps you
    // set some stuff directly to its fields
    err = confkey.Validate(c)
    if err != nil {
        fmt.Printf("invalid: %s\n", err)
        panic(err)
    }

    fmt.Println("valid")

    // setting a specific bad value yields an error
    err = confkey.SetStructFieldWithKey(c, "loglevel", "fail")
    if err != nil {
        fmt.Printf("invalid: %s\n", err)
    }

}

Index

Examples

Package Files

confkey.go

func BoolWithKey Uses

func BoolWithKey(target interface{}, key string) bool

BoolWithKey retrieves a bool from target that matches key, false when not found

func Int64WithKey Uses

func Int64WithKey(target interface{}, key string) int64

Int64WithKey retrieves an int from target that matches key, 0 when not found

func IntWithKey Uses

func IntWithKey(target interface{}, key string) int

IntWithKey retrieves an int from target that matches key, 0 when not found

func SetStructDefaults Uses

func SetStructDefaults(target interface{}) error

SetStructDefaults extract defaults out of the tags and set them to the key

func SetStructFieldWithKey Uses

func SetStructFieldWithKey(target interface{}, key string, value interface{}) error

SetStructFieldWithKey finds the struct key that matches the confkey on target and assign the value to it

func StringFieldWithKey Uses

func StringFieldWithKey(target interface{}, key string) string

StringFieldWithKey retrieves a string from target that matches key, "" when not found

func StringListWithKey Uses

func StringListWithKey(target interface{}, key string) []string

StringListWithKey retrieves a []string from target that matches key, empty when not found

func Validate Uses

func Validate(target interface{}) error

Validate validates the struct

Package confkey imports 10 packages (graph) and is imported by 2 packages. Updated 2018-05-21. Refresh now. Tools for package owners.