config

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2023 License: MIT Imports: 11 Imported by: 15

README

na4ma4/config

CI GoDoc

Go package for a thread-safe config interface

Installation

go get -u github.com/na4ma4/config

Example

package main

import (
    "fmt"
    "log"

    "github.com/na4ma4/config"
)

func main() {
    // Create config (example supplied viper)
    // Supplied ViperConf takes a project name, then a list of file names,
    // if no filenames are found, the last one is considered where you want the config to be saved.
    vcfg := config.NewViperConfig("test-project2", "artifacts/test-project.toml", "/tmp/test-project.toml", "test/test-project.toml")

    server := vcfg.GetString("server.address")

    fmt.Printf("Server: %s\n", server)

    err := vcfg.Save()
    if err != nil {
        log.Fatal(err)
    }
}

Documentation

Overview

Package config provides a standard interface to a configuration storage system.

With an example using spf13/viper and wrapping with a mutex to make it thread-safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conf

type Conf interface {
	Get(key string) interface{}
	GetBool(key string) bool
	GetDuration(key string) time.Duration
	GetFloat64(key string) float64
	GetInt(key string) int
	GetIntSlice(key string) []int
	GetString(key string) string
	GetStringSlice(key string) []string

	Set(key string, value interface{})
	SetBool(key string, value bool)
	SetDuration(key string, value time.Duration)
	SetFloat64(key string, value float64)
	SetInt(key string, value int)
	SetIntSlice(key string, value []int)
	SetString(key string, value string)
	SetStringSlice(key string, value []string)

	ZapConfig() zap.Config
	Save() error
}

Conf is the default configuration object interface.

func NewViperConfD added in v0.5.0

func NewViperConfD(project string, confdpath string, filename ...string) Conf

NewViperConfD returns a Conf compatible ViperConfD object.

func NewViperConfDFromViper added in v0.5.0

func NewViperConfDFromViper(vcfg *viper.Viper, confdpath string, filename ...string) Conf

NewViperConfDFromViper returns a Conf compatible ViperConfD object copied from the system viper.Viper.

func NewViperConfig

func NewViperConfig(project string, filename ...string) Conf

NewViperConfig returns a Conf compatible ViperConf object.

func NewViperConfigFromViper added in v0.2.0

func NewViperConfigFromViper(vcfg *viper.Viper, filename ...string) Conf

NewViperConfigFromViper returns a Conf compatible ViperConf object copied from the system viper.Viper.

type ViperConf

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

ViperConf is a Conf compatible Viper configuration object.

func (*ViperConf) Get

func (v *ViperConf) Get(key string) interface{}

Get can retrieve any value given the key to use. Get is case-insensitive for a key. Get has the behavior of returning the value associated with the first place from where it is set. Viper will check in the following order: override, flag, env, config file, key/value store, default

Get returns an interface. For a specific value use one of the Get____ methods.

func (*ViperConf) GetBool

func (v *ViperConf) GetBool(key string) bool

GetBool returns the value associated with the key as a boolean.

func (*ViperConf) GetDuration

func (v *ViperConf) GetDuration(key string) time.Duration

GetDuration returns the value associated with the key as a duration.

func (*ViperConf) GetFloat64

func (v *ViperConf) GetFloat64(key string) float64

GetFloat64 returns the value associated with the key as a float64.

func (*ViperConf) GetInt

func (v *ViperConf) GetInt(key string) int

GetInt returns the value associated with the key as an int.

func (*ViperConf) GetIntSlice

func (v *ViperConf) GetIntSlice(key string) []int

GetIntSlice returns the value associated with the key as a slice of ints.

func (*ViperConf) GetString

func (v *ViperConf) GetString(key string) string

GetString returns the value associated with the key as a string.

func (*ViperConf) GetStringSlice

func (v *ViperConf) GetStringSlice(key string) []string

GetStringSlice returns the value associated with the key as a slice of strings.

func (*ViperConf) Save

func (v *ViperConf) Save() error

Save writes the config to the file system.

func (*ViperConf) Set

func (v *ViperConf) Set(key string, value interface{})

Set sets the value for the key in the viper object.

func (*ViperConf) SetBool

func (v *ViperConf) SetBool(key string, value bool)

SetBool sets the value for the key in the viper object.

func (*ViperConf) SetDefault added in v0.3.0

func (v *ViperConf) SetDefault(key string, value interface{})

SetDefault sets the default value for this key. SetDefault is case-insensitive for a key. Default only used when no value is provided by the user via flag, config or ENV.

func (*ViperConf) SetDuration

func (v *ViperConf) SetDuration(key string, value time.Duration)

SetDuration sets the value for the key in the viper object.

func (*ViperConf) SetFloat64

func (v *ViperConf) SetFloat64(key string, value float64)

