envconf

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: GPL-3.0 Imports: 8 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

This section is empty.

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

Create main Config instance with specified config file path

func (*Config) Fetch

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

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

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

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

Get config section with name

func (*Config) Getenv

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

Sugar for config object to get env

func (*Config) Getkey

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

Get key from config

func (*Config) List

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

List keys from current section

func (*Config) Put

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

Wrapper for Put

func (*Config) Save

func (c *Config) Save()

Save config file and default keys locally

func (*Config) Setenv

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

Sugar for config object to set env

type Section

type Section map[string]interface{}

Section is a based on map

func NewSection

func NewSection(section string) Section

Create new section in config

func (*Section) Fetch

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

Get config key, args pattern: configKey, envKey, defaultValue

func (*Section) Get

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

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

func (*Section) GetConf

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

Get config key, args pattern: confKey, defaultValue

func (*Section) GetEnv

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

Get config key, args pattern: envKey, defaultValue

func (*Section) Getenv

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

Sugar for section object to get env

func (*Section) Getkey

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

get key from config

func (*Section) List

func (sec *Section) List() []string

list keys section key without order

func (*Section) Put

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

Add new key with or without value

func (*Section) Setenv

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

Sugar for section object to set env

Jump to

Keyboard shortcuts

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