configo

package module
v0.0.0-...-b18a026 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: MIT Imports: 12 Imported by: 0

README

Configo

GoDoc Go Report Card

Configo provides an auto configuration loading tool without bothering to define a lot of flags, which is also highly extensible.

Usage

For the most simple usage, we define our config structure, and then let configo help to load configuration from env, file, flag by build-in loaders as follows:

// ./examples/simple/main.go

type Config struct {
        Foo string `yaml:"foo"`
}

func main() {
        var config Config
        _ = configo.Default().Load(&config, os.Args[1:])
        fmt.Printf("foo: %s\n", config.Foo)
}

Then we can run it like:

# with env
FOO=bar go run ./examples/simple/main.go

# with flag
go run ./examples/simple/main.go --foo bar

# with file config.yaml
go run ./examples/simple/main.go -f ./examples/simple/config.yaml

Custom Configuration Loader

TODO

Acknowledgement

The whole idea came from the awesome project traefik's package config, which has much more features to do configuration loading than current work. Configo is just another lightweight choice to do so.

Documentation

Overview

Package configo provides an auto configuration loading tool without bothering to define a lot of flags, which is also highly extensible.

For the most simple usage, we define our config structure, and then let configo help to load configuration from env, file, flag by build-in loaders as follows:

type Config struct {
	Foo string `yaml:"foo"`
}

func main() {
	var config Config
	_ = configo.Default().Load(&config, os.Args[1:])
	fmt.Printf("foo: %s\n", config.Foo)
}

Then we can run ./examples/simple/main.go like:

# with env
FOO=bar go run ./examples/simple/main.go

# with flag
go run ./examples/simple/main.go --foo bar

# with file config.yaml
go run ./examples/simple/main.go -f ./examples/simple/config.yaml

You will see more usage in ./examples

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseFile

func ParseFile(filepaths []string) (*util.FlattenMap, error)

ParseFile parses files into a map.

func ParseFlag

func ParseFlag(args []string) (*util.FlattenMap, error)

ParseFlag parses arguments into a map.

Types

type Configo

type Configo struct {
	Loader         Loader
	TagName        string
	TagDescription string
}

Configo is the main structure of configo which wraps all utilities for quick usage.

func Default

func Default() *Configo

Default provides a common usage of Configo with env, file, flag loaders.

func (*Configo) Load

func (configo *Configo) Load(config interface{}, args []string) error

Load loads configurations into config, optionally used with arguments to do so.

type EnvLoader

type EnvLoader struct {
	Prefix         string
	DisallowUnused bool
}

EnvLoader loads from environment variables.

func (*EnvLoader) Load

func (loader *EnvLoader) Load(n *node.Node, args []string) error

Load implements ConfigLoader.

type FileLoader

type FileLoader struct {
	DisallowUnused bool
	ConfigFileFlag string
	ConfigFileEnv  string
}

FileLoader loads config from file.

func (*FileLoader) Load

func (loader *FileLoader) Load(n *node.Node, args []string) error

Load implements ConfigLoader.

type FlagLoader

type FlagLoader struct {
	DisallowUnused bool
	EscapeUnused   []string
}

FlagLoader loads config from flags.

func (*FlagLoader) Load

func (loader *FlagLoader) Load(n *node.Node, args []string) error

Load implements ConfigLoader.

type Loader

type Loader interface {
	// Load fills node, optionally used with arguments to do so.
	Load(n *node.Node, args []string) error
}

Loader is a configuration store.

type Loaders

type Loaders []Loader

Loaders wraps multiple Loaders as Loader interface

func (Loaders) Load

func (loaders Loaders) Load(n *node.Node, args []string) error

Load implements Loader

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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