config

package module
v1.8.6 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 17 Imported by: 478

README

config

Add flexibility to the way you configure your applications. Load it from files as JSON, YAML and TOML, command line flags, environment variables and more. This package is a wrapper for koanf.

Installation

go get -u github.com/americanas-go/config

Example reading from files

# config.yaml
app:
  application:
    name: app_example_file
package main

import (
    "log"

    "github.com/americanas-go/config"
)

func init() {
    config.Add("app.application.name", "app_example_file", "description of name")
}

func main() {

    config.Load()

    log.Println(config.String("app.application.name"))
    //output: 2021/05/28 16:52:49 app_example_file
}

Example reading from environment variables

package main

import (
    "log"

    "github.com/americanas-go/config"
)

func init() {
    config.Add("k.env", "env", "description of env")
    config.Add("k.camelCase", "camel-case", "description of camel case")
    config.Add("k.camelCaseTwo", "camel_case_two", "description of camel case two")
}

func main() {

    config.Load()

    log.Println(config.String("k.env"))
    //output: 2021/05/28 17:20:03 env
    log.Println(config.String("k.camelCase"))
    //output: 2021/05/28 17:20:03 camel-case
    log.Println(config.String("k.camelCaseTwo"))
    //output: 2021/05/28 17:20:03 camel_case_two
}

Example reading from command line

package main

import (
    "log"

    "github.com/americanas-go/config"
)

func init() {
    config.Add("app.application.name", "app_example_file", "description of name")
    config.Add("app.application.enabled", true, "description of enabled")
    config.Add("app.application.duration", 10, "description of duration")
}

func main() {

    config.Load()

    log.Println(config.String("app.application.name"))
    //output: 2021/05/28 17:40:27 app_example_file
    log.Println(config.Bool("app.application.enabled"))
    //output: 2021/05/28 17:40:27 true
    log.Println(config.Int("app.application.duration"))
    //output: 2021/05/28 17:40:27 10
}

Example Unmarshalling

# config.yaml
app:
  application:
    name: app_example_file
    enabled: true
    duration: 10
package main

import (
    "log"

    "github.com/americanas-go/config"
)

type AppConfig struct {
    Application struct {
        Name     string
        Enabled  bool
        Duration int
    }
}

func init() {
    config.Add("app.application.name", "app_example_file", "description of name")
    config.Add("app.application.enabled", true, "description of enabled")
    config.Add("app.application.duration", 10, "description of duration")
}

func main() {

    config.Load()

    c := AppConfig{}

    config.UnmarshalWithPath("app", &c)

    log.Println(c.Application.Name)
    //output: 2021/05/28 17:54:12 app_example_file
    log.Println(c.Application.Enabled)
    //output: 2021/05/28 17:54:12 true
    log.Println(c.Application.Duration)
    //output: 2021/05/28 17:54:12 10
}

Contributing

Every help is always welcome. Feel free do throw us a pull request, we'll do our best to check it out as soon as possible. But before that, let us establish some guidelines:

  1. This is an open source project so please do not add any proprietary code or infringe any copyright of any sort.
  2. Avoid unnecessary dependencies or messing up go.mod file.
  3. Be aware of golang coding style. Use a lint to help you out.
  4. Add tests to cover your contribution.
  5. Add godoc to your code.
  6. Use meaningful messages to your commits.
  7. Use pull requests.
  8. At last, but also important, be kind and polite with the community.

Any submitted issue which disrespect one or more guidelines above, will be discarded and closed.


Released under the MIT License.

Documentation

Overview

Package config provides a wrapper around koanf.

Index

Constants

View Source
const ConfArgument = "conf"
View Source
const ConfEnvironment = "CONF"

Variables

This section is empty.

Functions

func Add

func Add(key string, value interface{}, description string, opts ...Option)

Add adds a flag configuration to entries.

func All added in v1.7.0

func All() map[string]interface{}

All returns all configs

func Bool

func Bool(path string) bool

