config

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2019 License: MIT Imports: 5 Imported by: 1

README

config

Build Status GoDoc

Config is a simple config package to load config items from files, command line flags and enviroment variables.

简体中文

Installation

go get gitea.com/lunny/config

Example

The config format is simliar with ini but simpler(don't support sections), like below:

a=b
c=d
// load config items from file, envs or command line flags
cfgs, err := Load("config.ini")
if err != nil {
    t.Error(err)
}

// if you want to ignore the error if file is not exist, then it will read envs or command line flags
cfgs, err := LoadIfExist("config.ini")
if err != nil {
    t.Error(err)
}

Load config from flags:

cfgs := New(LoadFlags())

Load config from envs:

cfgs := New(LoadEnvs())

Documentation

Overview

Config is a simple k/v config package to load config items from files, command line flags and enviroment variables.

Installation

go get gitea.com/lunny/config

Load config items

// This will load configs from files, envs, flags and will be overrided according files < envs < flags
cfgs, err := config.Load(myconfigfile1, myconfigfile2)

// Or if you want to ignore the error when files do not exist.
cfgs, err := config.LoadIfExist(myconfigfile)

Usage

cfgs.Get(key)
cfgs.Set(key, value)
cfgs.MustString(key)
cfgs.MustInt(key)

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConfigFileNotExist reprents the error
	ErrConfigFileNotExist = errors.New("config file is not exist")
)

Functions

func LoadEnvs

func LoadEnvs() map[string]string

LoadEnvs loads config items from envs

func LoadFlags

func LoadFlags() map[string]string

LoadFlags loads config items from commandline flags

Types

type Config

type Config map[string]string

Config config object

func Load

func Load(fPath string) (*Config, error)

Load loads config items from files, envs, flags

func LoadFiles

func LoadFiles(fPaths ...string) (*Config, error)

LoadFiles loads config items from files

func LoadIfExist

func LoadIfExist(fPaths ...string) (*Config, error)

LoadIfExist will load files if exist and then environments and then flags

func New

func New(data ...map[string]string) *Config

New creates a config object from maps

func Parse

func Parse(data string) *Config

Parse parses data to config

func (*Config) Get

func (config *Config) Get(key string) string

Get returns a config item

func (*Config) GetBool

func (config *Config) GetBool(key string) (bool, error)

GetBool returns item value as bool

func (*Config) GetFloat64

func (config *Config) GetFloat64(key string) (float64, error)

GetFloat64 returns item value as float64

func (*Config) GetInt

func (config *Config) GetInt(key string) (int, error)

GetInt returns item value as int

func (*Config) GetSlice

func (config *Config) GetSlice(key, sep string) []string

GetSlice returns item value as a slice

func (*Config) Has

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

Has returned if the key existed

func (*Config) Map

func (config *Config) Map() map[string]string

Map returns all the item key and value as a map

func (*Config) MustBool

func (config *Config) MustBool(key string, defaultVal ...bool) bool

MustBool always returns item value as bool

func (*Config) MustFloat64

func (config *Config) MustFloat64(key string, defaultVal ...float64) float64

MustFloat64 always returns item value as float64

func (*Config) MustInt

func (config *Config) MustInt(key string, defaultVal ...int) int

MustInt always returns item value as int

func (*Config) MustInt64

func (config *Config) MustInt64(key string, defaultVal ...int64) int64

MustInt64 always returns item value as int64

func (*Config) MustString

func (config *Config) MustString(key string, defaultVal ...string) string

MustString return the string value

func (*Config) Set

func (config *Config) Set(key, value string)

Set sets a config item

Jump to

Keyboard shortcuts

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