config

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2021 License: Apache-2.0 Imports: 14 Imported by: 71

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key string) interface{}

Get can retrieve any value given the key to use. Get is case-insensitive for a key. Get has the behavior of returning the value associated with the first place from where it is set. Viper will check in the following order: override, flag, env, config file, key/value store, default

Get returns an interface. For a specific value use one of the Get____ methods.

func GetBool

func GetBool(key string) bool

GetBool returns the value associated with the key as a bool.

func GetDuration

func GetDuration(key string) time.Duration

GetDuration returns the value associated with the key as a time.Duration.

func GetFloat32

func GetFloat32(key string) float32

GetFloat32 returns the value associated with the key as a float32.

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 returns the value associated with the key as a float64.

func GetInt

func GetInt(key string) int

GetInt returns the value associated with the key as a int.

func GetInt16

func GetInt16(key string) int16

GetInt16 returns the value associated with the key as a int.

func GetInt32

func GetInt32(key string) int32

GetInt32 returns the value associated with the key as a int32.

func GetInt64

func GetInt64(key string) int64

GetInt64 returns the value associated with the key as a int64.

func GetInt8

func GetInt8(key string) int8

GetInt8 returns the value associated with the key as a int.

func GetString

func GetString(key string) string

GetString returns the value associated with the key as a string.

func GetStringSlice

func GetStringSlice(key string) []string

GetStringSlice returns the value associated with the key as a []string.

func GetUint

func GetUint(key string) uint

GetUint returns the value associated with the key as a uint.

func GetUint16

func GetUint16(key string) uint16

GetUint16 returns the value associated with the key as a uint.

func GetUint32

func GetUint32(key string) uint32

GetUint32 returns the value associated with the key as a uint32.

func GetUint64

func GetUint64(key string) uint64

GetUint64 returns the value associated with the key as a uint64.

func GetUint8

func GetUint8(key string) uint8

GetUint8 returns the value associated with the key as a uint.

func IsSet

func 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 Set

func Set(key string, value interface{})

Set sets the value for the key in the override regiser. Set is case-insensitive for a key. Will be used instead of values obtained via flags, config file, ENV, default, or key/value store.

Types

type CustomOption

type CustomOption interface{}

CustomOption must be a pointer to struct.

Here is an example:

type Option struct {
    FirstName string `desc:"Desc for First Name"`
    Age       uint16 `desc:"Desc for Age"`
}

The struct has two fields (with prefix example):

Field       Flag                   ENV                  Key (In config file)
FirstName   --example-first-name   EXAMPLE_FIRST_NAME   example.firstName
Age         --example-age          EXAMPLE_AGE          example.age

When you execute command with `--help`, you can see the help doc of flags and descriptions (From field tag `desc`).

The priority is:

Flag > ENV > Key > The value you set in option

type NirvanaCommand

type NirvanaCommand interface {
	// EnablePlugin enables plugins.
	EnablePlugin(plugins ...Plugin) NirvanaCommand
	// AddOption will fill up options from flags/ENV/config after executing.
	// A non-empty prefix is recommended. It's used to divide option namespaces.
	AddOption(prefix string, options ...CustomOption) NirvanaCommand
	// Add adds a field by key.
	// If you don't have any struct to describe an option, you can use the method to
	// add a single field into nirvana command.
	// `pointer` must be a pointer to golang basic data type (e.g. *int, *string).
	// `key` must a config key. It's like 'nirvana.ip' and 'myconfig.name.firstName'.
	// The key will be converted to flag and env (e.g. --nirvana-ip and NIRVANA_IP).
	// If you want a short flag for the field, you can only set a one-char string.
	// `desc` describes the field.
	Add(pointer interface{}, key string, shortFlag string, desc string) NirvanaCommand
	// Execute runs nirvana server.
	Execute(descriptors ...interface{}) error
	// ExecuteWithConfig runs nirvana server from a custom config.
	ExecuteWithConfig(cfg *nirvana.Config) error
	// Command returns a command for command.
	Command(cfg *nirvana.Config) *cobra.Command
	// SetHook sets nirvana command hook.
	SetHook(hook NirvanaCommandHook)
	// Hook returns nirvana command hook.
	Hook() NirvanaCommandHook
}

