united

package
v12.120.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package united provides KNF configuration extended by environment variables and options

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	Millisecond = knf.Millisecond
	Second      = knf.Second
	Minute      = knf.Minute
	Hour        = knf.Hour
	Day         = knf.Day
	Week        = knf.Week
)

Functions

func AddOptions added in v12.111.0

func AddOptions(m options.Map, names ...string)

AddOptions adds options with knf properties to map

Example
m := options.Map{}

AddOptions(m, "test:option-one", "test:option-two")

fmt.Printf("Map size: %d\n", len(m))
Output:

Map size: 2

func Combine

func Combine(config *knf.Config, mappings ...Mapping) error

Combine applies mappings to combine knf properties, options, and environment variables

Note that the environment variable will be moved to config after combining (e.g. won't be accessible with os.Getenv)

Example
// Load KNF config
config, err := knf.Read("/path/to/your/config.knf")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

optMap := options.Map{
	"O:option-one": {},
	"k:option-two": {},
}

// Parse command-line options
_, errs := options.Parse(optMap)

if len(errs) != 0 {
	for _, err := range errs {
		fmt.Printf("Error: %v\n", err)
	}

	return
}

// Combine combines KNF configuration, options and environment variables
Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

// Also, you can set options and environment variables using helpers
var (
	optOne = "test:option-one"
	optTwo = "test:option-two"
)

Combine(
	config,
	// Create mapping manually
	Mapping{optOne, ToOption(optOne), ToEnvVar(optOne)},
	// Create simple mapping
	Simple(optTwo),
)

// Read string value
GetS("section:string")

// Read integer value
GetI("section:int")

// Read float value
GetF("section:float")

// Read boolean value
GetB("section:boolean")

// Read file mode value
GetM("section:file-mode")

// Read duration as seconds
GetD("section:duration", Second)

// Read duration as minutes
GetD("section:duration", Minute)

// Read time duration
GetTD("section:time-duration")

// Read timestamp
GetTS("section:timestamp")

// Read timezone
GetTZ("section:timezone")

// Read list
GetL("section:list")
Output:

func CombineSimple added in v12.114.0

func CombineSimple(config *knf.Config, props ...string) error

CombineSimple applies mappings to combine knf properties, options, and environment variables. This method creates simple mappings based on properties names.

Note that the environment variable will be moved to config after combining (e.g. won't be accessible with os.Getenv)

Example
// Load KNF config
config, err := knf.Read("/path/to/your/config.knf")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

optMap := options.Map{
	"O:option-one": {},
	"k:option-two": {},
}

// Parse command-line options
_, errs := options.Parse(optMap)

if len(errs) != 0 {
	for _, err := range errs {
		fmt.Printf("Error: %v\n", err)
	}

	return
}

// Combine simple combines KNF configuration, options and environment variables
CombineSimple(config, "test:option-one", "test:option-two")
Output:

func E added in v12.106.0

func E(name string) string

E is a shortcut for ToEnvVar

Example
fmt.Println(ToEnvVar("section:time-duration"))
Output:

SECTION_TIME_DURATION

func GetB

func GetB(name string, defvals ...bool) bool

GetB returns configuration value as boolean

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %t\n", GetB("user:is-admin"))
Output:

func GetD

func GetD(name string, mod DurationMod, defvals ...time.Duration) time.Duration

GetD returns configuration values as duration

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetD("user:timeout", Minute))
Output:

func GetF

func GetF(name string, defvals ...float64) float64

GetF returns configuration value as floating number

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %g\n", GetF("user:priority"))
Output:

func GetI

func GetI(name string, defvals ...int) int

GetI returns configuration value as int

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %d\n", GetI("user:uid"))
Output:

func GetI64

func GetI64(name string, defvals ...int64) int64

GetI64 returns configuration value as int64

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %d\n", GetI64("user:uid"))
Output:

func GetL

func GetL(name string, defvals ...[]string) []string

GetL returns configuration value as list

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetL("issue:labels"))
Output:

func GetM

func GetM(name string, defvals ...os.FileMode) os.FileMode

GetM returns configuration value as file mode

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetF("user:default-mode"))
Output:

func GetS

func GetS(name string, defvals ...string) string

GetS returns configuration value as string

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %s\n", GetS("user:name"))
Output:

func GetTD

func GetTD(name string, defvals ...time.Duration) time.Duration

GetTD returns configuration value as time duration

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetTD("user:timeout"))
Output:

func GetTS

func GetTS(name string, defvals ...time.Time) time.Time

GetTS returns configuration timestamp value as time

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetTS("user:created"))
Output:

func GetTZ

func GetTZ(name string, defvals ...*time.Location) *time.Location

GetTS returns configuration value as timezone

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %v\n", GetTZ("service:timezone"))
Output:

func GetU

func GetU(name string, defvals ...uint) uint

GetU returns configuration value as uint

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %d\n", GetU("user:uid"))
Output:

func GetU64

func GetU64(name string, defvals ...uint64) uint64

GetU64 returns configuration value as uint64

Example
config, _ := knf.Read("/path/to/your/config.knf")

Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

fmt.Printf("Value from config: %d\n", GetU64("user:uid"))
Output:

func O added in v12.106.0

func O(name string) string

O is a shortcut for ToOption

Example
fmt.Println(ToOption("section:time-duration"))
Output:

section-time-duration

func ToEnvVar added in v12.106.0

func ToEnvVar(name string) string

ToEnvVar converts knf property name to environment variable name

Example
fmt.Println(ToEnvVar("section:time-duration"))
Output:

SECTION_TIME_DURATION

func ToOption added in v12.106.0

func ToOption(name string) string

ToOption converts knf property name to option name

Example
fmt.Println(ToOption("section:time-duration"))
Output:

section-time-duration

func Validate added in v12.104.0

func Validate(validators []*knf.Validator) []error

Validate executes all given validators and returns slice with validation errors

Types

type DurationMod

type DurationMod = knf.DurationMod

DurationMod is type for duration modificator

type Mapping

type Mapping struct {
	Property string // Property from KNF configuration file
	Option   string // Command-line option
	Variable string // Environment variable
}

Mapping contains mapping [knf property] → [option] → [envvar]

func GetMapping added in v12.115.0

func GetMapping(prop string) Mapping

GetMapping returns mapping info for given property

Example
// Load KNF config
config, err := knf.Read("/path/to/your/config.knf")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

// Combine combines KNF configuration, options and environment variables
Combine(
	config,
	Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
	Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

// Print mapping for property test:option-one
fmt.Println(GetMapping("test:option-one"))
Output:

func Simple added in v12.107.0

func Simple(name string) Mapping

Simple creates simple mapping for knf property section:property → --section-property + SECTION_PROPERTY

Example
m := Simple("test:option-one")

fmt.Printf("%s → --%s + %s\n", m.Property, m.Option, m.Variable)
Output:

test:option-one → --test-option-one + TEST_OPTION_ONE

func (Mapping) IsEmpty

func (m Mapping) IsEmpty() bool

IsEmpty returns true if mapping is empty

Jump to

Keyboard shortcuts

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