goconfig

package module
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: MIT Imports: 4 Imported by: 0

README

goconfig

How to manage configurations in Golang

Download

go get github.com/karamelsoft/goconfig

Usage

Definitions
type Configuration interface {

    GetString(name string, defaultSupplier func() string) string
    GetStringOrCrash(name string) string
    GetStringArray(name string, defaultSupplier func() []string) []string
    GetStringArrayOrCrash(name string) []string
    
    GetBool(name string, defaultSupplier func() bool) bool
    GetBoolOrCrash(name string) bool
    GetBoolArray(name string, defaultSupplier func() []bool) []bool
    GetBoolArrayOrCrash(name string) []bool
    
    GetInt(name string, defaultSupplier func() int) int
    GetIntOrCrash(name string) int
    GetIntArray(name string, defaultSupplier func() []int) []int
    GetIntArrayOrCrash(name string) []int
    
    GetInt32(name string, defaultSupplier func() int32) int32
    GetInt32OrCrash(name string) int32
    GetInt32Array(name string, defaultSupplier func() []int32) []int32
    GetInt32ArrayOrCrash(name string) []int32
    
    GetInt64(name string, defaultSupplier func() int64) int64
    GetInt64OrCrash(name string) int64
    GetInt64Array(name string, defaultSupplier func() []int64) []int64
    GetInt64ArrayOrCrash(name string) []int64
    
    GetFloat32(name string, defaultSupplier func() float32) float32
    GetFloat32OrCrash(name string) float32
    GetFloat32Array(name string, defaultSupplier func() []float32) []float32
    GetFloat32ArrayOrCrash(name string) []float32
    
    GetFloat64(name string, defaultSupplier func() float64) float64
    GetFloat64OrCrash(name string) float64
    GetFloat64Array(name string, defaultSupplier func() []float64) []float64
    GetFloat64ArrayOrCrash(name string) []float64
}
Implementations
Environment
config := goconfig.NewEnvConfiguration()
InMemory (Testing purpose)
config := goconfig.NewInMemoryConfiguration()
YAML
URL: "www.google.com"
PORT: "80"
config := goconfig.NewYamlConfiguration(filePath)
Fallback

A fallback configuration has a primary and a secondary configuration.

config := goconfig.NewFallbackConfiguration(primary, secondary)
Default

The default configuration is using Fallback configuration to provide an interesting hierarchy of configurations.

  1. Environment, the environment will always win!
  2. A YAML configuration as Docker secrets called "configuration.yaml", only if exists.
  3. A YAML configuration next to the program called "configuration.yaml", only if exists.
  4. A YAML configuration in the home directory called "configuration.yaml", only if exists.
package goconfig

const (
	DefaultConfigurationFile = "configuration.yaml"
	DefaultDockerSecretsFile = "/run/secrets/" + DefaultConfigurationFile
)

func NewDefaultConfiguration() *FallbackConfiguration {
	log.Println("creating default configuration")
	homeDirectory, _ := os.UserHomeDir()
	return NewFallbackConfiguration(
		environment.NewConfiguration(),
		NewFallbackConfiguration(
			file.NewConfigurationIfExists(DefaultDockerSecretsFile),
			NewFallbackConfiguration(
				file.NewConfigurationIfExists(DefaultConfigurationFile),
				file.NewConfigurationIfExists(homeDirectory + "/" + DefaultConfigurationFile),
			),
		),
	)
}

Documentation

Index

Constants

