conf

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: MIT Imports: 21 Imported by: 54

Documentation

Index

Constants

View Source
const (
	// KeyDelimiter is used as the default key delimiter in the default koanf instance.
	KeyDelimiter = "."
)

Variables

This section is empty.

Functions

func Abs

func Abs(path string) string

Abs returns the absolute path by the base dir if path is a relative path

func AllSettings

func AllSettings() map[string]any

AllSettings return all settings

func Bool

func Bool(path string) bool

func Duration

func Duration(path string) time.Duration

func Float64

func Float64(path string) float64

func Get

func Get(path string) any

func GetIP added in v0.0.3

func GetIP(useIPv6 bool) string

GetIP returns the first non-loopback address

func Int

func Int(path string) int

func IntSlice

func IntSlice(path string) []int

func IsSet

func IsSet(path string) bool

IsSet check if the key is set

func Join

func Join(ps ...string) string

Join paths

func ParseEnv added in v0.4.0

func ParseEnv(src []byte) []byte

ParseEnv parse env value in src.

func String

func String(path string) string

func StringMap

func StringMap(path string) map[string]string

func StringSlice

func StringSlice(path string) []string

func Time

func Time(path string, layout string) time.Time

Time return time by layout, eg: 2006-01-02 15:04:05

if config is init from a map value of time.Time, the layout will be: `2006-01-02 15:04:05 -0700 MST`

func TryLoadEnvFromFile added in v0.4.0

func TryLoadEnvFromFile(scan, mod string)

TryLoadEnvFromFile try load env from files

Types

type AppConfiguration added in v0.0.3

type AppConfiguration struct {
	*Configuration
}

AppConfiguration is the application level configuration,include all of component's configurations

func Global

func Global() *AppConfiguration

Global return default(global) Configuration instance

type Configurable

type Configurable interface {
	// Apply set up property or field value by configuration
	//
	// cnf is the Configuration of the component, and it's sub configuration of root
	// path is the relative path to root,if root is the component,path will be empty
	// Apply also use for lazy load scene
	// notice: if any error in apply process,you should choose use panic to expose error,and stop application.
	Apply(cnf *Configuration) error
}

Configurable can initial by framework

type Configuration

type Configuration struct {
	Development bool
	// contains filtered or unexported fields
}

Configuration hold settings of the component.

func New

func New(opts ...Option) *Configuration

New create an application configuration instance.

New as global by default, if you want to create a local configuration, set global to false. Initialization such as:

cnf := conf.New().Load()

func NewFromBytes

func NewFromBytes(b []byte, opts ...Option) *Configuration

NewFromBytes create from byte slice,return with a parser, But you'd better use Load()

func NewFromParse added in v0.0.3

func NewFromParse(parser *Parser, opts ...Option) *Configuration

NewFromParse create from parser

func NewFromStringMap added in v0.1.0

func NewFromStringMap(data map[string]any, opts ...Option) *Configuration

NewFromStringMap create from a string map

func (*Configuration) Abs

func (c *Configuration) Abs(path string) string

Abs returns the absolute path by the base dir if path is a relative path,if path is empty return empty.

func (*Configuration) AllSettings

func (c *Configuration) AllSettings() map[string]any

func (*Configuration) AppName

func (c *Configuration) AppName() string

AppName indicates the application's name

returns the path 'appName' value if exists

func (*Configuration) AsGlobal

func (c *Configuration) AsGlobal() *Configuration

AsGlobal set the Configuration as global

func (*Configuration) Bool

func (c *Configuration) Bool(path string) bool

func (*Configuration) Copy

func (c *Configuration) Copy() *Configuration

func (*Configuration) CutFromOperator

func (c *Configuration) CutFromOperator(kf *koanf.Koanf) *Configuration

CutFromOperator return a new copied Configuration but replace the parser by koanf operator.

func (*Configuration) Duration

func (c *Configuration) Duration(path string) time.Duration

func (*Configuration) Each added in v0.1.0

func (c *Configuration) Each(path string, cb func(root string, sub *Configuration))

Each iterates the slice path of the Configuration.

func (*Configuration) Exists added in v0.4.0

func (c *Configuration) Exists() bool

Exists check if the default configuration file exists

func (*Configuration) Float64

func (c *Configuration) Float64(path string) float64

func (*Configuration) Get

func (c *Configuration) Get(key string) any

func (*Configuration) GetBaseDir

func (c *Configuration) GetBaseDir() string

GetBaseDir return the application dir

func (*Configuration) Int

func (c *Configuration) Int(path string) int

func (*Configuration) IntSlice

func (c *Configuration) IntSlice(path string) []int

func (*Configuration) IsSet

func (c *Configuration) IsSet(path string) bool

IsSet check if the key is set

func (*Configuration) Load

func (c *Configuration) Load() *Configuration

Load parse configuration from file

func (*Configuration) Map added in v0.4.0

func (c *Configuration) Map(path string, cb func(root string, sub *Configuration))

Map iterates the map path of the Configuration.It is sort by sort.Strings, no guarantee of the order in the configuration file. Each key in the path must be a map value, otherwise it will panic.

func (*Configuration) Merge

func (c *Configuration) Merge(b []byte) error

Merge an input config stream, parameter b is YAML stream

types operation:

  • map[string]any: merge
  • []any: override

func (*Configuration) Namespace added in v0.2.0

func (c *Configuration) Namespace() string

Namespace indicates the application's namespace

func (*Configuration) Parser

func (c *Configuration) Parser() *Parser

Parser return configuration operator

func (*Configuration) ParserOperator

func (c *Configuration) ParserOperator() *koanf.Koanf

ParserOperator return the underlying parser that converts bytes to map

func (*Configuration) Root

func (c *Configuration) Root() *Configuration

Root return root configuration if it came from Sub method

