config

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2019 License: MPL-2.0 Imports: 7 Imported by: 2

README

Travis Build Status CircleCI

go-config

This is a relatively simple wrapper around configuring projects that I've been using as a pattern for a while.

I've pulled it out of one of the original projects, generalised it better, and gave it a better API.

Why?

The problem I was facing was two-fold:

  • Configuring logging in a way that I can easily manipulate it from config (args, env-vars, etc)
  • Adding new config items from anywhere in my code; i.e. have it tied more-closely to the piece of code that needs it, rather than in main.go and then having to pass it through.

Tools like Flags and Viper support me really well in configuration, but I was still having to write the same pattern repetitively, and that seemed a good candidate for abstraction.

Also, whilst they are technically separated, configuring logging seemed to have some overlaps in need: they both need to be configured early in the run-time of a tool, and there was interdependence in a few places, e.g. enabling debug logging, sending log-messages (and errors) somewhere.

How to use it?

By Example

The godoc doc's are the definitive guide, but a few simple exmples are:

Importing it:

import "github.com/mexisme/go-config"

Initialising:

func init() {
	config.Init(config.Config{
		File:       ".subprocess",
		EnvPrefix:  "subprocess",
		Name:       version.Application(),
		Release:    version.Release(),
		FromConfig: true,
	})
}

Adding new config items (in another file):

func init() {
	config.AddConfigItems([]string{"http.cache.url"})
}

[...]

func (s *Service) FromConfig() *Service {
	config.ApplyWith("http.cache.url", func(val interface{}) {
		s.SetURL(val.(string))
	})

	return s
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddConfigItems

func AddConfigItems(configKeys []string)

AddConfigItems passes the configItems through to settings.AddConfigItems()

func AddConfigItemsWithPFlags added in v0.2.6

func AddConfigItemsWithPFlags(configKeys []string) error

AddConfigItemsWithFlags passes the configItems through to settings.AddConfigItemsWithFlags()

func AllSettings added in v0.2.3

func AllSettings() map[string]interface{}

AllSettings forwards to viper.AllSettings

func ApplyWith

func ApplyWith(key string, f func(interface{}))

ApplyWith gets a setting from viper, and passes it to a closure

func DryRun

func DryRun(reason string, args ...interface{}) bool

DryRun says whether the dry_run config has been set

func FromStringOrFunc added in v0.2.0

func FromStringOrFunc(val interface{}) (string, error)

FromStringOrFunc will return a different value depending on the provided val: - If it's a string, provide the given val - If it's a func(), provide the val returned by the func

func Get added in v0.2.3

func Get(key string) interface{}

Get forwards to viper.Get

func GetBool added in v0.2.3

func GetBool(key string) bool

GetBool forwards to viper.GetBool

func GetDuration added in v0.2.3

func GetDuration(key string) time.Duration

GetDuration forwards to viper.GetDuration

func GetFloat64 added in v0.2.3

func GetFloat64(key string) float64

GetFloat64 forwards to viper.GetFloat64

func GetInt added in v0.2.3

func GetInt(key string) int

GetInt forwards to viper.GetInt

func GetString added in v0.2.3

func GetString(key string) string

GetString forwards to viper.GetString

func GetStringMap added in v0.2.3

func GetStringMap(key string) map[string]interface{}

GetStringMap forwards to viper.GetStringMap

func GetStringMapString added in v0.2.3

func GetStringMapString(key string) map[string]string

GetStringMapString forwards to viper.GetStringMapString

func GetStringSlice added in v0.2.3

func GetStringSlice(key string) []string

GetStringSlice forwards to viper.GetStringSlice

func GetTime added in v0.2.3

func GetTime(key string) time.Time

GetTime forwards to viper.GetTime

func Init

func Init(initConfig Config)

Init forwards to the "config" struct's Init()

func IsSet added in v0.2.3

func IsSet(key string) bool

IsSet forwards to viper.IsSet

func MissingValueIsEmpty added in v0.3.0

func MissingValueIsEmpty(val interface{}, err error) interface{}

MissingValueIsEmpty panics if "err" != nil or is a "MissingValue" type, otherwise it returns the "val"

func Must added in v0.3.0

func Must(val interface{}, err error) interface{}

Must panics if "err" != nil, otherwise it returns the the "val"

Types

type Config

type Config struct {
	File       string
	Dir        string
	OnlyUseDir bool
	EnvPrefix  string
	Debug      bool

	FromConfig bool

	Name             interface{}
	Environment      interface{}
	Release          interface{}
	LoggingFormat    string
	LoggingSentryDsn string
	// contains filtered or unexported fields
}

Config provides basic fields for configuring the "settings" and "logging" packages.

"File" is the name of a file that Viper will read for configuration. By default, it searches for the file in the user's `$HOME` dir as well as the current workig dir -- but see "OnlyUseDir" below. If the file-name is empty, settings won't be loaded from a file (only env-vars).

"Dir" is an optional additional additional dir to search for a config file.

"OnlyUseDir" when false will additionally search "$HOME" and current working dir for the config file. When true, will only search in the above "Dir" directory. If "Dir" is not given, then the config file won't be loaded.

"EnvPrefix" is a required prefix-string that Viper uses to filter Env-vars for settings.

"Debug" enables debug logging if set to "true":

"FromConfig" enables the following settings (Name ... LoggingSentryDsn) to be configured via Viper. This means it will use the above Config file and appropriate Env-vars

"Name" is the App name, used in log messages. This can be a string, or a func ref that will return a string.

"Environment" is the App's environment it was run in -- e.g. "staging" or "prod" This can be a string, or a func ref that will return a string.

"Release" is the App's release / version string This can be a string, or a func ref that will return a string.

"LoggingFormat" sets the log-out format for log messages

"LoggingSentryDsn" is the connection string (DSN) used to send errors to Sentry.io

func (*Config) Init added in v0.3.0

func (s *Config) Init() *Config

Init is to allow other packages to easily depend on this one, since most of the important logic is in init()

type MissingValue added in v0.3.0

type MissingValue struct {
	Val interface{}
}

func (*MissingValue) Error added in v0.3.0

func (s *MissingValue) Error() string

type UnsupportedValue added in v0.3.0

type UnsupportedValue struct {
	Val interface{}
}

func (*UnsupportedValue) Error added in v0.3.0

func (s *UnsupportedValue) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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