config

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2020 License: MIT Imports: 18 Imported by: 13

Documentation

Overview

Package config package helps manage application configuration:

  1. Each package do not need store and parse configuration
  2. Application only need one line code to get configuration support, no matter how many packages pulled.
  3. Currently support configuration file only, later can store configuration in a dedicate config server, suitable for micro service structure.
  4. Monitor SIGUSR1, on SIGUSR1 reload configuration file, and apply to each package.
  5. Works with life package in mind. Only allow first init in life.Initing phase, and not reload configuration in Shutingdown phase.

Note: no order and dependency sort, although in current implementation, Init() called by Register order, which probabbly is go package dependency order, they maybe change in future implementation. The point is each package must work independently, matches the concept of life package: no order in life.Initing phase, just get the configuration, delay the work to Starting phase.

If .Load() not called, all packages inited() with its default option, and no reload() support.

Index

Constants

This section is empty.

Variables

View Source
var (

	// Flag is config file command line flag, add Flag to your cli.App.Flags.
	Flag cli.Flag = &cli.StringFlag{
		Name:        "config, c",
		Usage:       "specify config file",
		Destination: &filename,
	}

	// FlagDumpDefaultOptions dump default options to stdout if enabled, add to your cli.App.Flags.
	FlagDumpDefaultOptions cli.Flag = &cli.BoolFlag{
		Name:        "dumpDefaultOptions",
		Usage:       "Dump default options to stdout, save it as config file",
		Destination: &dumpDefaultOptions,
	}
)

Functions

func DumpDefaultOptions

func DumpDefaultOptions() (string, error)

DumpDefaultOptions dump default options in config file format. All options are comment out.

func Load

func Load(configFile string) (err error)

Load config file manually. Normally loading config file is triggered by life package before life starting. For non-daemon applications, do not start life cycle, calling Load() manually in this case. Load can only be called once, it will fail if calling config.Load() and life.Start().

Note: Use alternative config filename in server mode is not supported.

If configFile is empty, use default config file resolve process, and this is recommended.

func Register

func Register(name string, fnCreateDefault OptionCreator)

Register an option object. Panic on name conflict. fnCreateDefault is a func create default value, must not return nil. fnCreateDefault may called multiple times, each time should create a new instance. Registered options not reset by spork/testing/reset package.

func Reload

func Reload()

Reload config file and apply changed configurations.

func ResetInternal

func ResetInternal()

ResetInternal reset config package internal state, all registered options lost. It is mainly used in unit tests of config package itself.

func SetDefaultOptionForTest

func SetDefaultOptionForTest(pkg string, op Option)

SetDefaultOptionForTest override default option object.

A default option is created by default option creator function, by calling SetDefaultOption(), override default option for unit tests.

Types

type Option

type Option interface {
	// Called on configuration first load, and ensure it will during life.Initing
	// phase. If any error returned, abort following Init() and return this error.
	Init() error

	// Called on reload configuration, do not allow return error. If the new
	// config can not apply, log to log.
	Apply()
}

Option is the interface config package manages. Must implement as a struct, and can be marshaled as toml, currently github.com/BurntSushi/toml.

type OptionCreator

type OptionCreator func() Option

The OptionCreator is a factory function to create option interface

Jump to

Keyboard shortcuts

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