Giter VIP home page Giter VIP logo

paerser's Introduction

Paerser

Package documentation Build Status Go Report Card

Features

Loads configuration from many sources:

  • CLI flags
  • Configuration files in YAML, TOML, JSON format
  • Environment variables

It also provides a simple CLI commands handling system.

Examples

Configuration

CLI Flags

package flag_test

import (
	"log"

	"github.com/davecgh/go-spew/spew"
	"github.com/traefik/paerser/flag"
)

type ConfigExample struct {
	Foo   string
	Bar   Bar
	Great bool
}

type Bar struct {
	Sub  *Sub
	List []string
}

type Sub struct {
	Name  string
	Value int
}

func ExampleDecode() {
	args := []string{
		"--foo=aaa",
		"--great=true",
		"--bar.list=AAA,BBB",
		"--bar.sub.name=bbb",
		"--bar.sub.value=6",
	}

	config := ConfigExample{}

	err := flag.Decode(args, &config)
	if err != nil {
		log.Fatal(err)
	}

	spew.Config = spew.ConfigState{
		Indent:                  "\t",
		DisablePointerAddresses: true,
	}
	spew.Dump(config)

	// Output:
	// (flag_test.ConfigExample) {
	// 	Foo: (string) (len=3) "aaa",
	// 	Bar: (flag_test.Bar) {
	// 		Sub: (*flag_test.Sub)({
	// 			Name: (string) (len=3) "bbb",
	// 			Value: (int) 6
	// 		}),
	// 		List: ([]string) (len=2 cap=2) {
	// 			(string) (len=3) "AAA",
	// 			(string) (len=3) "BBB"
	// 		}
	// 	},
	// 	Great: (bool) true
	// }
}

File

package file_test

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"github.com/davecgh/go-spew/spew"
	"github.com/traefik/paerser/file"
)

type ConfigExample struct {
	Foo   string
	Bar   Bar
	Great bool
}

type Bar struct {
	Sub  *Sub
	List []string
}

type Sub struct {
	Name  string
	Value int
}

func ExampleDecode() {
	tempFile, err := ioutil.TempFile("", "paeser-*.yml")
	if err != nil {
		log.Fatal(err)
	}

	defer os.RemoveAll(tempFile.Name())

	data := `
foo: aaa
bar:
  sub:
    name: bbb
    value: 6
  list:
  - AAA
  - BBB
great: true
`

	_, err = fmt.Fprint(tempFile, data)
	if err != nil {
		log.Fatal(err)
	}

	// Read configuration file

	filePath := tempFile.Name()

	config := ConfigExample{}

	err = file.Decode(filePath, &config)
	if err != nil {
		log.Fatal(err)
	}

	spew.Config = spew.ConfigState{
		Indent:                  "\t",
		DisablePointerAddresses: true,
	}
	spew.Dump(config)

	// Output:
	// (file_test.ConfigExample) {
	// 	Foo: (string) (len=3) "aaa",
	// 	Bar: (file_test.Bar) {
	// 		Sub: (*file_test.Sub)({
	// 			Name: (string) (len=3) "bbb",
	// 			Value: (int) 6
	// 		}),
	// 		List: ([]string) (len=2 cap=2) {
	// 			(string) (len=3) "AAA",
	// 			(string) (len=3) "BBB"
	// 		}
	// 	},
	// 	Great: (bool) true
	// }
}

Environment Variables

package env_test

import (
	"log"
	"os"

	"github.com/davecgh/go-spew/spew"
	"github.com/traefik/paerser/env"
)

type ConfigExample struct {
	Foo   string
	Bar   Bar
	Great bool
}

type Bar struct {
	Sub  *Sub
	List []string
}

type Sub struct {
	Name  string
	Value int
}

func ExampleDecode() {
	_ = os.Setenv("MYAPP_FOO", "aaa")
	_ = os.Setenv("MYAPP_GREAT", "true")
	_ = os.Setenv("MYAPP_BAR_LIST", "AAA,BBB")
	_ = os.Setenv("MYAPP_BAR_SUB_NAME", "bbb")
	_ = os.Setenv("MYAPP_BAR_SUB_VALUE", "6")

	config := ConfigExample{}

	err := env.Decode(os.Environ(), "MYAPP_", &config)
	if err != nil {
		log.Fatal(err)
	}

	spew.Config = spew.ConfigState{
		Indent:                  "\t",
		DisablePointerAddresses: true,
	}
	spew.Dump(config)

	// Output:
	// (env_test.ConfigExample) {
	// 	Foo: (string) (len=3) "aaa",
	// 	Bar: (env_test.Bar) {
	// 		Sub: (*env_test.Sub)({
	// 			Name: (string) (len=3) "bbb",
	// 			Value: (int) 6
	// 		}),
	// 		List: ([]string) (len=2 cap=2) {
	// 			(string) (len=3) "AAA",
	// 			(string) (len=3) "BBB"
	// 		}
	// 	},
	// 	Great: (bool) true
	// }
}

CLI Commands

TODO

paerser's People

Contributors

ldez avatar jbdoumenjou avatar lbenguigui avatar rbeuque74 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.