conf

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2022 License: MIT Imports: 12 Imported by: 0

README

Conf

Conf is a front-end for https://github.com/jessevdk/go-flags that supports config files.

Examples

package main

import (
	"log"
	"os"
	"time"

	"github.com/go-chai/conf"
	"gopkg.in/yaml.v3"
)

func main() {
	cfg, err := conf.Load[Config](
		conf.ConfigFlag("conf"),
		conf.OptionalPaths("testdata/config.yaml"),
	)
	if err != nil {
		log.Fatalf("failed to load config: %s", err)
	}
	yaml.NewEncoder(os.Stdout).Encode(cfg)
}

type Config struct {
	Int        int `long:"i" yaml:"int" description:"int"`
	IntDefault int `long:"id" default:"1" yaml:"intDefault" description:"int with a default"`

	Float64        float64 `long:"f" yaml:"float64" description:"float64"`
	Float64Default float64 `long:"fd" default:"-3.14"  yaml:"float64Default" description:"float64 with a default"`

	NumericFlag bool `short:"3" long:"n" yaml:"numericFlag" description:"numeric flag"`

	String            string `long:"str" yaml:"string" description:"string"`
	StringDefault     string `long:"strd" default:"abc" yaml:"stringDefault" description:"string with a default"`
	StringNotUnquoted string `long:"strnot" unquote:"false" yaml:"stringNotUnquoted" description:"string not unquoted"`

	Time        time.Duration `long:"t" yaml:"time" description:"time"`
	TimeDefault time.Duration `long:"td" default:"1m" yaml:"timeDefault" description:"time with a default"`

	Map        map[string]int `long:"m" yaml:"map" description:"map"`
	MapDefault map[string]int `long:"md" default:"a:1" yaml:"mapDefault" description:"map with a default"`

	Slice        []int `long:"s" yaml:"slice" description:"slice"`
	SliceDefault []int `long:"sd" default:"1" default:"2" yaml:"sliceDefault" description:"slice with a default"`
}


go run ./examples/conf/ --help

Usage:
  conf [OPTIONS]

Application Options:
      --i=      int
      --id=     int with a default (default: 1)
      --f=      float64
      --fd=     float64 with a default (default: -3.14)
  -3, --n       numeric flag
      --str=    string
      --strd=   string with a default (default: abc)
      --strnot= string not unquoted
      --t=      time
      --td=     time with a default (default: 1m)
      --m=      map
      --md=     map with a default (default: a:1)
      --s=      slice
      --sd=     slice with a default (default: 1, 2)

Config:
      --conf=   config file paths

Help Options:
  -h, --help    Show this help message


go run ./examples/conf/

int: 3
intDefault: 13
float64: 2.712
float64Default: 1.1234
numericFlag: false
string: asdf
stringDefault: defg
stringNotUnquoted: ""
time: 13s
timeDefault: 11m0s
map:
    val1: 3
    val2: 4
mapDefault:
    val21: 21
    val22: 22
slice:
    - 1
    - 2
    - 3
sliceDefault:
    - 4
    - 5
    - 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultDecoders = map[string]DecoderFunc{
	".yaml": YAMLDecoder,
	".yml":  YAMLDecoder,
	".json": JSONDecoder,
	".toml": TOMLDecoder,
}
View Source
var JSONDecoder = func(cfg any, r io.Reader) error {
	err := yaml.NewDecoder(r).Decode(cfg)
	return errors.Wrap(err, "failed to decode json")
}
View Source
var TOMLDecoder = func(cfg any, r io.Reader) error {
	_, err := toml.DecodeReader(r, cfg)
	return errors.Wrap(err, "failed to decode toml")
}
View Source
var YAMLDecoder = func(cfg any, r io.Reader) error {
	err := yaml.NewDecoder(r).Decode(cfg)
	return errors.Wrap(err, "failed to decode yaml")
}

Functions

func Load

func Load[T any](opts ...ConfOption) (*T, error)

Types

type ConfOption

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

func AddDecoder

func AddDecoder(ext string, dec DecoderFunc) ConfOption

func Args

func Args(args []string) ConfOption

func ConfigFlag

func ConfigFlag(longName string, paths ...string) ConfOption

func ConfigFlagOption

func ConfigFlagOption(configFlagOption *flags.Option) ConfOption

func Delimiter

func Delimiter(delimiter string) ConfOption

func NoValidation

func NoValidation() ConfOption

func OptionalPaths

func OptionalPaths(paths ...string) ConfOption

func Paths

func Paths(paths ...string) ConfOption

func WithFlagOpts

func WithFlagOpts(flagOpts flags.Options) ConfOption

type ConfOptions

type ConfOptions []ConfOption

type DecoderFunc

type DecoderFunc func(cfg any, r io.Reader) error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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