Git Product home page Git Product logo

cfg's Introduction

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

cfg's People

Contributors

lhooge avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.