appconfig

package module
v0.0.0-...-bcfd264 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2020 License: MIT Imports: 8 Imported by: 0

README

Build Status

appconfig

SpringBoot like profile based application configuration loader.

See confita for details.

Settings

ActiveProfileEnvName

Environment variable name to specify active profile.

Default value is app.profiles.active

appconfig.ActiveProfileEnvName = "app.profiles.active"
ConfigFilenamePrefix

Configuration filename prefix.

Default value is application.

appconfig.ConfigFilenamePrefix = "application"

Load Precedence

  1. Environment variable.
    • Environment variable is read from config tag.
    • If field tag is nested.first-name, field value will be read from nested.first-name, NESTED_FIRST_NAME environment variable if set.
    • List items are separated by comma(,).
  2. Specified configFilename file (passed as LoadConfig() argument)
  3. config/application-{profile}.yaml
  4. application-{profile}.yaml
  5. config/application.yaml
  6. application.yaml
  7. config/application-{profile}.yaml

Example

Suppose we have set environment variables as:

  • FOO_CONFIG_FILE=myconfig.yml
  • foo.profiles.active=dev

The following code will load files in the following order:

  1. myconfig.yml
  2. config/foo-dev.yaml
  3. foo-dev.yaml
  4. config/foo.yaml
  5. foo.yaml
package appconfig

import (
	"fmt"
	"github.com/lechuckroh/appconfig"
	"os"
)

type Config struct {
	Name   string   `config:"name"`
	Age    int      `config:"age"`
	Tags   []string `config:"tags"`
	Nested struct {
		Name string `config:"nested.name"`
		Age  int    `config:"nested.age"`
	}
}

func main() {
	appconfig.ActiveProfileEnvName = "foo.profiles.active"
	appconfig.ConfigFilenamePrefix = "foo"
	configFilename := os.Getenv("FOO_CONFIG_FILE")

	config := Config{}
	loadedFilenames, err := appconfig.LoadConfig(configFilename, &config)
	if err != nil {
		fmt.Printf("Failed to load config: %s\n", err.Error())
		return
	}
	for idx, filename := range loadedFilenames {
		fmt.Printf("Configuration loaded: [%d] %s\n", idx+1, filename)
	}
	if len(loadedFilenames) == 0 {
		fmt.Println("No config file loaded")
	}

	fmt.Printf("Config: %+v\n", config)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ActiveProfileEnvName = "app.profiles.active"

ActiveProfileEnvName represents environment name for active profile

View Source
var ConfigFilenamePrefix = "application"

ConfigFilenamePrefix represents configuration filename prefix

Functions

func LoadConfig

func LoadConfig(configFilename string, to interface{}) ([]string, error)

LoadConfig loads configuration from files or environment variables.

Configuration precedence:

1. Specified 'configFilename' file

2. 'config/application-{profile}.yaml'

3. 'application-{profile}.yaml'

4. 'config/application.yaml'

5. 'application.yaml'

func NewEnvBackend

func NewEnvBackend() backend.Backend

Types

This section is empty.

Jump to

Keyboard shortcuts

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