settings

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: GPL-3.0 Imports: 17 Imported by: 0

README

Configuration library

Allows the application to load it's configuration from .config files or environment variables

Install

To install the platform

$ go get github.com/najibulloShapoatov/server-core/settings

Usage example

import "github.com/najibulloShapoatov/server-core/settings"

type MySettings struct {
    Host    string        `config:"app.host" default:"localhost"`
    Port    int           `config:"app.port" default:"80"`
    Timeout time.Duration `config:"app.timeout" default:"3s"`
    Debug   bool          `config:"debug" default:"on"`
    
}

func main() {

   s := settings.GetSettings()
   err := s.Load(
         settings.NewFileLoader("file.conf", false),  // load config from this file
         settings.NewEnvLoader(false, "app")          // and also from environment variables
   )
   if err != nil {
       fmt.Prinln("failed to parse settings", err)
   }

   // unmarshall directly into a struct
   var mySettings MySettings
   if err := settings.Unmarshal(&mySettings); err != nil {
       fmt.Println("some error", err)
   }

   // retrieve a value by name
   isDebug, exists := settings.GetBool("debug")
}

Configuration files

Configuration files are mostly key=value files but with few additions. For example, numbers are evaluated by the parser and booleans can be all truthy values besides true or false. Other files can be included using the include directive

# String values
key.name = "value" # this is inline comment
key.multiline = "multi \
line \
string"

# Number values
test.int.value = 5
test.float.value = 3.14
test.negative.value = -1.2
test.hex.number = 0x1234 # will parse to 4660
test.octal.number = 0o123 # will parse to 83
test.binary.number = 0b1010101 # will parse to 85
test.exponential.number = 1e3 # will parse to 1000
test.negative.exponential.number = 2e-2 # will parse to 0.02

# Boolean values
test.bool.value1 = yes       # or no
test.bool.value2 = on        # or off
test.bool.value3 = set       # or unset
test.bool.value4 = active    # or inactive
test.bool.value5 = enabled   # or disabled
test.bool.value6 = true      # or false
test.bool.value6 = 1         # or 0

# Duration
test.duration.value1 = "1h5m"
test.duration.value2 = "3s"

# Include other file
include "sub-config-file.conf"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

Types

type Loader

type Loader interface {
	// Parse method is called
	Parse() (map[string]string, error)
}

Loader is used to load and parse configuration values from various formats and location

func NewEnvLoader

func NewEnvLoader(watchForChanges bool, prefix string) Loader

NewEnvLoader returns a env vars parser

func NewFileLoader

func NewFileLoader(filename string, watchForChanges bool) Loader

NewFileLoader returns a .conf file parser

func NewStringLoader

func NewStringLoader(src string) Loader

type Settings

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

func GetSettings

func GetSettings() *Settings

Instantiate new settings reader

func (*Settings) GetBool

func (s *Settings) GetBool(key string) (bool, bool)

GetBool returns the value at the given key parsed as bool and true if the key exists or false and false if the key doesn't exist or failed to parse as a truthy value

func (*Settings) GetDuration

func (s *Settings) GetDuration(key string) (time.Duration, bool)

GetDuration returns the value at the given key parsed as Duration and true if the key exists or Duration(0) and false if the key doesn't exist or failed to parse as Duration

func (*Settings) GetFloat

func (s *Settings) GetFloat(key string) (float64, bool)

GetFloat returns the value at the given key parsed as a float and true if the key exists or 0.0 and false if the key doesn't exist or failed to parse as a float64

func (*Settings) GetInt

func (s *Settings) GetInt(key string) (int, bool)

GetInt returns the value at the given key parsed as a int and true if the key exists or 0 and false if the key doesn't exist or failed to parse as a int

func (*Settings) GetKeys

func (s *Settings) GetKeys() (res []string)

func (*Settings) GetString

func (s *Settings) GetString(key string) (string, bool)

GetString returns the value at the given key as a string and true if the key exists or empty string and false if the key doesn't exist

func (*Settings) Has

func (s *Settings) Has(key string) bool

Has returns true if the given key exists

func (*Settings) Load

func (s *Settings) Load(loaders ...Loader) error

Load runs the given loaders in order to load and parse the configuration values. The first loader that returns an error stops the load process

func (*Settings) Unmarshal

func (s *Settings) Unmarshal(destinationPtr interface{}) error

Unmarshal decodes the configuration in a structure based on the `config` and `default` tags

Jump to

Keyboard shortcuts

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