oneconf

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package oneconf will populate a central configuration struct wil data from TOML files, default, environment and command line.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateHelp

func GenerateHelp(c any, prefix string, useName, showShort, showLong, showEnv bool) string

GenerateHelp returns a string with help information

func GetArg

func GetArg(name string) string

GetArg will return the value of a short line argument -c=VAL, -c by itself is true or empty

func IsAskingForHelp

func IsAskingForHelp() bool

IsAskingForHelp return true in case command line includes -h or --help

func LoadDefaults

func LoadDefaults(c any)

LoadDefaults will set all default to the structure fields

Example
type cfg struct {
	A bool
	B bool    `default:"t"`
	C bool    `default:"1"`
	D bool    `default:"true"`
	E bool    `default:"false"`
	F int8    `default:"12"`
	G int32   `default:"-12"`
	H uint8   `default:"70"`
	I uint16  `default:"70"`
	J uint32  `default:"0x70"`
	K uint64  `default:"0o70"`
	L uint    `default:"0b10011"`
	M float32 `default:"12e-3"`
	N float64 `default:"12e+1"`
	O string  `default:"text"`
}

c := cfg{}

LoadDefaults(&c)

fmt.Println(c)
Output:

{false true true true false 12 -12 70 70 112 56 19 0.012 120 text}

func LoadEnv

func LoadEnv(c any, prefix string, useName bool)

LoadEnv set fields to a value define by a environment variable we test variables named after prefix + (tag "env" or field name)

Example
type cfg struct {
	A bool
	B bool `env:"one"`
	C bool `env:"two"`
	D bool
	E bool
	F int8
	G int32
	H uint8
	I uint16
	J uint32 `env:"THREE"`
	K uint64
	L uint
	M float32
	N float64
	O string
}

os.Setenv("PRE_ONE", "true")
os.Setenv("PRE_TWO", "T")
os.Setenv("PRE_THREE", "0x77") // used to populate J
os.Setenv("PRE_D", "T")
os.Setenv("PRE_E", "F")
os.Setenv("PRE_F", "6")
os.Setenv("PRE_G", "-8")
os.Setenv("PRE_H", "110")
os.Setenv("PRE_I", "0x110")
os.Setenv("PRE_J", "0o110") //ignored since field has a env name
os.Setenv("PRE_K", "0b110")
os.Setenv("PRE_L", "110")
os.Setenv("PRE_M", "1.28e-2")
os.Setenv("PRE_N", "3.1415")
os.Setenv("PRE_O", "my-text")

c := cfg{}

LoadEnv(&c, "PRE_", false) // populate named ones only
fmt.Println(c)

LoadEnv(&c, "PRE_", true) // populate named ones and then by field name
fmt.Println(c)
Output:

{false true true false false 0 0 0 0 119 0 0 0 0 }
{false true true true false 6 -8 110 272 119 6 110 0.0128 3.1415 my-text}

func LoadFlags

func LoadFlags(c any, useName bool)

LoadFlags set structure with values from the command line

Example
type cfg struct {
	A bool
	B bool
	C bool
	D bool
	E bool
	F int8
	G int32
	H uint8
	I uint16
	J uint32
	K uint64
	L uint
	M float32
	N float64
	O string
}

c := cfg{}
fmt.Println(c)

os.Args = []string{"binary", "-h", "-a"}

LoadFlags(&c, false) // populate named ones only
fmt.Println(c)

LoadFlags(&c, true) // populate named ones and then by field name
fmt.Println(c)
Output:

{d}

func LoadTOML

func LoadTOML(c any, file string)

LoadTOML set structure value to the TOML file content

Example
type cfg struct {
	A string
	B int
	C []string
	D struct {
		E bool
		F float64
	}
}

toml := `
A = "my-string"
B = 0x110
C = ["one", "two"]

[D]
E = true
F = 12e-1
	`

if err := os.WriteFile("_$_test.toml", []byte(toml), 0o600); err != nil {
	fmt.Println("can not save test file")
	os.Exit(1)
}

c := cfg{}

LoadTOML(&c, "_$_test.toml")

fmt.Print(c)

if err := os.Remove("_$_test.toml"); err != nil {
	fmt.Println("can not remove test file")
	os.Exit(1)
}
Output:

{my-string 272 [one two] {true 1.2}}

func ParseCommand

func ParseCommand(in []string) (bools map[string]bool, vals map[string]string, args []string)

ParseCommand will parse an []string to brake it into a list of booleans, a list of arguments and a map of key:value

Types

This section is empty.

Jump to

Keyboard shortcuts

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