cvc

package module
v0.0.0-...-11ab557 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2019 License: GPL-3.0 Imports: 20 Imported by: 0

README

cvc

cvc ≈ cobra + viper

Basic Usage

See example/basic.go

$ go run examples/basic.go -h
naru is API server for SEBAK

Usage:
  naru [flags]

Flags:
  -h, --help                help for naru
      --log-file string     log output file (default "naru.log")
      --log-format string   log format {terminal json} (default "terminal")
      --log-level string    log level {debug error warn crit} (default "debug")
      --set-int int         set integer (default 100)
      --set-string string   set string (default "find me")
      --verbose             verbose
$ go run example/basic.go --log-file /naru.log --log-format json --log-level error --verbose --set-int 3 --set-string 'show me'

# loaded config:
{
  "Verbose": true,
  "SetString": "show me",
  "SetInt": 3,
  "Log": {
    "File": "/naru.log",
    "Level": "error",
    "Format": "json"
  }
}

Documentation

Index

Constants

View Source
const (
	ErrorMethodNotFoundCode
	ErrorInvalidMethodCode
)

Variables

View Source
var (
	ErrorMethodNotFound, _ = NewError(ErrorMethodNotFoundCode, "method not found")
	ErrorInvalidMethod, _  = NewError(ErrorInvalidMethodCode, "invalid method found")
)

Functions

func CallMergeFunc

func CallMergeFunc(f StructMethod) error

func CallParseFunc

func CallParseFunc(f StructMethod, i interface{}) (interface{}, error)

func CallValidateFunc

func CallValidateFunc(f StructMethod) error

func GetFlagValue

func GetFlagValue(item *Item) (reflect.Value, error)

func GetKeysFromViperConfig

func GetKeysFromViperConfig(group string, format string, v *viper.Viper, r io.Reader) ([]string, error)

func NormalizeVar

func NormalizeVar(s string, sep string) string

func SetLogging

func SetLogging(level logging.Lvl, handler logging.Handler)

Types

type BaseGroup

type BaseGroup struct{}

func (*BaseGroup) Merge

func (b *BaseGroup) Merge() error

func (*BaseGroup) ThisIsGroup

func (b *BaseGroup) ThisIsGroup()

func (*BaseGroup) Validate

func (b *BaseGroup) Validate() error

type Error

type Error struct {
	sync.RWMutex
	Code    int
	Message string
	Extra   map[string]interface{}
}

func NewError

func NewError(code int, message string, extras ...interface{}) (Error, error)

func (Error) Clone

func (e Error) Clone() *Error

func (*Error) Equal

func (e *Error) Equal(i error) bool

func (Error) Error

func (e Error) Error() string

func (Error) JSON

func (e Error) JSON() (o map[string]interface{})

func (*Error) Set

func (e *Error) Set(k string, v interface{}) *Error

type Group

type Group interface {
	ThisIsGroup()
	Validate() error
	Merge() error
}

type Item

type Item struct {
	FieldName string
	Value     reflect.Value
	Group     *Item
	Children  []*Item
	Tag       reflect.StructTag
	Input     interface{}
	IsGroup   bool
	ViperName string
}

func (*Item) EnableFlag

func (c *Item) EnableFlag() bool

func (*Item) EnvName

func (c *Item) EnvName(prefix string) string

func (*Item) FlagName

func (c *Item) FlagName() string

func (*Item) FullName

func (c *Item) FullName() string

func (*Item) Merge

func (c *Item) Merge() (string, error)

func (*Item) Name

func (c *Item) Name() string

func (*Item) Parse

func (c *Item) Parse(i interface{}) (interface{}, error)

func (*Item) ParseEnv

func (c *Item) ParseEnv(i string) (interface{}, error)

func (Item) String

func (c Item) String() string

func (*Item) Validate

func (c *Item) Validate() (string, error)

type Manager

type Manager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewManager

func NewManager(name string, c interface{}, cmd *cobra.Command, v *viper.Viper) *Manager

func (*Manager) Cobra

func (m *Manager) Cobra() *cobra.Command

func (*Manager) Config

func (m *Manager) Config() interface{}

func (*Manager) ConfigPprint

func (m *Manager) ConfigPprint() (o []interface{})

func (*Manager) ConfigString

func (m *Manager) ConfigString() string

func (*Manager) EnvName

func (m *Manager) EnvName(item *Item) string

func (*Manager) Envs

func (m *Manager) Envs() []string

func (*Manager) FlagSet

func (m *Manager) FlagSet() *pflag.FlagSet

func (*Manager) Get

func (m *Manager) Get(key string) (*Item, bool)

func (*Manager) GetValue

func (m *Manager) GetValue(key string, i interface{}) error

func (*Manager) Group

func (m *Manager) Group() string

func (*Manager) Groups

func (m *Manager) Groups() []string

func (*Manager) ItemByFlag

func (m *Manager) ItemByFlag(flag string) (*Item, bool)

func (*Manager) Map

func (m *Manager) Map() map[string]*Item

func (*Manager) Merge

func (m *Manager) Merge() (string, error)

func (*Manager) MergeFromEnv

func (m *Manager) MergeFromEnv() (string, error)

func (*Manager) MergeFromFlags

func (m *Manager) MergeFromFlags() (string, error)

func (*Manager) MergeFromViper

func (m *Manager) MergeFromViper() (string, error)

func (*Manager) Root

func (m *Manager) Root() *Item

func (*Manager) SetEnvLookupFunc

func (m *Manager) SetEnvLookupFunc(fn func(string) (string, bool))

func (*Manager) SetRaw

func (m *Manager) SetRaw(key string, i interface{}) error

func (*Manager) SetUseEnv

func (m *Manager) SetUseEnv(s bool)

func (*Manager) SetValue

func (m *Manager) SetValue(key string, i interface{}) error

func (*Manager) SetViperConfig

func (m *Manager) SetViperConfig(format string, b []byte) error

func (*Manager) SetViperConfigFile

func (m *Manager) SetViperConfigFile(fs ...string) error

func (*Manager) UseEnv

func (m *Manager) UseEnv() bool

func (*Manager) Viper

func (m *Manager) Viper() *viper.Viper

func (*Manager) ViperString

func (m *Manager) ViperString(format string) (string, error)

type StructMethod

type StructMethod struct {
	Func reflect.Value
	Body reflect.Value
}

func GetFuncFromItem

func GetFuncFromItem(item *Item, name string, numIn, numOut int) []StructMethod

func GetFuncFromItemStruct

func GetFuncFromItemStruct(item *Item, name string, numIn, numOut int) []StructMethod

func GetMethodByName

func GetMethodByName(i interface{}, name string, numIn, numOut int) (m StructMethod, found bool)

func (StructMethod) Call

func (m StructMethod) Call(args ...reflect.Value) []reflect.Value

func (StructMethod) Empty

func (m StructMethod) Empty() bool

func (StructMethod) In

func (m StructMethod) In(i int) reflect.Type

func (StructMethod) NumIn

func (m StructMethod) NumIn() int

func (StructMethod) NumOut

func (m StructMethod) NumOut() int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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