config

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2025 License: MIT Imports: 13 Imported by: 0

README

Go Config

configuration library in go

Get it

go get github.com/patrickhuber/go-config

Example

config.yml

yaml: yes

config.json

{"json": "yes"}

config.toml

toml="yes"

.env

dotenv="yes"
package main
import (
    "log"
    "github.com/patrickhuber/go-config"
)
func main(){
    args := []string{"--hello", "world"}
    os.SetEnv("env", "yes")
    builder := config.NewBuilder(
        config.NewYaml("config.yml"),
        config.NewJson("config.json"),
        config.NewToml("config.toml"),
        config.NewEnv("env"),
        config.NewDotEnv(".env"),
        config.NewFlag([]config.Flag{
            config.StringFlag{
                Name: "hello",
            },
        }, args),
    )    
    cfg, err := builder.Build()
    if err != nil{
        log.Fatal(err)
    }else{
        fmt.Println("%v", cfg)
    }
}

output

yaml: yes
json: yes
toml: yes
dotenv: yes
env: yes

Documentation

Index

Constants

View Source
const (
	Create changeType = "create"
	Update changeType = "update"
	Delete changeType = "delete"
)

Variables

This section is empty.

Functions

func Merge added in v0.1.2

func Merge(from any, to any) (any, error)

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder(providers ...Provider) *Builder

func (*Builder) Build

func (b *Builder) Build() (any, error)

func (*Builder) With

func (b *Builder) With(provider Provider) *Builder

type Change added in v0.1.2

type Change struct {
	ChangeType ChangeType
	Path       []string
	From       any
	To         any
}

func Diff added in v0.1.2

func Diff(from, to any) ([]Change, error)

type ChangeType added in v0.1.2

type ChangeType interface {
	// contains filtered or unexported methods
}

type Codec added in v0.1.3

type Codec interface {
	Marshaler
	Unmarshaler
}

func NewJsonCodec added in v0.1.3

func NewJsonCodec() Codec

func NewTomlCodec added in v0.1.3

func NewTomlCodec() Codec

func NewYamlCodec added in v0.1.3

func NewYamlCodec() Codec

type DynamicResolver added in v0.1.5

type DynamicResolver func(ctx *GetContext) (Provider, error)

type EnvOption added in v0.1.4

type EnvOption struct {
	Prefix       string
	Transformers []Transformer
}

type EnvProvider

type EnvProvider struct {
	// contains filtered or unexported fields
}

func NewEnv

func NewEnv(options ...EnvOption) *EnvProvider

func (*EnvProvider) Get

func (p *EnvProvider) Get(ctx *GetContext) (any, error)

type FileBeforeGet added in v0.1.4

type FileBeforeGet func(ctx *GetContext, provider Provider, file string) (any, error)

type FileOption added in v0.1.4

type FileOption struct {
	Transformers []Transformer
	BeforeGet    FileBeforeGet
}

type Flag

type Flag interface {
	Set(value string) error
	String() string
	Value() any
}

type GetContext added in v0.1.3

type GetContext struct {
	MergedConfiguration any
}

type GlobOption added in v0.1.4

type GlobOption struct {
	Transformers []Transformer
	Resolver     GlobProviderResolver
}

type GlobProviderResolver added in v0.1.3

type GlobProviderResolver func(match string) Provider

GlobProviderResolver returns the Provider for the given glob match

type JsonOption added in v0.1.4

type JsonOption struct {
	Transformers []Transformer
}

type Marshaler added in v0.1.3

type Marshaler interface {
	Marshal(data any) ([]byte, error)
}

type Provider

type Provider interface {
	Get(ctx *GetContext) (any, error)
}

func NewDotEnv added in v0.1.1

func NewDotEnv(file string, options ...FileOption) Provider

func NewDynamic added in v0.1.5

func NewDynamic(resolver DynamicResolver) Provider

func NewFile added in v0.1.3

func NewFile(file string, codec Codec, options ...FileOption) Provider

func NewFlag

func NewFlag(flags []Flag, args []string) Provider

func NewGlob

func NewGlob(directory string, pattern string, options ...GlobOption) Provider

func NewGlobUp

func NewGlobUp(directory string, pattern string, options ...GlobOption) Provider

func NewJson

func NewJson(file string, options ...FileOption) Provider

func NewMemory added in v0.1.3

func NewMemory(memory any, transformers ...Transformer) Provider

func NewToml

func NewToml(file string, options ...FileOption) Provider

func NewYaml

func NewYaml(file string, options ...FileOption) Provider

func TransformProvider added in v0.1.3

func TransformProvider(transform func(any) (any, error)) Provider

type SetContext added in v0.1.3

type SetContext struct{}

type StringFlag

type StringFlag struct {
	Name    string
	Default string
	Usage   string
	// contains filtered or unexported fields
}

func (*StringFlag) Set

func (s *StringFlag) Set(value string) error

func (*StringFlag) String

func (s *StringFlag) String() string

func (*StringFlag) Value

func (s *StringFlag) Value() any

type StringSliceFlag

type StringSliceFlag struct {
	Name    string
	Default []string
	Usage   string
	// contains filtered or unexported fields
}

func (*StringSliceFlag) Set

func (s *StringSliceFlag) Set(value string) error

func (*StringSliceFlag) String

func (s *StringSliceFlag) String() string

func (*StringSliceFlag) Value

func (s *StringSliceFlag) Value() any

type Transformer added in v0.1.3

type Transformer interface {
	Transform(instance any) (any, error)
}

func FuncTransformer added in v0.1.3

func FuncTransformer(transform func(any) (any, error)) Transformer

type Unmarshaler added in v0.1.3

type Unmarshaler interface {
	Unmarshal(buf []byte) (any, error)
}

Jump to

Keyboard shortcuts

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