config

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

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

Go to latest
Published: Nov 11, 2022 License: Apache-2.0 Imports: 7 Imported by: 8

Documentation

Overview

Package config exposes an opinionated loader for configuration files.

This package exposes a Loadable interface that knows how to load and merge configuration files.

The resulting configuration is made available to consuming applications as a *viper.Viper configuration registry.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// DefaultPathEnv is the default environment variable used
	// to configure the search path for configurations.
	DefaultPathEnv = "CONFIG_DIR"

	// DefaultConfigRadix is the default basename for a configuration file.
	DefaultConfigRadix = "config"

	// DefaultSecretRadix is the default basename for a secrets file.
	DefaultSecretRadix = "secrets"

	// DefaultEnvPath is the default folder for environment-specific configurations, relative to the base path.
	DefaultEnvPath = "config.d"

	// DefaultSecretSuffix is the default optional suffix to look for unencrypted secrets.
	DefaultSecretSuffix = "dec"
)

Functions

func GetenvOrDefault

func GetenvOrDefault(key, defaultValue string) string

GetenvOrDefault wraps os.Getenv, applying a default value if the environment variable "key" is undefined or empty.

func Load

func Load(env string, opts ...Option) (*viper.Viper, error)

Load configuration files. The merged outcome is available in a *viper.Viper registry.

func LoadForTest

func LoadForTest(env string, opts ...Option) (*viper.Viper, error)

LoadForTest combines configs from a test loader.

This is a convenient wrapper around LoadForEnv for a configuration composed both regular and secrets files.

Typically, test programs would get a configuration from some "test" environment like so:

Usage:

v, err := config.LoadForTest("test")

func LoadWithSecrets

func LoadWithSecrets(env string, opts ...Option) (*viper.Viper, error)

LoadWithSecrets combines configuration files from a default loader with config from (unencrypted) secrets.

This is a convenient wrapper around LoadForEnv for a configuration composed both regular and secrets files.

Just like

Usage:

v, err := config.LoadWithSecrets("dev")
Example
package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/fredbi/go-cli/config"
)

func main() {
	// loads a config, merge clear-text secrets, then save the result file.

	var err error
	defer func() {
		if err != nil {
			log.Fatal(err)
		}
	}()

	folder, err := os.MkdirTemp("", "")
	if err != nil {
		err = fmt.Errorf("creating temp dir: %w", err)

		return
	}

	defer func() {
		_ = os.RemoveAll(folder)
	}()

	here, err := os.Getwd()
	if err != nil {
		err = fmt.Errorf("get current working dir: %w", err)

		return
	}

	os.Setenv("CONFIG_DIR", filepath.Join(here, "examples"))

	// load and merge configuration files
	cfg, err := config.LoadWithSecrets("dev", config.WithMute(true))
	if err != nil {
		err = fmt.Errorf("loading config: %w", err)

		return
	}

	// writes down the merged config
	configmap := filepath.Join(folder, "configmap.yaml")
	err = cfg.WriteConfigAs(configmap)
	if err != nil {
		err = fmt.Errorf("writing config: %w", err)

		return
	}

	result, err := os.ReadFile(configmap)
	if err != nil {
		err = fmt.Errorf("reading result: %w", err)

		return
	}

	_, _ = os.Stdout.Write(result)

}
Output:

app:
    threads: 10
    url: https://example.dev.co
log:
    level: info
metrics:
    enabled: true
    exporter: prometheus
secrets:
    token: xyz
trace:
    enabled: true
    exporter: jaeger

Types

type CombinedLoader

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

CombinedLoader loads and merge configuration files using a collection of loaders.

func NewCombinedLoader

func NewCombinedLoader(loaders ...Loadable) *CombinedLoader

NewCombinedLoader builds a compound loader considering several Loadable in the provided order.

func (*CombinedLoader) LoadForEnv

func (c *CombinedLoader) LoadForEnv(env string) (*viper.Viper, error)

type Loadable

type Loadable interface {
	LoadForEnv(env string) (*viper.Viper, error)
}

Loadable knows how to load a configuration for an environment.

func DefaultLoader

func DefaultLoader(opts ...Option) Loadable

DefaultLoader provides a config loader with all defaults.

* The base path from where to search for configuration files is provided by the environment variable "CONFIG_DIR" * Considered root config files are of the form "config.{json|yaml|yml}" * Environment-specific config files are found in the "config.d" folder. * Considered environment-specific files are of the form "config[.*].{json|yaml|yml}". * Environment-specific files are located in the {base path}/config.d/{env} folder. * Files are watched for changes. * Config loading logging goes to os.Stdout.

Options may be provided to override some of these defaults.

func LoaderForTest

func LoaderForTest(opts ...Option) Loadable

LoaderForTest provides a default config loader intended to be used by test programs.

It mutes internal logging to limit unwanted test verbosity. Config watch is disabled: this is convenient when loading many configuration files in different test go routines.

The LoaderForTest searches for configuration files in the tree containing the current working directory, meaning that any test program in your source tree may load a test config.

func LoaderWithSecrets

func LoaderWithSecrets(opts ...Option) Loadable

LoaderWithSecrets combines configuration files and secrets.

func SecretsLoader

func SecretsLoader(opts ...Option) Loadable

SecretsLoader works like the default loader, but includes config files of the form "secrets.{env}.{json|yaml|yml}[.dec]".

This allows to work with files containing secrets, conventionally named "secrets[.*].[json|yaml|yml]". Secret files may possibly be temporarily decrypted with a ".dec" extension: this allows to work on a local testing environment with secret configurations managed by sops.

type Loader

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

Loader loads and merge configuration files.

func NewLoader

func NewLoader(opts ...Option) *Loader

NewLoader creates a new loader for config files.

func (*Loader) LoadForEnv

func (l *Loader) LoadForEnv(env string) (*viper.Viper, error)

type Option

type Option func(*options)

Option configuring the config loader.

func WithBasePath

func WithBasePath(pth string) Option

func WithBasePathFromEnvVar

func WithBasePathFromEnvVar(variable string) Option

WithBasePathFromEnv specifies the environment variable to be used to define the search path for configs (default: none).

func WithEnvDir

func WithEnvDir(dir string) Option

WithEnvDir defines the path to environment-specific configs, relative to the base path.

Defaults to "config.d"

func WithMute

func WithMute(mute bool) Option

WithMute discards any logging occuring inside the config loader.

The default is false.

This option is equivalent to WithOutput({io.Discard|os.Stdout}).

func WithOutput

func WithOutput(output io.Writer) Option

WithOutput specifies a io.Writer to output logs during config search.

The default is os.Stdout.

func WithRadix

func WithRadix(radix string) Option

WithRadix defines the radix (base name) of a config file.

Default to "config", meaning that recognized file are: "config.yaml", "config.yml", "config.json", or forms such as "config.xxx.{json|yaml|yml}.

func WithSearchParentDir

func WithSearchParentDir(enabled bool) Option

WithSearchParentDir enables the search for config files in the folder tree that contains the current working directory.

This is primarily designed to support test programs loading config files from a source repository.

func WithSuffix

func WithSuffix(suffix string) Option

WithSuffix defines an optional suffix extension to be recognized.

The loader will primarily search for files with the suffix extension, then fall back to no suffix.

Example:

WithSuffix("dec") will look first for "secrets.yaml.dec",
then if none is found, will search for "secrets.yaml".

The default is empty.

func WithWatch

func WithWatch(canWatch bool) Option

WithWatch enables the configuration watch.

The default is true.

Jump to

Keyboard shortcuts

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