Giter Site home page Giter Site logo

lodr's Introduction

lodr

Minimal configuration loader for Go.

Load from file (YAML, JSON), from environment variables or from command-line flags.

Usage

Load from file

# config.yaml
name: value
package main

import (
    "fmt"
    "github.com/blakelead/lodr"
)

type MyConfig struct {
    Name string
}

func main() {
    var myConfig MyConfig

    lodr.Load(&myConfig).File("config.yaml")

    fmt.Println(myConfig.Name)
}
> go run main.go
value

Load from file with tag

It uses gopkg.in/yaml.v2 so it works exactly the same:

# config.yaml
my_app_name: value
type MyConfig struct {
    Name string `yaml:"my_app_name"`
}

func main() {
    var myConfig MyConfig

    lodr.Load(&myConfig).File("config.yaml")

    fmt.Println(myConfig.Name)
}
> go run main.go
value

Load from env

type MyConfig struct {
    Name string
}

func main() {

    var myConfig MyConfig

    lodr.Load(&myConfig).Env()

    fmt.Println(myConfig.Name)
}
> NAME=new_value go run main.go
new_value

Load from env with tag

type MyConfig struct {
    Name string `env:MY_APP_NAME`
}

func main() {

    var myConfig MyConfig

    lodr.Load(&myConfig).Env()

    fmt.Println(myConfig.Name)
}
> MY_APP_NAME=value go run main.go
value

Load from env with options

type MyConfig struct {
    Name string `env:NAME`
}

func main() {

    var myConfig MyConfig

    opts := &lodr.EnvOptions{
          Prefix:     "MY_APP",
          ProcessAll: false,
    }

    lodr.Load(&myConfig).EnvWithOptions(opts)

    fmt.Println(myConfig.Name)
}
> MY_APP_NAME=value go run main.go
value

Options:

  • Prefix: environment variables are all prefixed by this value
  • ProcessAll: if true, no need to specify tags. Names will be infered from attributes.

Load from command-line arguments

main.go

type MyConfig struct {
   Name string `cmd:myapp.name`
}

func main() {

   var myConfig MyConfig

   lodr.Load(&myConfig).Cmd()

   fmt.Println(myConfig.Name)
}
> go run main.go --myapp.name the_value
the_value

Complete example

# config.yaml
name: mydb
db:
  host: localhost
  port: 8080
  timeout: 10s
  tls: true
package main

import (
    "fmt"
    "time"
    "github.com/blakelead/lodr"
)

type MyConfig struct {
    Name string `cmd:"name"`
    DB   struct {
        Host     string        `yaml:"host" env:"DB_HOST" cmd:"db.host"`
        Port     int           `yaml:"port" env:"DB_PORT" cmd:"db.port"`
        Password string        `env:"DB_PASSWORD"`
        Timeout  time.Duration `yaml:"timeout" env:"DB_TIMEOUT" cmd:"db.timeout"`
        TLS      bool          `yaml:"tls" env:"DB_TLS" cmd:"db.tls"`
    }
}

func main() {
    var mc MyConfig

    opts := &lodr.EnvOptions{
        Prefix: "MY_APP",
    }

    c := lodr.Load(&mc).File("config.yaml").EnvWithOptions(opts).Cmd()

    if c.Error != nil {
        panic(c.Error)
    }

    fmt.Println(mc.Name)
    fmt.Println(mc.DB.Password)
    fmt.Printf("%s:%d\n", mc.DB.Host, mc.DB.Port)
}
> MY_APP_DB_PASSWORD=pass go run main.go --name the_db
the_db
pass
localhost:8080

lodr's People

Watchers

James Cloos avatar

Forkers

hugmatj

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.