config

package
v0.0.0-...-67c52db Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Example
package main

import (
	"path"

	"github.com/sraphs/go/config"
	"github.com/sraphs/go/config/env"
	"github.com/sraphs/go/config/file"
	"github.com/sraphs/go/config/flag"

	testData "github.com/sraphs/go/config/internal/testdata"
)

func main() {
	p := path.Join("internal", "testdata")

	c := config.New(
		config.WithSource(
			env.NewSource("sraph_"),
			file.NewSource(p),
			flag.NewSource(),
		),
	)

	if err := c.Load(); err != nil {
		panic(err)
	}

	var conf testData.Conf

	if err := c.Scan(&conf); err != nil {
		panic(err)
	}

	c.Watch(func(c config.Config) {
		c.Scan(&conf)
	})

	// fmt.Println(conf)

}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedFormat = errors.New("unsupported format")
)
View Source
var (
	SupportedFormats = []string{"env", "json", "xml", "yaml", "yml"}
)

Functions

This section is empty.

Types

type Config

type Config interface {
	Load() error
	Scan(v interface{}) error
	Watch(o Observer) error
	Close() error
}

Config is a config interface.

func New

func New(opts ...Option) Config

New new a config with options.

type Descriptor

type Descriptor struct {
	Name   string
	Format string
	Data   []byte
}

Descriptor is file or env or flag descriptor.

func (*Descriptor) GetCodec

func (d *Descriptor) GetCodec() encoding.Codec

type Observer

type Observer func(Config)

Observer is config observer.

type Option

type Option func(*options)

Option is config option.

func WithSource

func WithSource(s ...Source) Option

WithSource with config source.

type Source

type Source interface {
	Load() ([]*Descriptor, error)
	Watch() (Watcher, error)
}

Source is config source.

type Watcher

type Watcher interface {
	Next() ([]*Descriptor, error)
	Stop() error
}

Watcher watches a source for changes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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