envconf

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: GPL-3.0 Imports: 9 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.Get("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")
}

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
	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{}) string

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) Get

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

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{}) string

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{}) string

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 env value

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

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 env value as bool

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

func (*Section) Fetch

func (sec *Section) Fetch(args ...interface{}) string

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

func (*Section) Get

func (sec *Section) Get(args ...interface{}) string

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

func (*Section) GetConf

func (sec *Section) GetConf(args ...interface{}) string

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

func (*Section) GetEnv

func (sec *Section) GetEnv(args ...interface{}) string

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 env 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 env value

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

func (*Section) Uint added in v0.0.4

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

Uint64 parse env value as uint64

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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