confighelper

package
v0.0.0-...-7b190fc Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

README

Generic Configuration Package

This package provides a generic configuration system using the Viper library, allowing users to define their own configuration prefix and provide default configuration.

Usage

Step 1: Create a default YAML configuration file

Create a YAML file that contains the default configuration for your project. For example, let's call it defaults.yaml:

db:
  host: "localhost"
  user: "postgres"
  # password: "" # no default, panic if not set
  name: "cc_terrarium"
  port: 5432
  ssl_mode: false
Step 2: Initialize the configuration

Import the config package and initialize the configuration by providing the prefix and default YAML configuration data:

package main

import (
  "fmt"
  "embed"
  "log"

  "github.com/<username>/<package>/config"
)

//go:embed defaults.yaml
var defaultsYamlFile embed.FS

func main() {
  defaultsYaml, err := defaultsYamlFile.ReadFile("defaults.yaml")
  if err != nil {
    log.Fatal(err)
  }

  defaultsMap := map[string]interface{}{}
  err = yaml.Unmarshal(defaultsYaml, &defaultsMap)
  if err != nil {
    log.Fatal(err)
  }

  cfg, err := config.LoadDefaults(defaultsMap, "TR")
  if err != nil {
    log.Fatal(err)
  }

  // Use the configuration...
  host := viper.GetString("db.host")
  fmt.Println("Host:", host)
}
Step 3: Set configuration values using environment variables

To override the default configuration values, you can set environment variables using the defined prefix and underscore separator. For example, to override the db.host configuration value, you can set the environment variable TR_DB_HOST.

Step 4: Use the configuration values

You can access the configuration values using the Viper library and dot notation. For example, to access the db.host value, use:

host := viper.GetString("db.host")

Refer to the Viper library documentation for more information on accessing configuration values.

Panic if not set

Use the Must... helper functions to panic if the value is not set. This is specially useful to help assert that the required values are set by the user. Example:

host := config.MustGetString("db.host")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadDefaults

func LoadDefaults(defaults map[string]interface{}, prefix string)

LoadDefaults loads the default configuration values provided as a map into Viper. It sets the environment prefix and key replacer for Viper to handle environment variables.

func MustGet

func MustGet[T comparable](key string, getter func(string) T) T

MustGet retrieves the value associated with the given key using the provided getter function from Viper. It panics if the value is not found.

func MustGetAny

func MustGetAny(key string) interface{}

MustGetAny retrieves the value associated with the given key as an interface{} from Viper. It uses the MustGet function to retrieve the value and panics if the value is not found. The retrieved value is returned as an interface{} type, allowing flexibility in handling different types of values.

func MustGetBool

func MustGetBool(key string) bool

MustGetBool retrieves the value associated with the given key as a bool from Viper. It uses the MustGet function to retrieve the value and panics if the value is not found.

func MustGetFloat64

func MustGetFloat64(key string) float64

MustGetFloat64 retrieves the value associated with the given key as a float64 from Viper. It uses the MustGet function to retrieve the value and panics if the value is not found.

func MustGetInt

func MustGetInt(key string) int

MustGetInt retrieves the value associated with the given key as an int from Viper. It uses the MustGet function to retrieve the value and panics if the value is not found.

func MustGetInt64

func MustGetInt64(key string) int64

MustGetInt64 retrieves the value associated with the given key as an int64 from Viper. It uses the MustGet function to retrieve the value and panics if the value is not found.

func MustGetOrError

func MustGetOrError[T comparable](key string, getter func(string) T) (val T, err error)

MustGetOrError retrieves the value associated with the given key using the provided getter function from Viper. It returns an error if the value is not found.

func MustGetString

func MustGetString(key string) string

MustGetString retrieves the value associated with the given key as a string from Viper. It uses the MustGet function to retrieve the value and panics if the value is not found.

Types

This section is empty.

Jump to

Keyboard shortcuts

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