cfg

package module
v0.0.0-...-93e75a8 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnvOption

type EnvOption func(*envSource)

func EnvSplitter

func EnvSplitter(splitter string) EnvOption

type FileDecoder

type FileDecoder interface {
	ExtNames() []string
	MimeNames() []string
	TagName() string
	Decode(r io.Reader, a any) error
}

type FileOption

type FileOption func(s *fileSource)

func FileDecoders

func FileDecoders(decoder ...FileDecoder) FileOption

func FileFlagSet

func FileFlagSet(set FlagSet) FileOption

type FlagOption

type FlagOption func(*flagSource)

func FlagSplitter

func FlagSplitter(splitter string) FlagOption

func FlagWithFlagSet

func FlagWithFlagSet(set FlagSet) FlagOption

type FlagSet

type FlagSet interface {
	BoolVar(p *bool, name string, value bool, usage string)
	StringVar(p *string, name string, value string, usage string)
	DurationVar(p *time.Duration, name string, value time.Duration, usage string)
	TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)
	Float64Var(p *float64, name string, value float64, usage string)
	Uint64Var(p *uint64, name string, value uint64, usage string)
	UintVar(p *uint, name string, value uint, usage string)
	Int64Var(p *int64, name string, value int64, usage string)
	IntVar(p *int, name string, value int, usage string)
	Var(value flag.Value, name string, usage string)
}

type JSON

type JSON struct{}

func (JSON) Decode

func (JSON) Decode(r io.Reader, a any) error

func (JSON) ExtNames

func (JSON) ExtNames() []string

func (JSON) MimeNames

func (JSON) MimeNames() []string

func (JSON) TagName

func (JSON) TagName() string

type Parser

type Parser[T any] struct {
	// contains filtered or unexported fields
}
Example

An example to load `Config` from sources orderly: - from a config file - from env variables - from flags

The same field in the flag overwrites the env variable, and the same file in the env variable overwrites the value in the config file.

package main

import (
	"bytes"
	"context"
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/googollee/go-cfg"
)

type Config struct {
	Str     string `cfg:"str,,string value from file"`
	I       int    `cfg:"int,,int value from file"`
	FromEnv struct {
		Str string `cfg:"str,none,string value from env"`
	} `cfg:"from_env"`
	FromFlag struct {
		Str string `cfg:"str,none,string value from flags"`
	} `cfg:"from_flag"`
	WithDefault struct {
		Str string `cfg:"str,default_value,string value with default"`
	} `cfg:"with_default"`
}

// An example to load `Config` from sources orderly:
// - from a config file
// - from env variables
// - from flags
//
// The same field in the flag overwrites the env variable, and the same file in the env variable overwrites the value in the config file.
func main() {
	os.Setenv("DEMO_FROM_ENV_STR", "string_from_env")

	set := flag.NewFlagSet("demo", flag.PanicOnError)

	parser := cfg.Parse[Config](
		cfg.FromFile("config", "./testdata/config.json", "config file",
			cfg.FileDecoders(cfg.JSON{}),
			cfg.FileFlagSet(set)),
		cfg.FromEnv("DEMO", cfg.EnvSplitter("_")),
		cfg.FromFlag("demo", cfg.FlagSplitter("."), cfg.FlagWithFlagSet(set)),
	)

	if err := set.Parse([]string{
		"--config", "./testdata/config.json",
		"--demo.from_flag.str", "string_from_flag",
	}); err != nil {
		fmt.Println("flag error:", err)
		return
	}

	var buf bytes.Buffer
	set.SetOutput(&buf)
	set.Usage()
	fmt.Println(strings.ReplaceAll(buf.String(), "\t", "  "))

	var config Config
	if err := parser.Parse(context.Background(), &config); err != nil {
		fmt.Println("load config error:", err)
		return
	}

	fmt.Println("config:", config)

}
Output:

Usage of demo:
  -config string
      config file (default "./testdata/config.json")
  -demo.from_env.str value
      string value from env (default none)
  -demo.from_flag.str value
      string value from flags (default none)
  -demo.int value
      int value from file
  -demo.str value
      string value from file
  -demo.with_default.str value
      string value with default (default default_value)

config: {string 10 {string_from_env} {string_from_flag} {default_value}}

func Parse

func Parse[T any](src ...Source) *Parser[T]

func (*Parser[T]) Parse

func (p *Parser[T]) Parse(ctx context.Context, v *T) error

type Source

type Source interface {
	Setup(t reflect.Type) error
	Parse(ctx context.Context, v any) error
}

func FromEnv

func FromEnv(prefix string, opt ...EnvOption) Source

func FromFile

func FromFile(flagName, flagValue, flagUsage string, opt ...FileOption) Source

func FromFlag

func FromFlag(prefix string, opt ...FlagOption) Source

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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