View Source
const (
	DefaultConfigurationFile = "configuration.yaml"
	DefaultDockerSecretsFile = "/run/secrets/" + DefaultConfigurationFile
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Configuration

type Configuration interface {
	GetString(name string, defaultSupplier func() string) string
	GetStringOrCrash(name string) string
	GetStringArray(name string, defaultSupplier func() []string) []string
	GetStringArrayOrCrash(name string) []string

	GetBool(name string, defaultSupplier func() bool) bool
	GetBoolOrCrash(name string) bool
	GetBoolArray(name string, defaultSupplier func() []bool) []bool
	GetBoolArrayOrCrash(name string) []bool

	GetInt(name string, defaultSupplier func() int) int
	GetIntOrCrash(name string) int
	GetIntArray(name string, defaultSupplier func() []int) []int
	GetIntArrayOrCrash(name string) []int

	GetInt32(name string, defaultSupplier func() int32) int32
	GetInt32OrCrash(name string) int32
	GetInt32Array(name string, defaultSupplier func() []int32) []int32
	GetInt32ArrayOrCrash(name string) []int32

	GetInt64(name string, defaultSupplier func() int64) int64
	GetInt64OrCrash(name string) int64
	GetInt64Array(name string, defaultSupplier func() []int64) []int64
	GetInt64ArrayOrCrash(name string) []int64

	GetFloat32(name string, defaultSupplier func() float32) float32
	GetFloat32OrCrash(name string) float32
	GetFloat32Array(name string, defaultSupplier func() []float32) []float32
	GetFloat32ArrayOrCrash(name string) []float32

	GetFloat64(name string, defaultSupplier func() float64) float64
	GetFloat64OrCrash(name string) float64
	GetFloat64Array(name string, defaultSupplier func() []float64) []float64
	GetFloat64ArrayOrCrash(name string) []float64
}

type FallbackConfiguration added in v0.4.0

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

func NewDefaultConfiguration added in v0.4.0

func NewDefaultConfiguration() *FallbackConfiguration

func NewFallbackConfiguration added in v0.4.0

func NewFallbackConfiguration(primary, secondary Configuration) *FallbackConfiguration

func (*FallbackConfiguration) GetBool added in v0.5.0

func (c *FallbackConfiguration) GetBool(name string, defaultSupplier func() bool) bool

func (*FallbackConfiguration) GetBoolArray added in v0.5.0

func (c *FallbackConfiguration) GetBoolArray(name string, defaultSupplier func() []bool) []bool

func (*FallbackConfiguration) GetBoolArrayOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetBoolArrayOrCrash(name string) []bool

func (*FallbackConfiguration) GetBoolOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetBoolOrCrash(name string) bool

func (*FallbackConfiguration) GetFloat32 added in v0.5.0

func (c *FallbackConfiguration) GetFloat32(name string, defaultSupplier func() float32) float32

func (*FallbackConfiguration) GetFloat32Array added in v0.5.0

func (c *FallbackConfiguration) GetFloat32Array(name string, defaultSupplier func() []float32) []float32

func (*FallbackConfiguration) GetFloat32ArrayOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetFloat32ArrayOrCrash(name string) []float32

func (*FallbackConfiguration) GetFloat32OrCrash added in v0.5.0

func (c *FallbackConfiguration) GetFloat32OrCrash(name string) float32

func (*FallbackConfiguration) GetFloat64 added in v0.5.0

func (c *FallbackConfiguration) GetFloat64(name string, defaultSupplier func() float64) float64

func (*FallbackConfiguration) GetFloat64Array added in v0.5.0

func (c *FallbackConfiguration) GetFloat64Array(name string, defaultSupplier func() []float64) []float64

func (*FallbackConfiguration) GetFloat64ArrayOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetFloat64ArrayOrCrash(name string) []float64

func (*FallbackConfiguration) GetFloat64OrCrash added in v0.5.0

func (c *FallbackConfiguration) GetFloat64OrCrash(name string) float64

func (*FallbackConfiguration) GetInt added in v0.5.0

func (c *FallbackConfiguration) GetInt(name string, defaultSupplier func() int) int

func (*FallbackConfiguration) GetInt32 added in v0.5.0

func (c *FallbackConfiguration) GetInt32(name string, defaultSupplier func() int32) int32

func (*FallbackConfiguration) GetInt32Array added in v0.5.0

func (c *FallbackConfiguration) GetInt32Array(name string, defaultSupplier func() []int32) []int32

func (*FallbackConfiguration) GetInt32ArrayOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetInt32ArrayOrCrash(name string) []int32

func (*FallbackConfiguration) GetInt32OrCrash added in v0.5.0

func (c *FallbackConfiguration) GetInt32OrCrash(name string) int32

func (*FallbackConfiguration) GetInt64 added in v0.5.0

func (c *FallbackConfiguration) GetInt64(name string, defaultSupplier func() int64) int64

func (*FallbackConfiguration) GetInt64Array added in v0.5.0

func (c *FallbackConfiguration) GetInt64Array(name string, defaultSupplier func() []int64) []int64

func (*FallbackConfiguration) GetInt64ArrayOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetInt64ArrayOrCrash(name string) []int64

func (*FallbackConfiguration) GetInt64OrCrash added in v0.5.0

func (c *FallbackConfiguration) GetInt64OrCrash(name string) int64

func (*FallbackConfiguration) GetIntArray added in v0.5.0

func (c *FallbackConfiguration) GetIntArray(name string, defaultSupplier func() []int) []int

func (*FallbackConfiguration) GetIntArrayOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetIntArrayOrCrash(name string) []int

func (*FallbackConfiguration) GetIntOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetIntOrCrash(name string) int

func (*FallbackConfiguration) GetString added in v0.5.0

func (c *FallbackConfiguration) GetString(name string, defaultSupplier func() string) string

func (*FallbackConfiguration) GetStringArray added in v0.5.0

func (c *FallbackConfiguration) GetStringArray(name string, defaultSupplier func() []string) []string

func (*FallbackConfiguration) GetStringArrayOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetStringArrayOrCrash(name string) []string

func (*FallbackConfiguration) GetStringOrCrash added in v0.5.0

func (c *FallbackConfiguration) GetStringOrCrash(name string) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL