config

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 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 DotEnvProvider added in v0.1.1

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

func NewDotEnv added in v0.1.1

func NewDotEnv(file string) *DotEnvProvider

func (*DotEnvProvider) Get added in v0.1.1

func (p *DotEnvProvider) Get() (any, error)

type EnvProvider

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

func NewEnv

func NewEnv(prefix string) *EnvProvider

func (*EnvProvider) Get

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

type Flag

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

type FlagProvider

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

func NewFlag

func NewFlag(flags []Flag, args []string) *FlagProvider

func (*FlagProvider) Get

func (p *FlagProvider) Get() (any, error)

type GlobProvider

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

func NewGlob

func NewGlob(directory string, pattern string) *GlobProvider

func NewGlobUp

func NewGlobUp(directory string, pattern string) *GlobProvider

func (*GlobProvider) Get

func (g *GlobProvider) Get() (any, error)

type JsonProvider

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

func NewJson

func NewJson(file string) *JsonProvider

func (*JsonProvider) Get

func (p *JsonProvider) Get() (any, error)

type Provider

type Provider interface {
	Get() (any, error)
}

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 TomlProvider

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

func NewToml

func NewToml(file string) *TomlProvider

func (*TomlProvider) Get

func (p *TomlProvider) Get() (any, error)

type YamlProvider

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

func NewYaml

func NewYaml(file string) *YamlProvider

func (*YamlProvider) Get

func (p *YamlProvider) Get() (any, error)

Jump to

Keyboard shortcuts

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