config

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2023 License: MIT Imports: 14 Imported by: 37

README

Go Reference GitHub go.mod Go version GitHub release (latest by date) Go Report Card Actions Status

Read & Access Configurations

Provides different sources to read config and a generic interface to access configurations. Under the hood it used ![Viper Config]/https://github.com/spf13/viper) to load, parse and access configurations.

Sources

Following sources are available:

  • local/static config
  • config from YAML files
  • config from files stored in AWS S3 bucket

Documentation

Overview

Package config provides access to config from different sources in YAML format. Uses viper config from github.com/spf13/viper to load and access config values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsBoolPtr

func AsBoolPtr(v bool) *bool

AsBoolPtr returns given value as pointer.

func AsDuration added in v1.1.0

func AsDuration(value string) *time.Duration

AsDuration will try to convert passed string to time.Duration

func AsDurationPtr added in v1.0.1

func AsDurationPtr(v time.Duration) *time.Duration

AsDurationPtr returns given value as pointer.

func AsIntPtr

func AsIntPtr(v int) *int

AsIntPtr will return passed int value as pointer.

func AsStringPtr

func AsStringPtr(v string) *string

AsStringPtr return given value as pointer.

func SetViperConfigType

func SetViperConfigType(configType string)

SetViperConfigType sets the config type which should be used at creating new viper config.

Types

type Config

type Config interface {

	// Get try to load config value for passed key and will return given default
	// if it's not available.
	Get(key string, defaultValue *string) *string

	// GetAsInt try to load value for given config and will try to convert it
	// to imt. If there's no config value for passed key or conversion to int failes,
	// it wll return given default value.
	GetAsInt(key string, defaultValue *int) *int

	// GetAsIntSlice returns a string slice of config values for passed key
	// or return passed default value it there's no value for tis key.
	GetAsIntSlice(key string, defaultValue *[]int) *[]int

	// GetAsBool returns config value as bool or given default value
	// if there's no value for this key or conversion to bool fails.
	GetAsBool(key string, defaultValue *bool) *bool

	// GetAsDuration returns config value as duration or passed default value
	// if there's no value for passed key or maybe config value parsing to duration fails.
	// Unit for durations can defined with suffix "s" for seconds, "m" for minutes or "h" for hourse.
	// If there's no unit default will be seconds.
	GetAsDuration(key string, defaultValue *time.Duration) *time.Duration

	// GetSliceOfMap returns all config values as a slice of maps.
	GetAsSliceOfMaps(key string) []map[string]string
}

Config is the ain interface provides by this package to get an access point for config from different sources and formats.

type ConfigSource

type ConfigSource interface {

	// Load config depending on config loader implemanteation and
	// return it as Config.
	Load() (Config, error)
}

ConfigSource can be used to load a config from different sources and with different formats.

func NewConfigSource

func NewConfigSource() ConfigSource

NewConfigSource returns the default config loader, the ViperConfigSource.

func NewFileConfigSource

func NewFileConfigSource(configFile *string) ConfigSource

NewFileConfigSource returns a new config source for given file. If you don't passed a specific config file this source will have a lool at different places for a default config file. See Load method for more details.

func NewS3ConfigSource

func NewS3ConfigSource(bucket, key string, region *string) ConfigSource

NewS3ConfigSource returns a new S3 config source which uses passed config file from given S4 bucket. If region is nil it will try to get current aws region from environment var AWS_REGION.

func NewS3ConfigSourceFromEnv added in v1.0.4

func NewS3ConfigSourceFromEnv() (ConfigSource, error)

NewS3ConfigSourceFromEnv creates a new S3 config source using values defined by environment variables.

AWS_REGION 			- Defines AWS region the bucket is related to
GO_CONFIG_S3_BUCKET - Bucket where config file are located
GO_CONFIG_S3_KEY 	- Config file in a bucket

func NewStaticConfigSource

func NewStaticConfigSource(yamlConfig string) ConfigSource

NewStaticConfigSource returns source with given static config values.

type FileConfigSource

type FileConfigSource struct {
	// contains filtered or unexported fields
}

FileConfigSource reads a config file in YAML format using viper config.

func (*FileConfigSource) Load

func (source *FileConfigSource) Load() (Config, error)

Load reads a config file and returns a ViperConfig. It uses the config file you've set during creating this source or it tries to find a file names config.yml or testconfig.yml in following locations. - loca directory, "./" - user home, "$HOME/" - user home at go_config dir, "$HOME/go_config/" - at "/etc/go_config/"

type S3ConfigSource

type S3ConfigSource struct {
	// contains filtered or unexported fields
}

S3ConfigSource loads a YAML config from a file in a AWS S3 bucket.

func (*S3ConfigSource) Load

func (source *S3ConfigSource) Load() (Config, error)

Load config file from S3 and pass it to a ViperConfig.

type StaticConfigSource

type StaticConfigSource struct {
	// contains filtered or unexported fields
}

StaticConfigSource uses a static config passed during creating this config source.

func (*StaticConfigSource) Load

func (source *StaticConfigSource) Load() (Config, error)

Load static config. This will create a new ViperConfig with static config content.

type ViperConfig

type ViperConfig struct {
	// contains filtered or unexported fields
}

ViperConfig is a wrapper to config handler provided by github.com/spf13/viper.

func (*ViperConfig) Get

func (conf *ViperConfig) Get(key string, defaultValue *string) *string

Get try to load config value for passed key and will return given default if it's not available.

func (*ViperConfig) GetAsBool

func (conf *ViperConfig) GetAsBool(key string, defaultValue *bool) *bool

GetAsBool returns config value as bool or given default value if there's no value for this key or conversion to bool fails.

func (*ViperConfig) GetAsDuration

func (conf *ViperConfig) GetAsDuration(key string, defaultValue *time.Duration) *time.Duration

GetAsDuration returns config value as duration or passed default value if there's no value for passed key or maybe config value parsing to duration fails. Unit for durations can defined with suffix "s" for seconds, "m" for minutes or "h" for hourse. If there's no unit default will be seconds.

func (*ViperConfig) GetAsInt

func (conf *ViperConfig) GetAsInt(key string, defaultValue *int) *int

GetAsInt try to load value for given config and will try to convert it to imt. If there's no config value for passed key or conversion to int failes, it wll return given default value.

func (*ViperConfig) GetAsIntSlice

func (conf *ViperConfig) GetAsIntSlice(key string, defaultValue *[]int) *[]int

GetAsIntSlice returns a string slice of config values for passed key or return passed default value it there's no value for tis key.

func (*ViperConfig) GetAsSliceOfMaps

func (conf *ViperConfig) GetAsSliceOfMaps(key string) []map[string]string

GetAsSliceOfMaps returns local config values as slice of maps.

Jump to

Keyboard shortcuts

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