package
0.3.2
Repository: https://github.com/raystack/salt.git
Documentation: pkg.go.dev

# README

config

This package is an wrapper over viper with opinionated defaults that allows loading config from a yaml file and environment variables.

Usage

package main

import (
	"encoding/json"
	"fmt"

	"github.com/raystack/salt/config"
)

type Config struct {
	Port     int            `mapstructure:"port" default:"8080"`
	DB       DBConfig       `mapstructure:"db"`
	NewRelic NewRelicConfig `mapstructure:"new_relic"`
	LogLevel string         `mapstructure:"log_level" default:"info"`
}

type DBConfig struct {
	Port int    `mapstructure:"port" default:"5432"`
	Host string `mapstructure:"host" default:"localhost"`
}

type NewRelicConfig struct {
	Enabled bool   `mapstructure:"enabled" default:"false"`
	AppName string `mapstructure:"app_name" default:"test-app"`
	License string `mapstructure:"license"`
}

func main() {
	var c Config
	l := config.NewLoader(
		// config.WithViper(viper.New()), // default
		// config.WithName("config"), // default
		// config.WithType("yaml"), // default
		// config.WithEnvKeyReplacer(".", "_"), // default
		config.WithPath("$HOME/.test"),
		config.WithEnvPrefix("CONFIG"),
	)

	if err := l.Load(&c); err != nil { // pass pointer to the struct into which you want to load config
		panic(err)
	}
	s, _ := json.MarshalIndent(c, "", "  ") // spaces: 2 | tabs: 1 😛
	fmt.Println(string(s))
}

In the above program a YAML file or environment variables can be used to configure.

port: 9000
db:
  port: 5432
  host: db-host-yaml
new_relic:
  enabled: true
  app_name: config-test-yaml
  license: ____LICENSE_STRING_OF_40_CHARACTERS_____
log_level: debug

or

export CONFIG_PORT=9001
export CONFIG_DB_PORT=5432
export CONFIG_DB_HOST=db-host-env
export CONFIG_NEW_RELIC_ENABLED=true
export CONFIG_NEW_RELIC_APP_NAME=config-test-env
export CONFIG_NEW_RELIC_LICENSE=____LICENSE_STRING_OF_40_CHARACTERS_____
export CONFIG_LOG_LEVEL=debug

or a mix of both.

Configs set in environment will override the ones set as default and in yaml file.

TODO

  • function to print/return config keys in yaml path and env format with defaults as helper
  • add support for flags

# Functions

NewLoader returns a config loader with given LoaderOption(s).
StringToJsonFunc is a mapstructure.DecodeHookFunc that converts a string to an interface{} if the string is valid json.
WithBindPFlags binds viper to pflags based on the tags in the struct.
WithDecoderConfigOption sets the decoder config options for viper.
WithEnvKeyReplacer sets the `old` string to be replaced with the `new` string environmental variable to a key that does not match it.
WithEnvPrefix sets the prefix for keys when checking for configs in environment variables.
WithFile explicitly defines the path, name and extension of the config file.
WithName sets the file name of the config file without the extension.
WithPath adds config path to search the config file in, can be used multiple times to add multiple paths to search.
WithType sets the type of the configuration e.g.
WithViper sets the given viper instance for loading configs instead of the default configured one.

# Structs

ConfigFileNotFoundError is returned when the config file is not found Viper will load from env or defaults.
No description provided by the author

# Type aliases

No description provided by the author