config

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MIT Imports: 6 Imported by: 1

README

Golang - Config

Quality Gate Status Coverage

Abstraction over Viper to read environment variables.

Environment variables from .env files are also supported.

Installation

  go get -u github.com/mitz-it/golang-config

Usage 1

package main

import "github.com/mitz-it/golang-config"

func main() {
  prefix := "mtz" // all environment variables starting with MTZ_ will be loaded
  start := config.StartConfig{
    ConfigPath: "../config/.env",
    Prefix: prefix,
  }

  cfg := config.NewConfig(start)

  connStr := cfg.Standard.GetString("connection_string") // MTZ_CONNECTION_STRING

  // ...
}

Usage 2

package main

import "github.com/mitz-it/golang-config"

func init() {
  prefix := "mtz" // all environment variables starting with MTZ_ will be loaded
  path := "../config/.env"
  config.LoadEnv(prefix, path)
}

func main() {
  connStr := config.Env.GetString("connection_string") // MTZ_CONNECTION_STRING

  // ...
}

Scopes

package main

import "github.com/mitz-it/golang-config"

func init() {
  config.LoadScopedEnv("scope1", "sc1", "../config/.env")
  config.LoadScopedEnv("scope1", "sc2", "../config/.env")
}

func main() {
  connStr1 := config.Scope("scope1").Env.GetString("connection_string") // SC1_CONNECTION_STRING
  connStr2 := config.Scope("scope2").Env.GetString("connection_string") // SC2_CONNECTION_STRING

  // ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// The viper instance to handle environment variables.
	Standard *v.Viper
}

Defines the instance type to be used to retrieve environment variables.

func NewConfig

func NewConfig(cfg StartConfig) *Config

Creates a new `Config` instance, given a `StartConfig` Loads the .env file with the given path. Automatically add environment variables (.env files included).

type Environment

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

Defines the environment variables reader.

var Env *Environment

Instance to read environment variables.

func LoadEnv

func LoadEnv(prefix, path string) *Environment

Loads the .env file with the given path. Automatically add environment variables (.env files included).

func (*Environment) Get

func (env *Environment) Get(key string) interface{}

Given the right key, it returns any value from an environment variable as an interface.

func (*Environment) GetBool

func (env *Environment) GetBool(key string) bool

Given the key, it returns the environment variable value as bool.

func (*Environment) GetDuration

func (env *Environment) GetDuration(key string) time.Duration

Given the key, it returns the environment variable value as time.Duration.

func (*Environment) GetFloat64

func (env *Environment) GetFloat64(key string) float64

Given the key, it returns the environment variable value as float64.

func (*Environment) GetInt

func (env *Environment) GetInt(key string) int

Given the key, it returns the environment variable value as int.

func (*Environment) GetString

func (env *Environment) GetString(key string) string

Given the key, it returns the environment variable value as a string.

func (*Environment) GetStringMap

func (env *Environment) GetStringMap(key string) map[string]interface{}

Given the right key, it returns the the environment variable value as a map, with string as key and interface as value.

func (*Environment) GetStringSlice

func (env *Environment) GetStringSlice(key string) []string

Given the key, it returns the environment variable value as a string slice.

func (*Environment) GetTime

func (env *Environment) GetTime(key string) time.Time

Given the key, it returns the environment variable value as time.Time.

func (*Environment) Viper

func (env *Environment) Viper() *v.Viper

It returns the Viper instance to handle environment variables functionality

type EnvironmentScope added in v0.0.3

type EnvironmentScope struct {
	Env *Environment
}

func LoadScopedEnv added in v0.0.3

func LoadScopedEnv(key, prefix, path string) (*EnvironmentScope, error)

Creates a new environment variables scope and loads the .env file with the given path into the new scope.

func Scope added in v0.0.3

func Scope(key string) *EnvironmentScope

Retrives the environment scope with the given key.

type StartConfig

type StartConfig struct {
	// Defines a prefix that environment variables will use.
	// If your prefix is mtz, your environment variables should start with MTZ_
	Prefix string
	// The path of the .env file to be loaded.
	// It is relative to the application entrypoint, e.g: main.go.
	ConfigPath string
}

Defines all the necessary input to load environment variables.

Jump to

Keyboard shortcuts

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