Documentation ¶
Overview ¶
Package united provides KNF configuration extended by environment variables and options
Index ¶
- Variables
- func AddOptions(m options.Map, names ...string)
- func Combine(config *knf.Config, mappings ...Mapping) error
- func CombineSimple(config *knf.Config, props ...string) error
- func E(name string) string
- func GetB(name string, defvals ...bool) bool
- func GetD(name string, mod DurationMod, defvals ...time.Duration) time.Duration
- func GetF(name string, defvals ...float64) float64
- func GetI(name string, defvals ...int) int
- func GetI64(name string, defvals ...int64) int64
- func GetL(name string, defvals ...[]string) []string
- func GetM(name string, defvals ...os.FileMode) os.FileMode
- func GetS(name string, defvals ...string) string
- func GetTD(name string, defvals ...time.Duration) time.Duration
- func GetTS(name string, defvals ...time.Time) time.Time
- func GetTZ(name string, defvals ...*time.Location) *time.Location
- func GetU(name string, defvals ...uint) uint
- func GetU64(name string, defvals ...uint64) uint64
- func O(name string) string
- func ToEnvVar(name string) string
- func ToOption(name string) string
- func Validate(validators []*knf.Validator) []error
- type DurationMod
- type Mapping
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func AddOptions ¶ added in v12.111.0
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 ¶
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
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
E is a shortcut for ToEnvVar
Example ¶
fmt.Println(ToEnvVar("section:time-duration"))
Output: SECTION_TIME_DURATION
func GetB ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
O is a shortcut for ToOption
Example ¶
fmt.Println(ToOption("section:time-duration"))
Output: section-time-duration
func ToEnvVar ¶ added in v12.106.0
ToEnvVar converts knf property name to environment variable name
Example ¶
fmt.Println(ToEnvVar("section:time-duration"))
Output: SECTION_TIME_DURATION
Types ¶
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
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
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