func (*Configuration) SetBaseDir

func (c *Configuration) SetBaseDir(dir string)

SetBaseDir return the application dir

func (*Configuration) String

func (c *Configuration) String(path string) string

func (*Configuration) StringMap

func (c *Configuration) StringMap(path string) map[string]string

func (*Configuration) StringSlice

func (c *Configuration) StringSlice(path string) []string

func (*Configuration) Sub

func (c *Configuration) Sub(path string) *Configuration

Sub return a new Configuration by a sub node.return current node if path empty,panic if path not found.

sub node keeps the same root configuration of the current node.

func (*Configuration) Time

func (c *Configuration) Time(path string, layout string) time.Time

func (*Configuration) Unmarshal added in v0.0.2

func (c *Configuration) Unmarshal(dst any) (err error)

Unmarshal map data of config into a struct, values are merged.

Tags on the fields of the structure must be properly set. Notice: if use nested struct, should tag `,inline` and can't use pointer: like

	type Config struct {
	    BaseConfig `yaml:",inline"`
     // should not *BaseConfig
 }

func (*Configuration) Version

func (c *Configuration) Version() string

Version indicates the application's sem version

returns the path 'version' value, no set return empty

type Option

type Option func(*options)

Option the function to apply configuration option

func WithBaseDir added in v0.0.3

func WithBaseDir(s string) Option

WithBaseDir init base directory where configuration files location, usually is the directory which application executable file is in parameter s can be an absolute path or relative path.

func WithGlobal added in v0.0.3

func WithGlobal(g bool) Option

WithGlobal indicate weather use as global configuration

func WithIncludeFiles added in v0.0.3

func WithIncludeFiles(paths ...string) Option

WithIncludeFiles init include files

The configuration in the attached file will overwrite the master configuration file and will be ignored for invalid files. you can set a configuration for dev ENV,but attach instance only effect in local file configuration

func WithLocalPath added in v0.0.3

func WithLocalPath(s string) Option

WithLocalPath init local instance file path A s is file path

type Parser

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

Parser loads configuration.

func NewParser

func NewParser() *Parser

NewParser creates a new empty Parser instance.

func NewParserFromBuffer

func NewParserFromBuffer(buf io.Reader) (*Parser, error)

NewParserFromBuffer creates a new Parser by reading the given yaml buffer.

func NewParserFromFile

func NewParserFromFile(fileName string) (*Parser, error)

NewParserFromFile creates a new Parser by reading the given file.

func NewParserFromOperator added in v0.4.0

func NewParserFromOperator(k *koanf.Koanf) *Parser

NewParserFromOperator creates a parser from a koanf.Koanf.

func NewParserFromStringMap

func NewParserFromStringMap(data map[string]any) *Parser

NewParserFromStringMap creates a parser from a map[string]any.

func (*Parser) AllKeys

func (l *Parser) AllKeys() []string

AllKeys returns all keys holding a value, regardless of where they are set. Nested keys are returned with a KeyDelimiter separator.

func (*Parser) Get

func (l *Parser) Get(key string) any

Get can retrieve any value given the key to use.

func (*Parser) IsSet

func (l *Parser) IsSet(key string) bool

IsSet checks to see if the key has been set in any of the data locations. IsSet is case-insensitive for a key.

func (*Parser) LoadFileWithEnv added in v0.4.0

func (l *Parser) LoadFileWithEnv(path string) error

LoadFileWithEnv loads the given file and env, and merges it into the config.

func (*Parser) MergeStringMap

func (l *Parser) MergeStringMap(cfg map[string]any) error

MergeStringMap merges the configuration from the given map with the existing config. Note that the given map may be modified.

func (*Parser) Operator added in v0.2.6

func (l *Parser) Operator() *koanf.Koanf

Operator returns the koanf instance.

func (*Parser) Set

func (l *Parser) Set(key string, value any)

Set sets the value for the key.

func (*Parser) Sub

func (l *Parser) Sub(key string) (*Parser, error)

Sub returns new Parser instance representing a sub-config of this instance. It returns an error is the sub-config is not a map (use Get()) or if none exists.

func (*Parser) ToBytes

func (l *Parser) ToBytes(p koanf.Parser) ([]byte, error)

ToBytes takes a Parser implementation and marshals the config map into bytes, for example, to TOML or JSON bytes.

func (*Parser) ToStringMap

func (l *Parser) ToStringMap() map[string]any

ToStringMap creates a map[string]any from a Parser.

func (*Parser) Unmarshal

func (l *Parser) Unmarshal(key string, dst any) (err error)

Unmarshal specified path config into a struct. Tags on the fields of the structure must be properly set.

func (*Parser) UnmarshalExact

func (l *Parser) UnmarshalExact(key string, intoCfg any) (err error)

UnmarshalExact unmarshals the config into a struct, error if a field is nonexistent.

type TLS added in v0.2.6

type TLS struct {
	CA                 string `json:"ca" yaml:"ca"`
	Cert               string `json:"cert" yaml:"cert"`
	Key                string `json:"key" yaml:"key"`
	InsecureSkipVerify bool   `json:"insecureSkipVerify" yaml:"insecureSkipVerify"`
}

TLS is the TLS configuration for TLS connections TLS content can be file or cert string,depend on your application access notice: TLS is experimental,and only support file path in configuration

func NewTLS added in v0.2.6

func NewTLS(cnf *Configuration) *TLS

NewTLS creates a new TLS configuration. It will initialize the same defaults as the tls.Config struct.

func (*TLS) Apply added in v0.2.6

func (t *TLS) Apply(cnf *Configuration)

func (*TLS) BuildTlsConfig added in v0.2.6

func (t *TLS) BuildTlsConfig() (*tls.Config, error)

Jump to

Keyboard shortcuts

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