Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

github.com/gookit/config

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/gookit/config


Version published
Created
Source

config

GoDoc Build Status Coverage Status Go Report Card

Golang application config manage tool library.

中文说明

  • Support multi format: JSON(default), INI, YAML, TOML, HCL
    • JSON content support comments. will auto clear comments
  • Support multi-file and multi-data loading
  • Support for loading configuration data from remote URLs
  • Support for setting configuration data from command line arguments(flags)
  • Support data overlay and merge, automatically load by key when loading multiple copies of data
  • Support get sub value by path, like map.key arr.2
  • Support parse ENV name. like envKey: ${SHELL} -> envKey: /bin/zsh
  • Generic api Get Int String Bool Ints IntMap Strings StringMap ...
  • complete unit test(code coverage > 95%)

Only use INI

If you just want to use INI for simple config management, recommended use gookit/ini

GoDoc

Usage

Here using the yaml format as an example(testdata/yml_other.yml):

name: app2
debug: false
baseKey: value2
shell: ${SHELL}
envKey1: ${NotExist|defValue}

map1:
    key: val2
    key2: val20

arr1:
    - val1
    - val21

Load data

examples code please see _examples/yaml.go:

package main

import (
    "github.com/gookit/config"
    "github.com/gookit/config/yaml"
)

// go run ./examples/yaml.go
func main() {
	config.WithOptions(config.ParseEnv)
	
	// add driver for support yaml content
	config.AddDriver(yaml.Driver)
	// config.SetDecoder(config.Yaml, yaml.Decoder)

	err := config.LoadFiles("testdata/yml_base.yml")
	if err != nil {
		panic(err)
	}

	// fmt.Printf("config data: \n %#v\n", config.Data())

	// load more files
	err = config.LoadFiles("testdata/yml_other.yml")
	// can also load multi at once
	// err := config.LoadFiles("testdata/yml_base.yml", "testdata/yml_other.yml")
	if err != nil {
		panic(err)
	}
}

Read data

  • get integer
age, ok := config.Int("age")
fmt.Print(ok, age) // true 100
  • Get bool
val, ok := config.Bool("debug")
fmt.Print(ok, val) // true true
  • Get string
name, ok := config.String("name")
fmt.Print(ok, name) // true inhere
  • Get strings(slice)
arr1, ok := config.Strings("arr1")
fmt.Printf("%v %#v", ok, arr1) // true []string{"val1", "val21"}
  • Get string map
val, ok := config.StringMap("map1")
fmt.Printf("%v %#v",ok, val) // true map[string]string{"key":"val2", "key2":"val20"}
  • Value contains ENV var
value, ok := config.String("shell")
fmt.Print(ok, value) // true /bin/zsh
  • Get value by key path
// from array
value, ok := config.String("arr1.0")
fmt.Print(ok, value) // true "val1"

// from map
value, ok := config.String("map1.key")
fmt.Print(ok, value) // true "val2"
  • Setting new value
// set value
config.Set("name", "new name")
name, ok = config.String("name")
fmt.Print(ok, name) // true "new name"

API Methods Refer

Load Config

  • LoadData(dataSource ...interface{}) (err error) Load from struts or maps
  • LoadFlags(keys []string) (err error) Load from cli flags
  • LoadExists(sourceFiles ...string) (err error)
  • LoadFiles(sourceFiles ...string) (err error)
  • LoadRemote(format, url string) (err error)
  • LoadSources(format string, src []byte, more ...[]byte) (err error)
  • LoadStrings(format string, str string, more ...string) (err error)

Getting Values

DefXXX get value with default value

  • Bool(key string) (value bool, ok bool)
  • DefBool(key string, defVal ...bool) bool
  • Int(key string) (value int, ok bool)
  • DefInt(key string, defVal ...int) int
  • Int64(key string) (value int64, ok bool)
  • DefInt64(key string, defVal ...int64)
  • Ints(key string) (arr []int, ok bool)
  • IntMap(key string) (mp map[string]int, ok bool)
  • Float(key string) (value float64, ok bool)
  • DefFloat(key string, defVal ...float64) float64
  • String(key string) (value string, ok bool)
  • DefString(key string, defVal ...string) string
  • Strings(key string) (arr []string, ok bool)
  • StringMap(key string) (mp map[string]string, ok bool)
  • Get(key string, findByPath ...bool) (value interface{}, ok bool)

Setting Values

  • Set(key string, val interface{}, setByPath ...bool) (err error)

Useful Methods

  • AddDriver(driver Driver)
  • Data() map[string]interface{}
  • DumpTo(out io.Writer, format string) (n int64, err error)

Run Tests

go test -cover
// contains all sub-folder
go test -cover ./...

Ini Config Use

License

MIT

FAQs

Package last updated on 13 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc