gconfig

package module
v0.0.0-...-c7fd719 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 11 Imported by: 1

README

gconfig

Spring boot style configuration management for Go

Note: Only supports *.properties file, working on YAML support

Code example
    // Profile can be set using 2 ways:
    // 1. Environment variable 'GC_PROFILE' eg: export GC_PROFILE='dev'
    // 2. Command line argument 'profile' eg: go run myserver.go -profile=dev

    //Path
    // 1. Environment variable 'GC_PATH' eg: export GC_PATH='./config' config directory in $GOPATH folder
    // 2. Command line argument 'path' eg: -path=/Users/puran/myserver/config

    import "github.com/narup/gconfig"

    //load configuration
	if _, err := gconfig.Load(); err != nil {
		fmt.Printf("Error::%s\n", err.Error())
	}
	cfg = gconfig.Gcg

	//read config
	host := cfg.GetString("maindb.host")
	port := cfg.GetInt("maindb.port")

Usage: command line flags
	go run main.go -profile=stage -path=/Users/puran/server/config

Documentation

Overview

Package gconfig - Spring boot style configuration manager. It can load properties files. properties file should follow the naming convention:

  1. application.properties: this holds all the default configuration values as key/value pair.
  2. application-{profile}.properties. contains all the environment specific configuration values. eg: for prod environment, application-prod.properties

Index

Constants

View Source
const (
	// PropertiesExtension defines extension for properties file
	PropertiesExtension string = ".properties"
	// StandardPropFileName standard properties file if default is not defined
	StandardPropFileName string = "application.properties"
)

Variables

View Source
var ErrConfigFileRequired = errors.New("At least one configuration file is required")

ErrConfigFileRequired represents file required error

Functions

This section is empty.

Types

type GConfig

type GConfig struct {
	Profile string
	// contains filtered or unexported fields
}

GConfig is the representation of all the configuration properties. It loads 2 types of data: default and environment specific. One out of 2 must be present otherwise, error is returned during the Load operation

var Gcg *GConfig

Gcg is a global variable that represents configuration

func Load

func Load() (*GConfig, error)

Load reads all the properties and creates GConfig representation. It loads config data based on passed in flags or environment variables. If none is defined it uses default values.

func (GConfig) Exists

func (c GConfig) Exists(key string) bool

Exists checks if key exists

func (GConfig) GetBool

func (c GConfig) GetBool(key string) bool

GetBool returns bool value for the given key

func (GConfig) GetFloat

func (c GConfig) GetFloat(key string) float64

GetFloat returns float value for the given key

func (GConfig) GetInt

func (c GConfig) GetInt(key string) int

GetInt returns int value for the given key

func (GConfig) GetString

func (c GConfig) GetString(key string) string

GetString returns string value for the given key

func (GConfig) GetStringOrDefault

func (c GConfig) GetStringOrDefault(key string) string

GetString returns string value for the given key

func (GConfig) GetStringOrDefaultInCommaSeparator

func (c GConfig) GetStringOrDefaultInCommaSeparator(key string) string

GetString returns string value for the given key

Jump to

Keyboard shortcuts

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