config

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 3 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(c Configuration) error

Init() initalize viper with the given Configuration.

Example
package main

import (
	"fmt"

	"github.com/spf13/viper"
	"github.com/tilseiffert/go-tools-config/config"
)

func main() {

	// preparation for test environment
	viper.Reset() // not needed for productive usage

	// prepare
	conf := config.New()

	optionName := conf.NewOption("NAME", "Tilmann", false, "")

	// initialize
	config.Init(conf)
	viper.SetTypeByDefaultValue(true)

	// do your stuff
	fmt.Printf("Hello %s\n", optionName.Get())

}
Output:

Hello Tilmann

Types

type Configuration

type Configuration struct {
	Options               []Option // list of used options
	SetTypeByDefaultValue bool     // set type of option by default value
	SetAutomaticEnv       bool     // set automatic env
	ConfigPaths           []string // list of config paths
	ConfigName            string   // name of config file
}

func New added in v0.3.0

func New() Configuration

New() creates default configuration: - SetTypeByDefaultValue: true - SetAutomaticEnv: true, - ConfigName: "", (no configuration file will be used)

func (*Configuration) AddOption added in v0.2.0

func (c *Configuration) AddOption(o Option) *Option

AddOption() appends the given option to the configuration options-array and returns a pointer to the newly appended option.

func (*Configuration) NewOption added in v0.2.0

func (c *Configuration) NewOption(name string, defaultValue interface{}, checkNotEmpty bool, helpMessage string) *Option

NewOpption() creates and adds an option with the given values to the options-array and returns a pointer to the new option asdf asdf adsf asdf

func (*Configuration) NewStringOption added in v0.2.0

func (c *Configuration) NewStringOption(name, helpMessage string) *Option

NewStrongOpption() creates and adds an option with the given values and an empty default-value to the options-array and returns a pointer to the new option.

type ErrConfigOptionsEmpty added in v0.3.0

type ErrConfigOptionsEmpty struct {
	FailedOptions []string
}

func (ErrConfigOptionsEmpty) Error added in v0.3.0

func (e ErrConfigOptionsEmpty) Error() string

define Error() method on the struct

type Option

type Option struct {
	Name          string
	Default       interface{}
	HelpMessage   string
	CheckNotEmpty bool // only implemented for string and int
}

func (*Option) Get added in v0.2.0

func (o *Option) Get() interface{}

Get() retrieves the current option from viper and returns its value.

func (*Option) GetBool added in v0.4.0

func (o *Option) GetBool() bool

GetBool() retrieves the current option from viper and returns its value as bool.

func (*Option) GetFloat64 added in v0.4.0

func (o *Option) GetFloat64() float64

GetFloat64() retrieves the current option from viper and returns its value as float64.

func (*Option) GetInt added in v0.4.0

func (o *Option) GetInt() int

GetInt() retrieves the current option from viper and returns its value as int.

func (*Option) GetString added in v0.4.0

func (o *Option) GetString() string

GetString() retrieves the current option from viper and returns its value as string.

Jump to

Keyboard shortcuts

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