NirvanaCommand is a nirvana command.

func NewDefaultNirvanaCommand

func NewDefaultNirvanaCommand() NirvanaCommand

NewDefaultNirvanaCommand creates a nirvana command with default option.

func NewNamedNirvanaCommand

func NewNamedNirvanaCommand(name string, option *Option) NirvanaCommand

NewNamedNirvanaCommand creates a nirvana command with an unique name. Empty name means `server`. Nil option means default option.

func NewNirvanaCommand

func NewNirvanaCommand(option *Option) NirvanaCommand

NewNirvanaCommand creates a nirvana command. Nil option means default option.

type NirvanaCommandHook

type NirvanaCommandHook interface {
	// PreConfigure runs before installing plugins.
	PreConfigure(config *nirvana.Config) error
	// PostConfigure runs after installing plugins and before creating nirvana server.
	PostConfigure(config *nirvana.Config) error
	// PreServe runs before nirvana server serving.
	PreServe(config *nirvana.Config, server nirvana.Server) error
	// PostServe runs after nirvana server shutting down or any error occurring.
	PostServe(config *nirvana.Config, server nirvana.Server, err error) error
}

NirvanaCommandHook provides several hook points for NirvanaCommand.

type NirvanaCommandHookFunc

type NirvanaCommandHookFunc struct {
	PreConfigureFunc  func(config *nirvana.Config) error
	PostConfigureFunc func(config *nirvana.Config) error
	PreServeFunc      func(config *nirvana.Config, server nirvana.Server) error
	PostServeFunc     func(config *nirvana.Config, server nirvana.Server, err error) error
}

NirvanaCommandHookFunc is a helper to generate NirvanaCommandHook. Hook points are optional.

func (*NirvanaCommandHookFunc) PostConfigure

func (h *NirvanaCommandHookFunc) PostConfigure(config *nirvana.Config) error

PostConfigure runs after installing plugins and before creating nirvana server.

func (*NirvanaCommandHookFunc) PostServe

func (h *NirvanaCommandHookFunc) PostServe(config *nirvana.Config, server nirvana.Server, err error) error

PostServe runs after nirvana server shutting down or any error occurring.

func (*NirvanaCommandHookFunc) PreConfigure

func (h *NirvanaCommandHookFunc) PreConfigure(config *nirvana.Config) error

PreConfigure runs before installing plugins.

func (*NirvanaCommandHookFunc) PreServe

func (h *NirvanaCommandHookFunc) PreServe(config *nirvana.Config, server nirvana.Server) error

PreServe runs before nirvana server serving.

type Option

type Option struct {
	// IP is the IP to listen.
	IP string `desc:"Nirvana server listening IP"`
	// Port is the port to listen.
	Port uint16 `desc:"Nirvana server listening Port"`
	// Key is private key for HTTPS.
	Key string `desc:"TLS private key (PEM format) for HTTPS"`
	// Cert is certificate for HTTPS.
	Cert string `desc:"TLS certificate (PEM format) for HTTPS"`
}

Option contains basic configurations of nirvana.

func NewDefaultOption

func NewDefaultOption() *Option

NewDefaultOption creates a default option.

func (*Option) Configure

func (p *Option) Configure(cfg *nirvana.Config) error

Configure configures nirvana config via current option.

func (*Option) Name

func (p *Option) Name() string

Name returns plugin name.

type Plugin

type Plugin interface {
	// Name returns plugin name.
	Name() string
	// Configure configures nirvana config via current options.
	Configure(cfg *nirvana.Config) error
}

Plugin is for plugins to collect configurations

Jump to

Keyboard shortcuts

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