Bool returns the bool value of a given key path or false if the path does not exist or if the value is not a valid bool representation. Accepted string representations of bool are the ones supported by strconv.ParseBool.

func BoolMap

func BoolMap(path string) map[string]bool

BoolMap returns the map[string]bool value of a given key path or an empty map[string]bool if the path does not exist or if the value is not a valid bool map.

func Bools

func Bools(path string) []bool

Bools returns the []bool slice value of a given key path or an empty []bool slice if the path does not exist or if the value is not a valid bool slice.

func Bytes

func Bytes(path string) []byte

Bytes returns the []byte value of a given key path or an empty []byte slice if the path does not exist or if the value is not a valid string.

func Duration

func Duration(path string) time.Duration

Duration returns the time.Duration value of a given key path assuming that the key contains a valid numeric value.

func Exists

func Exists(path string) bool

Exists returns true if the given key path exists in the conf map.

func Float64

func Float64(path string) float64

Float64 returns the float64 value of a given key path or 0 if the path does not exist or if the value is not a valid float64.

func Float64Map

func Float64Map(path string) map[string]float64

Float64Map returns the map[string]float64 value of a given key path or an empty map[string]float64 if the path does not exist or if the value is not a valid float64 map.

func Float64s

func Float64s(path string) []float64

Float64s returns the []float64 slice value of a given key path or an empty []float64 slice if the path does not exist or if the value is not a valid float64 slice.

func Get added in v1.8.0

func Get(path string) interface{}

Get returns interface{} value

func Int

func Int(path string) int

Int returns the int value of a given key path or 0 if the path does not exist or if the value is not a valid int.

func Int64

func Int64(path string) int64

Int64 returns the int64 value of a given key path or 0 if the path does not exist or if the value is not a valid int64.

func Int64Map

func Int64Map(path string) map[string]int64

Int64Map returns the map[string]int64 value of a given key path or an empty map[string]int64 if the path does not exist or if the value is not a valid int64 map.

func Int64s

func Int64s(path string) []int64

Int64s returns the []int64 slice value of a given key path or an empty []int64 slice if the path does not exist or if the value is not a valid int slice.

func IntMap

func IntMap(path string) map[string]int

IntMap returns the map[string]int value of a given key path or an empty map[string]int if the path does not exist or if the value is not a valid int map.

func Ints

func Ints(path string) []int

Ints returns the []int slice value of a given key path or an empty []int slice if the path does not exist or if the value is not a valid int slice.

func Load

func Load()

Load parsing and load flags, files and environments.

func String

func String(path string) string

String returns the string value of a given key path or "" if the path does not exist or if the value is not a valid string.

func StringMap

func StringMap(path string) map[string]string

StringMap returns the map[string]string value of a given key path or an empty map[string]string if the path does not exist or if the value is not a valid string map.

func Strings

func Strings(path string) []string

Strings returns the []string slice value of a given key path or an empty []string slice if the path does not exist or if the value is not a valid string slice.

func Time

func Time(path, layout string) time.Time

Time attempts to parse the value of a given key path and return time.Time representation. If the value is numeric, it is treated as a UNIX timestamp and if it's string, a parse is attempted with the given layout.

func Unmarshal

func Unmarshal(o interface{}) error

Unmarshal unmarshals the given struct using the mapstructure lib. The whole map is unmarshalled.

func UnmarshalWithPath

func UnmarshalWithPath(path string, o interface{}) error

UnmarshalWithPath unmarshals a given key path into the given struct using the mapstructure lib.

Types

type Config

type Config struct {
	Key         string
	Value       interface{}
	Description string
	Options     *Options
}

Config represents a flag configuration.

func Entries

func Entries() []Config

Entries returns the flag configuration list as an array.

type Option

type Option func(options *Options)

Option is a func to set values in options.

func WithHide

func WithHide() Option

WithHide sets hide option is true to config.

type Options

type Options struct {
	Hide bool
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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