Skip to content

Commit

Permalink
Merge pull request #439 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.104.0
  • Loading branch information
andyone authored Mar 7, 2024
2 parents a4523a3 + e7b3ac4 commit 6febb77
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 90 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
## Changelog

### 12.104.0

* `[knf/united]` Added validation using `knf` validators
* `[knf/united]` Added usage examples

### 12.103.0

* `[knf/united]` Added new package for working with united configuration (_knf + options + environment variables_)
* `[knf]` Added method `GetTD`
* `[knf]` Added method `GetTS`
* `[knf]` Added method `GetTZ`
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.103.0"
const VERSION = "12.104.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
47 changes: 46 additions & 1 deletion knf/knf.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,50 @@ import (

// ////////////////////////////////////////////////////////////////////////////////// //

// IConfig is knf like configuration
type IConfig interface {
// GetS returns configuration value as string
GetS(name string, defvals ...string) string

// GetI returns configuration value as int
GetI(name string, defvals ...int) int

// GetI64 returns configuration value as int64
GetI64(name string, defvals ...int64) int64

// GetU returns configuration value as uint
GetU(name string, defvals ...uint) uint

// GetU64 returns configuration value as uint64
GetU64(name string, defvals ...uint64) uint64

// GetF returns configuration value as floating number
GetF(name string, defvals ...float64) float64

// GetB returns configuration value as boolean
GetB(name string, defvals ...bool) bool

// GetM returns configuration value as file mode
GetM(name string, defvals ...os.FileMode) os.FileMode

// GetD returns configuration values as duration
GetD(name string, mod DurationMod, defvals ...time.Duration) time.Duration

// GetTD returns configuration value as time duration
GetTD(name string, defvals ...time.Duration) time.Duration

// GetTS returns configuration timestamp value as time
GetTS(name string, defvals ...time.Time) time.Time

// GetTS returns configuration value as timezone
GetTZ(name string, defvals ...*time.Location) *time.Location

// GetL returns configuration value as list
GetL(name string, defvals ...[]string) []string
}

// ////////////////////////////////////////////////////////////////////////////////// //

// Config is basic config struct
type Config struct {
sections []string
Expand All @@ -40,7 +84,7 @@ type Validator struct {
}

// PropertyValidator is default type of property validation function
type PropertyValidator func(config *Config, prop string, value any) error
type PropertyValidator func(config IConfig, prop string, value any) error

// DurationMod is type for duration modificator
type DurationMod int64
Expand All @@ -61,6 +105,7 @@ var (
Minute = 60 * Second
Hour = 60 * Minute
Day = 24 * Hour
Week = 7 * Day
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion knf/knf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func (s *KNFSuite) TestSimpleValidator(c *check.C) {
c.Assert(global, check.NotNil)
c.Assert(err, check.IsNil)

var simpleValidator = func(config *Config, prop string, value any) error {
var simpleValidator = func(config IConfig, prop string, value any) error {
if prop == "string:test2" {
return fmt.Errorf("ERROR")
}
Expand Down
199 changes: 199 additions & 0 deletions knf/united/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package united

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"

"github.com/essentialkaos/ek/v12/knf"
"github.com/essentialkaos/ek/v12/options"
)

// ////////////////////////////////////////////////////////////////////////////////// //

func ExampleCombine() {
// Load global KNF config
err := knf.Global("/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 combine KNF configuration, options and environment variables
Combine(
Mapping{"test:option-one", "O:option-one", "TEST_OPTION_ONE"},
Mapping{"test:option-two", "k:option-two", "TEST_OPTION_TWO"},
)

// 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")
}

func ExampleGetS() {
Combine(
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"))
}

func ExampleGetB() {
Combine(
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"))
}

func ExampleGetI() {
Combine(
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"))
}

func ExampleGetI64() {
Combine(
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"))
}

func ExampleGetU() {
Combine(
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"))
}

func ExampleGetU64() {
Combine(
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"))
}

func ExampleGetF() {
Combine(
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"))
}

func ExampleGetM() {
Combine(
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"))
}

func ExampleGetD() {
Combine(
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))
}

func ExampleGetTD() {
Combine(
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"))
}

func ExampleGetTS() {
Combine(
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"))
}

func ExampleGetTZ() {
Combine(
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"))
}

func ExampleGetL() {
Combine(
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"))
}
Loading

0 comments on commit 6febb77

Please sign in to comment.