config

package module
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 14 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 (
    "fmt"
    "log"
    "github.com/patrickhuber/go-config"
    "github.com/patrickhuber/go-cross"
    "github.com/patrickhuber/go-cross/arch"
    "github.com/patrickhuber/go-cross/platform"
)
func main(){
    args := []string{"--hello", "world"}

    // Create a target for cross-platform abstractions
    target := cross.NewTest(platform.Linux, arch.AMD64)
    filesystem := target.FS()

    // Use environment provider from target and set environment variable
    osEnv := target.Env()
    osEnv.Set("env", "yes")

    builder := config.NewBuilder(
        config.NewYaml(filesystem, "config.yml"),
        config.NewJson(filesystem, "config.json"),
        config.NewToml(filesystem, "config.toml"),
        config.NewEnv(osEnv, config.EnvOption{Prefix: "env"}),
        config.NewDotEnv(filesystem, ".env"),
        config.NewFlag([]config.Flag{
            &config.StringFlag{
                Name: "hello",
            },
        }, args),
    )
    
    root, err := builder.Build()
    if err != nil{
        log.Fatal(err)
    }

    cfg, err := root.Get(&config.GetContext{})
    if err != nil {
        log.Fatal(err)
    } else {
        log.Printf("%v", cfg)
    }
}

output

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

Documentation

Overview

Example
args := []string{"--hello", "world"}

// Create a target for cross-platform abstractions
target := cross.NewTest(platform.Linux, arch.AMD64)
filesystem := target.FS()

// Use environment provider from target and set environment variable
osEnv := target.Env()
osEnv.Set("env", "yes")

builder := config.NewBuilder(
	config.NewYaml(filesystem, "config.yml"),
	config.NewJson(filesystem, "config.json"),
	config.NewToml(filesystem, "config.toml"),
	config.NewEnv(osEnv, config.EnvOption{Prefix: "env"}),
	config.NewDotEnv(filesystem, ".env"),
	config.NewFlag([]config.Flag{
		&config.StringFlag{
			Name: "hello",
		},
	}, args),
)
root, err := builder.Build()
if err != nil {
	log.Fatal(err)
}

cfg, err := root.Get(&config.GetContext{})
if err != nil {
	log.Fatal(err)
} else {
	log.Printf("%v", cfg)
}

Index

Examples

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)

Merge merges 'from' into 'to' returning a new value without mutating inputs. Semantics (retains original intention): - Primitive / non-composite conflicts: keep 'to' value - Map conflicts: recursively merge; on leaf conflict keep 'to' - Slice/array conflicts: concatenate (from... + to...) if same concrete type - Nil handling: if one side is nil, return the other (deep copied for composites)

Types

type Builder

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

func NewBuilder

func NewBuilder(factories ...Factory) *Builder

func (*Builder) Build

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

func (*Builder) WithFactory added in v0.6.0

func (b *Builder) WithFactory(factory Factory) *Builder

func (*Builder) WithProvider added in v0.6.0

func (b *Builder) WithProvider(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 EnvOption added in v0.1.4

type EnvOption struct {
	Prefix       string
	Transformers []Transformer
}

type EnvProvider

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

func (*EnvProvider) Get

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

type Factory added in v0.6.0

type Factory interface {
	Providers() ([]Provider, error)
}

func NewDotEnv added in v0.1.1

func NewDotEnv(filesystem fs.FS, file string, options ...FileOption) Factory

func NewEnv

func NewEnv(environment env.Environment, options ...EnvOption) Factory

func NewFactory added in v0.6.0

func NewFactory(providers ...Provider) Factory

func NewFile added in v0.1.3

func NewFile(filesystem fs.FS, file string, codec Codec, options ...FileOption) Factory

func NewFlag

func NewFlag(flags []Flag, args []string, options ...FlagOption) Factory

func NewGlob

func NewGlob(
	filesystem fs.FS,
	filePath filepath.Provider,
	resolver GlobResolver,
	directory string,
	pattern string,
	options ...GlobOption) Factory

func NewGlobUp

func NewGlobUp(
	filesystem fs.FS,
	filePath filepath.Provider,
	resolver GlobResolver,
	directory string,
	pattern string,
	options ...GlobOption) Factory

func NewJson

func NewJson(filesystem fs.FS, file string, options ...FileOption) Factory

func NewToml

func NewToml(filesystem fs.FS, file string, options ...FileOption) Factory

func NewYaml

func NewYaml(filesystem fs.FS, file string, options ...FileOption) Factory

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 FlagOption added in v0.1.6

type FlagOption struct {
	Transformers []Transformer
}

type GetContext added in v0.1.3

type GetContext struct {
	MergedConfiguration any
}

type GlobOption added in v0.1.4

type GlobOption struct {
	Transformers []Transformer
}

type GlobResolver added in v0.6.0

type GlobResolver interface {
	Resolve(match string) Factory
}

GlobResolver returns the Provider for the given glob match

func DefaultGlobResolver added in v0.6.0

func DefaultGlobResolver(fileSystem fs.FS, path filepath.Provider) GlobResolver

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 NewMemory added in v0.1.3

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

func TransformProvider added in v0.1.3

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

type Root added in v0.2.0

type Root interface {
	Provider
	Providers() ([]Provider, error)
}

func NewRoot added in v0.2.0

func NewRoot(providers ...Provider) Root

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

func FuncTypedTransformer added in v0.1.7

func FuncTypedTransformer[T any](transform func(T) (T, 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