configer

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: BSD-3-Clause Imports: 16 Imported by: 12

README

the env variable will override default value when set flag default

flag > config > (env) > default

Documentation

Overview

the configer is not thread safe, make sure not use it after call process.Start()

Index

Examples

Constants

View Source
const GlobalKey = "global"

GlobalKey is the name of the Values key that is used for storing global vars.

Variables

View Source
var (
	DefaultConfiger = NewConfiger()
)

Functions

func AddFlags

func AddFlags(fs *pflag.FlagSet)

func Envs

func Envs() []string

func FlagSet added in v0.0.2

func FlagSet(fs *pflag.FlagSet, sample interface{}, opts ...ConfigFieldsOption) error

just for cmdcli

func Flags

func Flags() []string

func ToFloat64Slice added in v0.0.2

func ToFloat64Slice(str string) []float64

func ToFloat64SliceE added in v0.0.2

func ToFloat64SliceE(i interface{}) ([]float64, error)

ToFloat64SliceE casts an interface to a []time.Duration type.

func ToIntSlice added in v0.0.2

func ToIntSlice(str string) []int

func ToString added in v0.0.2

func ToString(v interface{}) string

func ToStringArrayVar added in v0.0.2

func ToStringArrayVar(str string) []string

func ToStringMapString added in v0.0.2

func ToStringMapString(str string) map[string]string

func ValueFiles

func ValueFiles() []string

func Var added in v0.0.2

func Var(fs *pflag.FlagSet, path string, sample interface{}, opts ...ConfigFieldsOption) error

Var set config fields to yaml configfile reader and pflags.FlagSet from sample

func WithConfiger added in v0.0.2

func WithConfiger(parent context.Context, cf ParsedConfiger) context.Context

Types

type ConfigFields added in v0.0.2

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

type ConfigFieldsOption

type ConfigFieldsOption func(*configFieldsOptions)

func WithTags

func WithTags(getter func() map[string]*FieldTag) ConfigFieldsOption

WithTags just for AddConfigs

type Configer

type Configer interface {
	Var(fs *pflag.FlagSet, path string, sample interface{}, opts ...ConfigFieldsOption) error

	Parse(opts ...ConfigerOption) (ParsedConfiger, error)
	AddFlags(fs *pflag.FlagSet)
	ValueFiles() []string
	Envs() []string
	Flags() []string
}

func NewConfiger

func NewConfiger() Configer

type ConfigerOption

type ConfigerOption func(*ConfigerOptions)

func WithDefault

func WithDefault(path string, sample interface{}) ConfigerOption

with config object

func WithDefaultYaml

func WithDefaultYaml(path, yaml string) ConfigerOption

with config yaml

func WithEnv

func WithEnv(allowEnv, allowEmptyEnv bool) ConfigerOption

func WithMaxDepth

func WithMaxDepth(maxDepth int) ConfigerOption

func WithOverride

func WithOverride(path string, sample interface{}) ConfigerOption

func WithOverrideYaml

func WithOverrideYaml(path, yaml string) ConfigerOption

func WithValueFile

func WithValueFile(valueFiles ...string) ConfigerOption

WithValueFile priority greater than --values

type ConfigerOptions

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

func (*ConfigerOptions) Validate

func (p *ConfigerOptions) Validate() error

type ErrNoTable

type ErrNoTable struct {
	Key string
}

ErrNoTable indicates that a chart does not have a matching table.

func (ErrNoTable) Error

func (e ErrNoTable) Error() string

type ErrNoValue

type ErrNoValue struct {
	Key string
}

ErrNoValue indicates that Values does not contain a key with a value

func (ErrNoValue) Error

func (e ErrNoValue) Error() string

type FieldTag

type FieldTag struct {
	Flag        []string // flag:"{long},{short}"
	Arg         string   // arg:"{arg}"  args[0] arg1... -- arg2... (deprecated)
	Default     string   // default:"{default}"
	Env         string   // env:"{env}"
	Description string   // description:"{description}"
	Deprecated  string   // deprecated:""
	// contains filtered or unexported fields
}

func GetFieldTag

func GetFieldTag(sf reflect.StructField) (tag *FieldTag)

func (FieldTag) Skip

func (p FieldTag) Skip() bool

func (FieldTag) String

func (p FieldTag) String() string

type ParsedConfiger added in v0.0.2

type ParsedConfiger interface {
	ValueFiles() []string
	Envs() []string
	Flags() []string

	//FlagSet() *pflag.FlagSet
	Set(path string, v interface{}) error
	GetConfiger(path string) ParsedConfiger
	GetRaw(path string) interface{}
	GetString(path string) string
	GetBool(path string) (bool, error)
	GetBoolDef(path string, def bool) bool
	GetFloat64(path string) (float64, error)
	GetFloat64Def(path string, def float64) float64
	GetInt64(path string) (int64, error)
	GetInt64Def(path string, def int64) int64
	GetInt(path string) (int, error)
	GetIntDef(path string, def int) int
	IsSet(path string) bool
	Read(path string, into interface{}) error
	String() string
}

func ConfigerFrom added in v0.0.2

func ConfigerFrom(ctx context.Context) (ParsedConfiger, bool)

func ConfigerMustFrom added in v0.0.2

func ConfigerMustFrom(ctx context.Context) ParsedConfiger

func Parse added in v0.0.2

func Parse(opts ...ConfigerOption) (ParsedConfiger, error)
Example
package main

import (
	"fmt"

	"github.com/yubo/golib/configer"
)

type User struct {
	Name string `flag:"name"`
}

func main() {
	c, err := configer.NewConfiger().Parse()
	fmt.Printf("%s", c)
	fmt.Printf("%v", err)
}
Output:

{}
<nil>

type ReleaseOptions

type ReleaseOptions struct {
	Name      string
	Namespace string
	Revision  int
	IsUpgrade bool
	IsInstall bool
}

ReleaseOptions represents the additional release options needed for the composition of the final values struct

type Values

type Values map[string]interface{}

Values represents a collection of chart values.

func ReadValues

func ReadValues(data []byte) (vals Values, err error)

ReadValues will parse YAML byte data into a Values.

func ReadValuesFile

func ReadValuesFile(filename string) (Values, error)

ReadValuesFile will parse a YAML file into a map of values.

func (Values) AsMap

func (v Values) AsMap() map[string]interface{}

AsMap is a utility function for converting Values to a map[string]interface{}.

It protects against nil map panics.

func (Values) Encode

func (v Values) Encode(w io.Writer) error

Encode writes serialized Values information to the given io.Writer.

func (Values) PathValue

func (v Values) PathValue(path string) (interface{}, error)

PathValue takes a path that traverses a YAML structure and returns the value at the end of that path. The path starts at the root of the YAML structure and is comprised of YAML keys separated by periods. Given the following YAML data the value at path "chapter.one.title" is "Loomings".

chapter:
  one:
    title: "Loomings"

func (Values) String added in v0.0.2

func (v Values) String() string

func (Values) Table

func (v Values) Table(name string) (Values, error)

Table gets a table (YAML subsection) from a Values object.

The table is returned as a Values.

Compound table names may be specified with dots:

foo.bar

The above will be evaluated as "The table bar inside the table foo".

An ErrNoTable is returned if the table does not exist.

func (Values) YAML

func (v Values) YAML() (string, error)

YAML encodes the Values into a YAML string.

Jump to

Keyboard shortcuts

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