config

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2022 License: MIT Imports: 16 Imported by: 0

README

Simple Configuration library

Allows the application to load its configuration from .config files or environment variables and automatic watches to changes

Install

To install the package

$ go get github.com/najibulloShapoatov/config

Methods for Config struct

Method Description
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
Has(key string) bool Has returns true if the given key exists
SetWatchInterval(duration time.Duration) SetWatchInterval sets interval of to automatic load configurations every duration, default interval is 5s
StopWatch() StopWatch stopping all watches
Unmarshal(destinationPtr interface{}) error Unmarshal decodes the configuration in a structure based on the config and default tags
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
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
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
GetBool(key string) (bool, 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
GetKeys() (res []string) GetKeys return all loaded keys

You can add custom loader like json or yaml or xml etc. you need implement Loader interface and add your loader in load function.

// Loader is used to load and parse configuration values from various formats and location
type Loader interface {
	// Parse method is called
	Parse() (map[string]string, error)
	// IsWatchable method should return true if you need watch this loader
	IsWatchable() bool
}

Usage example

package main

import (
	"fmt"
	"github.com/joho/godotenv"
	"github.com/najibulloShapoatov/config"
	"time"
)

type MyConf struct {
	Debug         bool          `config:"APPDEBUG" default:"true"`
	Name          string        `config:"key.name"`
	Name1         string        `config:"key.name1" default:"name_f"`
	Multiline     string        `config:"key.multiline"`
	Int           int           `config:"test.int.value"`
	Float         float64       `config:"test.float.value"`
	NegativeFloat float64       `config:"test.negative.value"`
	Hex           int           `config:"test.hex.number"`
	Octal         int           `config:"test.octal.number"`
	Binary        int           `config:"test.binary.number"`
	Exp           float64       `config:"test.exponential.number"`
	NegExp        float64       `config:"test.negative.exponential.number"`
	B1            bool          `config:"test.bool.value1"`
	B2            bool          `config:"test.bool.value2"`
	B3            bool          `config:"test.bool.value3"`
	B4            bool          `config:"test.bool.value4"`
	B5            bool          `config:"test.bool.value5"`
	B6            bool          `config:"test.bool.value6"`
	B7            bool          `config:"test.bool.value7"`
	Duration1     time.Duration `config:"test.duration.value1" default:"1h"`
	Duration2     time.Duration `config:"test.duration.value2"`
}

func main() {

	err := godotenv.Load()
	if err != nil {
		return 
	}

	//conf := config.Get()

	conf, err := config.Load(
		config.NewFileLoader("file.conf", true), // load config from this file
		config.NewEnvLoader(false, "app"),       // and also from environment variables
		config.NewStringLoader(`
test.int.value = 7
test.float.value = 3.17
test.negative.value = -1.7
`),
	)
	if err != nil {
		fmt.Println("failed to parse", err)
	}

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

	fmt.Printf("+%v", cfg)
	fmt.Println()

	// retrieve a value by name
	isDebug, exists := conf.GetBool("APPDEBUG")

	fmt.Println("isDebug: ", isDebug, " exists: ", exists)
}

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 Config

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

func Get

func Get() *Config

Get Instantiate new config reader

func Load

func Load(loaders ...Loader) (*Config, error)

func (*Config) GetBool

func (c *Config) 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 (*Config) GetDuration

func (c *Config) 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 (*Config) GetFloat

func (c *Config) 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 (*Config) GetInt

func (c *Config) 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 (*Config) GetKeys

func (c *Config) GetKeys() (res []string)

GetKeys return all loaded keys

func (*Config) GetString

func (c *Config) 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 (*Config) Has

func (c *Config) Has(key string) bool

Has returns true if the given key exists

func (*Config) Load

func (c *Config) 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 (*Config) SetWatchInterval

func (c *Config) SetWatchInterval(duration time.Duration)

SetWatchInterval sets interval of to automatic load configurations every duration, default interval is 5s

func (*Config) StopWatch

func (c *Config) StopWatch()

StopWatch stopping all watches

func (*Config) Unmarshal

func (c *Config) Unmarshal(destinationPtr interface{}) error

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

type Loader

type Loader interface {
	// Parse method is called
	Parse() (map[string]string, error)
	// IsWatchable method should return true if you need watch this loader
	IsWatchable() bool
}

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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