SetFloat64 sets the value for the key in the viper object.

func (*ViperConf) SetInt

func (v *ViperConf) SetInt(key string, value int)

SetInt sets the value for the key in the viper object.

func (*ViperConf) SetIntSlice

func (v *ViperConf) SetIntSlice(key string, value []int)

SetIntSlice sets the value for the key in the viper object.

func (*ViperConf) SetString

func (v *ViperConf) SetString(key string, value string)

SetString sets the value for the key in the viper object.

func (*ViperConf) SetStringSlice

func (v *ViperConf) SetStringSlice(key string, value []string)

SetStringSlice sets the value for the key in the viper object.

func (*ViperConf) Write

func (v *ViperConf) Write(out io.Writer) error

func (*ViperConf) ZapConfig

func (v *ViperConf) ZapConfig() zap.Config

ZapConfig returns a zap logger configuration derived from settings in the viper config.

type ViperConfD added in v0.5.0

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

ViperConfD is a Conf compatible Viper configuration object.

func (*ViperConfD) AllSettings added in v0.5.0

func (v *ViperConfD) AllSettings() map[string]interface{}

AllSettings merges all settings and returns them as a map[string]interface{}.

func (*ViperConfD) Get added in v0.5.0

func (v *ViperConfD) Get(key string) interface{}

Get can retrieve any value given the key to use. Get is case-insensitive for a key. Get has the behavior of returning the value associated with the first place from where it is set. Viper will check in the following order: override, flag, env, config file, key/value store, default

Get returns an interface. For a specific value use one of the Get____ methods.

func (*ViperConfD) GetBool added in v0.5.0

func (v *ViperConfD) GetBool(key string) bool

GetBool returns the value associated with the key as a boolean.

func (*ViperConfD) GetDuration added in v0.5.0

func (v *ViperConfD) GetDuration(key string) time.Duration

GetDuration returns the value associated with the key as a duration.

func (*ViperConfD) GetFloat64 added in v0.5.0

func (v *ViperConfD) GetFloat64(key string) float64

GetFloat64 returns the value associated with the key as a float64.

func (*ViperConfD) GetInt added in v0.5.0

func (v *ViperConfD) GetInt(key string) int

GetInt returns the value associated with the key as an int.

func (*ViperConfD) GetIntSlice added in v0.5.0

func (v *ViperConfD) GetIntSlice(key string) []int

GetIntSlice returns the value associated with the key as a slice of ints.

func (*ViperConfD) GetString added in v0.5.0

func (v *ViperConfD) GetString(key string) string

GetString returns the value associated with the key as a string.

func (*ViperConfD) GetStringSlice added in v0.5.0

func (v *ViperConfD) GetStringSlice(key string) []string

GetStringSlice returns the value associated with the key as a slice of strings.

func (*ViperConfD) Save added in v0.5.0

func (v *ViperConfD) Save() error

Save writes the config to the file system.

func (*ViperConfD) Set added in v0.5.0

func (v *ViperConfD) Set(key string, value interface{})

Set sets the value for the key in the viper object.

func (*ViperConfD) SetBool added in v0.5.0

func (v *ViperConfD) SetBool(key string, value bool)

SetBool sets the value for the key in the viper object.

func (*ViperConfD) SetDefault added in v0.5.0

func (v *ViperConfD) SetDefault(key string, value interface{})

SetDefault sets the default value for this key. SetDefault is case-insensitive for a key. Default only used when no value is provided by the user via flag, config or ENV.

func (*ViperConfD) SetDuration added in v0.5.0

func (v *ViperConfD) SetDuration(key string, value time.Duration)

SetDuration sets the value for the key in the viper object.

func (*ViperConfD) SetFloat64 added in v0.5.0

func (v *ViperConfD) SetFloat64(key string, value float64)

SetFloat64 sets the value for the key in the viper object.

func (*ViperConfD) SetInt added in v0.5.0

func (v *ViperConfD) SetInt(key string, value int)

SetInt sets the value for the key in the viper object.

func (*ViperConfD) SetIntSlice added in v0.5.0

func (v *ViperConfD) SetIntSlice(key string, value []int)

SetIntSlice sets the value for the key in the viper object.

func (*ViperConfD) SetString added in v0.5.0

func (v *ViperConfD) SetString(key string, value string)

SetString sets the value for the key in the viper object.

func (*ViperConfD) SetStringSlice added in v0.5.0

func (v *ViperConfD) SetStringSlice(key string, value []string)

SetStringSlice sets the value for the key in the viper object.

func (*ViperConfD) Write added in v0.5.0

func (v *ViperConfD) Write(out io.Writer) error

func (*ViperConfD) ZapConfig added in v0.5.0

func (v *ViperConfD) ZapConfig() zap.Config

ZapConfig returns a zap logger configuration derived from settings in the viper config.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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