envconf

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2024 License: GPL-3.0 Imports: 10 Imported by: 2

README

envconf

Build Status Go Report Card GoDoc

Boostrap for operations with config file or env variables. Read/Save config files like "~/.app" with sections and Set/Get env variables.

Get Started

Config file sample:

[main]
name = test

[server]

[client]

Usages:

import "github.com/devfans/envconf"

func main() {
  config := envconf.NewConfig("~/.app")

  // get name key from config from default section: [main]
  name := config.Get("name")

  // get sections
  server := config.GetSection("server")
  client := config.GetSection("client")

  // add new keys
  server.Put("ip", "localhost")
  client.Put("ip", "0.0.0.0")

  // save to disk as file "~/.app"
  config.Save() 
  
  // other usages
  config.Section = "server"     // switch current section
  serverIp := config.String("ip")  // localhost

  config.Get("SERVER_IP", "ip") // get env first if env variable is not null

  config.Getenv("SERVER_IP")
  config.Setenv("SERVER_IP", "localhost")
}

dotenv

# file: .env

[main]
use_section = case1

[case1]
name = a

[case2]
name = b

Example


import (
	"os"
	"testing"

	"github.com/devfans/envconf/dotenv"
)

func TestEnv(t *testing.T) {
	t.Log(os.Getenv("a"))
	t.Log(os.Getenv("b"))
	t.Log(dotenv.Int("a"))
	t.Log(dotenv.Uint("b"))
	t.Log(dotenv.Bool("c"))
	t.Log(dotenv.Bool("d"))
	t.Log(os.Getenv("test"))
	t.Log(dotenv.String("test"))
	t.Log(dotenv.EnvConf().Get("a"))
	t.Log(dotenv.EnvConf().Get("b"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseValue added in v0.0.3

func ParseValue(value string) string

ParseValue will parse config from raw string

Types

type Config

type Config struct {
	Path     string
	Sections map[string]Section
	sync.Mutex
	Section string
}

Config struct has map to contains secions and an attribute to indicate the current section

func NewConfig

func NewConfig(paths ...string) *Config

NewConfig creates main Config instance with specified config file path

func NewEmptyConfig added in v0.0.4

func NewEmptyConfig() *Config

NewEmptyConfig creates an empty Config instance

func (*Config) Bool added in v0.0.4

func (c *Config) Bool(args ...interface{}) bool

Bool parse env value as bool

args set: (name) args set: (name, defaultValue)

func (*Config) Fetch

func (c *Config) Fetch(args ...interface{}) Value

Feth will get key values from config

At least the key name should be provided

Parameter sets: conf_key

Parameter sets: conf_key, env_key

Parameter sets: conf_key, env_key, default_value

When env_key is provided it will try to fetch env variable only if the value of conf_key is empty

func (*Config) Float added in v0.0.8

func (c *Config) Float(args ...interface{}) float64

Float parse env value as float64

args set: (name) args set: (name, defaultValue)

func (*Config) Get

func (c *Config) Get(args ...interface{}) Value

Get will get key values from config

At least the key name should be provided

Parameter sets: conf_key

Parameter sets: env_key, conf_key

Parameter sets: env_key, conf_key, default_value

When env_key is provided it will try to fetch env variable first, if it's empty, it will try to get it from config

func (*Config) GetConf

func (c *Config) GetConf(args ...interface{}) Value

GetConf will get key values from config

At least the key name should be provided

Parameter sets: conf_key

Parameter sets: conf_key, default_value

func (*Config) GetEnv

func (c *Config) GetEnv(args ...interface{}) Value

GetEnv will get key values from env

At least the key name should be provided

Parameter sets: env_key

Parameter sets: env_key, default_value

func (*Config) GetSection

func (c *Config) GetSection(section string) Section

GetSection gets config section with name

func (*Config) Getenv

func (c *Config) Getenv(key interface{}) string

Getenv is a sugar for config object to get env

func (*Config) Getkey

func (c *Config) Getkey(key interface{}) string

Getkey will key from config

func (*Config) Int added in v0.0.4

func (c *Config) Int(args ...interface{}) int64

Int parse env value as int64

args set: (name) args set: (name, defaultValue)

func (*Config) List

func (c *Config) List() []string

List keys from current section

func (*Config) Put

func (c *Config) Put(args ...interface{})

Put is a Wrapper for Put

func (*Config) Save

func (c *Config) Save()

Save saves config file and default keys locally

func (*Config) Setenv

func (c *Config) Setenv(key, value interface{})

Setenv is a sugar for config object to set env

func (*Config) String added in v0.0.4

func (c *Config) String(args ...interface{}) string

String parse config value as string

args set: (name) args set: (name, defaultValue)

func (*Config) Uint added in v0.0.4

func (c *Config) Uint(args ...interface{}) uint64

Uint parse env value as uint64

args set: (name) args set: (name, defaultValue)

type Section

type Section map[string]interface{}

Section is a based on map, not thread safe

func NewSection

func NewSection(section string) Section

NewSection will create new section in config

func (Section) Bool added in v0.0.4

func (sec Section) Bool(args ...interface{}) bool

Bool parse config value as bool

args set: (name) args set: (name, defaultValue)

func (Section) Fetch

func (sec Section) Fetch(args ...interface{}) Value

Fetch will get config key, args pattern: configKey, envKey, defaultValue

func (Section) Float added in v0.0.8

func (sec Section) Float(args ...interface{}) float64

Float parse config value as float64

args set: (name) args set: (name, defaultValue)

func (Section) Get

func (sec Section) Get(args ...interface{}) Value

Get will get config key, args pattern: envKey, configKey, defaultValue or just configKey

func (Section) GetConf

func (sec Section) GetConf(args ...interface{}) Value

GetConf will get config key, args pattern: confKey, defaultValue

func (Section) GetEnv

func (sec Section) GetEnv(args ...interface{}) Value

GetEnv will get config key, args pattern: envKey, defaultValue

func (Section) Getenv

func (sec Section) Getenv(key interface{}) string

Getenv is a sugar for section object to get env

func (Section) Getkey

func (sec Section) Getkey(key interface{}) string

Getkey will get key from config

func (Section) Int added in v0.0.4

func (sec Section) Int(args ...interface{}) int64

Int parse config value as int64

args set: (name) args set: (name, defaultValue)

func (Section) List

func (sec Section) List() []string

List will list keys section key without order

func (Section) Put

func (sec Section) Put(args ...interface{})

Put will add new key with or without value

func (Section) Setenv

func (sec Section) Setenv(key, value interface{})

Setenv is a sugar for section object to set env

func (Section) String added in v0.0.4

func (sec Section) String(args ...interface{}) string

String parse config value

args set: (name) args set: (name, defaultValue)

func (Section) Uint added in v0.0.4

func (sec Section) Uint(args ...interface{}) uint64

Uint parse config value as uint64

args set: (name) args set: (name, defaultValue)

type Value added in v0.0.7

type Value string

Value define the general value type

func (Value) Bool added in v0.0.7

func (v Value) Bool() bool

Int converts Value to bool

func (Value) Float added in v0.0.8

func (v Value) Float() float64

Float converts Value to float64

func (Value) Int added in v0.0.7

func (v Value) Int() int64

Int converts Value to int64

func (Value) String added in v0.0.7

func (v Value) String() string

Int converts Value to string

func (Value) Uint added in v0.0.7

func (v Value) Uint() uint64

Int converts Value to uint64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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