conf

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2018 License: MIT Imports: 5 Imported by: 8

README

conf

GoDoc Build Status Go Report Card

Package conf is an extensible solution for cascading configuration of application. Package conf provides configuration processor that can load configuration layers from different sources and merges them into the one configuration tree. In addition configuration processor can expand variables in string values and process _var and _include directives in resulting configuration tree. Package conf comes with built-in configuration loaders: fileconf and envconf, and can be extended by third-party configuration loaders. Package conf do not watch for configuration changes, but you can implement this feature in the custom configuration loader. See full documentation on GoDoc for more information.

Documentation

Overview

Package conf is an extensible solution for cascading configuration of application. Package conf provides configuration processor, that can load configuration layers from different sources and merges them into the one configuration tree. In addition configuration processor can expand variables in string values and process _var and _include directives in resulting configuration tree (see below). Package conf comes with built-in configuration loaders: fileconf and envconf, and can be extended by third-party configuration loaders. Package conf do not watch for configuration changes, but you can implement this feature in the custom configuration loader.

Configuration processor can expand variables in string values (if you need alias for complex structures see _var directive). Variable names can be absolute or relative. Relative variable names begins with "." (dot). The section, in which a value of relative variable will be searched, determines by number of dots in variable name. For example, we have a YAML file:

myapp:
  mediaFormats: ["images", "audio", "video"]

  dirs:
    rootDir: "/myapp"
    templatesDir: "${myapp.dirs.rootDir}/templates"
    sessionsDir: "${.rootDir}/sessions"

    mediaDirs:
      - "${..rootDir}/media/${myapp.mediaFormats.0}"
      - "${..rootDir}/media/${myapp.mediaFormats.1}"
      - "${..rootDir}/media/${myapp.mediaFormats.2}"

After processing of the file we will get a map:

"myapp": map[string]interface{}{
  "mediaFormats": []interface{}{"images", "audio", "video"},

  "dirs": map[string]interface{}{
    "rootDir":      "/myapp",
    "templatesDir": "/myapp/templates",
    "sessionsDir": "/myapp/sessions",

    "mediaDirs": []interface{}{
      "/myapp/media/images",
      "/myapp/media/audio",
      "/myapp/media/video",
    },
  },
}

To escape variable expansion add one more "$" symbol before variable name.

templatesDir: "$${myapp.dirs.rootDir}/templates"

After processing we will get:

templatesDir: "${myapp.dirs.rootDir}/templates"

Package conf supports two special directives in configuration layers: _var and _include. _var directive assigns configuration parameter value to another configuration parameter. Argument of the _var directive is a variabale name, absolute or relative. Here some example:

myapp:
  db:
    defaultOptions:
      PrintWarn: 0
      PrintError: 0
      RaiseError: 1

    connectors:
      stat:
        host: "stat.mydb.com"
        port: "1234"
        dbname: "stat"
        username: "stat_writer"
        password: "stat_writer_pass"
        options: {_var: "myapp.db.defaultOptions"}

      metrics:
        host: "metrics.mydb.com"
        port: "1234"
        dbname: "metrics"
        username: "metrics_writer"
        password: "metrics_writer_pass"
        options: {_var: "...defaultOptions"}

_include directive loads configuration layer from external sources and assigns it to configuration parameter. Argument of the _include directive is a list of configuration locators.

myapp:
  db:
    defaultOptions:
      PrintWarn:  0
      PrintError: 0
      RaiseError: 1

  connectors: {_include: ["file:connector.yml"]}

You can find full example in repository.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Loader

type Loader interface {
	Load(*Locator) (interface{}, error)
}

Loader is an interface for configuration loaders.

type Locator added in v1.2.1

type Locator struct {
	Loader      string
	BareLocator string
}

Locator is used by configuration processor and loaders to load configuration layers.

func ParseLocator added in v1.2.1

func ParseLocator(rawLoc string) (*Locator, error)

ParseLocator method creates Locator instance from the string.

func (*Locator) String added in v1.2.1

func (l *Locator) String() string

String method convert Locator instance to the string.

type Processor added in v1.2.1

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

Processor loads configuration layers from different sources and merges them into the one configuration tree. In addition configuration processor can expand variables in string values and process _var and _include directives in resulting configuration tree. Processing can be disabled if not needed.

func NewProcessor added in v1.2.1

func NewProcessor(config ProcessorConfig) *Processor

NewProcessor method creates new configuration processor instance.

func (*Processor) Load added in v1.2.1

func (p *Processor) Load(locators ...interface{}) (map[string]interface{}, error)

Load method loads configuration tree using configuration locators. Configuration locator can be a string or a map of type map[string]interface{}. Map type can be used to specify default configuration layers. The merge priority of loaded configuration layers depends on the order of configuration locators. Layers loaded by rightmost locator have highest priority.

type ProcessorConfig added in v1.2.1

type ProcessorConfig struct {
	// Loaders specifies configuration loaders. Map keys reperesents names of
	// configuration loaders, that further can be used in configuration locators.
	Loaders map[string]Loader

	// DisableProcessing disables expansion of variables and processing of directives.
	DisableProcessing bool
}

ProcessorConfig is a structure with configuration parameters for configuration processor.

Directories

Path Synopsis
Package envconf is configuration loader for the conf package.
Package envconf is configuration loader for the conf package.
examples
basic command
Package fileconf is configuration loader for the conf package.
Package fileconf is configuration loader for the conf package.

Jump to

Keyboard shortcuts

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