Skip to content

Commit

Permalink
Merge pull request #455 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 12.114.0
  • Loading branch information
andyone authored Apr 3, 2024
2 parents 2a47332 + a1deaee commit c32ecc2
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.114.0

- `[knf/united]` Added method `CombineSimple`

### 12.113.1

- `[support/pkgs]` Added compatibility with macOS
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.113.1"
const VERSION = "12.114.0"

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

Expand Down
10 changes: 5 additions & 5 deletions fmtutil/fmtutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var _ = Suite(&FmtUtilSuite{})

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

func (s *FmtUtilSuite) TestPretyNum(c *C) {
func (s *FmtUtilSuite) TestPrettyNum(c *C) {
c.Assert(PrettyNum(999), Equals, "999")
c.Assert(PrettyNum(1000), Equals, "1,000")
c.Assert(PrettyNum(1234567890), Equals, "1,234,567,890")
Expand All @@ -43,14 +43,14 @@ func (s *FmtUtilSuite) TestPretyNum(c *C) {
c.Assert(PrettyNum("abcd"), Equals, "abcd")
}

func (s *FmtUtilSuite) TestPretyDiff(c *C) {
func (s *FmtUtilSuite) TestPrettyDiff(c *C) {
c.Assert(PrettyDiff(0), Equals, "0")
c.Assert(PrettyDiff(100), Equals, "+100")
c.Assert(PrettyDiff(15620), Equals, "+15,620")
c.Assert(PrettyDiff(-15620), Equals, "-15,620")
}

func (s *FmtUtilSuite) TestPretyPerc(c *C) {
func (s *FmtUtilSuite) TestPrettyPerc(c *C) {
c.Assert(PrettyPerc(0.12), Equals, "0.12%")
c.Assert(PrettyPerc(1), Equals, "1%")
c.Assert(PrettyPerc(1.123), Equals, "1.12%")
Expand All @@ -59,7 +59,7 @@ func (s *FmtUtilSuite) TestPretyPerc(c *C) {
c.Assert(PrettyPerc(0.0002), Equals, "< 0.01%")
}

func (s *FmtUtilSuite) TestPretySize(c *C) {
func (s *FmtUtilSuite) TestPrettySize(c *C) {
c.Assert(PrettySize(0), Equals, "0B")
c.Assert(PrettySize(345), Equals, "345B")
c.Assert(PrettySize(1025), Equals, "1KB")
Expand All @@ -83,7 +83,7 @@ func (s *FmtUtilSuite) TestPretySize(c *C) {
c.Assert(PrettySize(math.NaN()), Equals, "0B")
}

func (s *FmtUtilSuite) TestPretyBool(c *C) {
func (s *FmtUtilSuite) TestPrettyBool(c *C) {
c.Assert(PrettyBool(true), Equals, "Y")
c.Assert(PrettyBool(false), Equals, "N")
c.Assert(PrettyBool(true, "Yes"), Equals, "Yes")
Expand Down
33 changes: 31 additions & 2 deletions knf/united/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func ExampleCombine() {
return
}

// Combine combine KNF configuration, options and environment variables
// 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
// Also, you can set options and environment variables using helpers
var (
optOne = "test:option-one"
optTwo = "test:option-two"
Expand Down Expand Up @@ -96,6 +96,35 @@ func ExampleCombine() {
GetL("section:list")
}

func ExampleCombineSimple() {
// 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")
}

func ExampleAddOptions() {
m := options.Map{}

Expand Down
30 changes: 29 additions & 1 deletion knf/united/united.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var optionGetFunc = options.GetS

// 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)
func Combine(config *knf.Config, mappings ...Mapping) error {
Expand All @@ -90,6 +90,34 @@ func Combine(config *knf.Config, mappings ...Mapping) error {
return nil
}

// 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)
func CombineSimple(config *knf.Config, props ...string) error {
if config == nil {
return knf.ErrNilConfig
}

global = &united{
knf: config,
mappings: make(map[string]Mapping),
env: make(map[string]string),
}

for _, p := range props {
m := Simple(p)

global.mappings[m.Property] = m
global.env[m.Variable] = os.Getenv(m.Variable)

os.Setenv(m.Variable, "")
}

return nil
}

// AddOptions adds options with knf properties to map
func AddOptions(m options.Map, names ...string) {
if m == nil {
Expand Down
8 changes: 8 additions & 0 deletions knf/united/united_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,14 @@ func (s *UnitedSuite) TestWithEnv(c *C) {
c.Assert(GetL("test:list"), DeepEquals, []string{"Test1Env", "Test2Env"})
}

func (s *UnitedSuite) TestCombineSimple(c *C) {
err := CombineSimple(nil, "test:string")
c.Assert(err, NotNil)

err = CombineSimple(s.config, "test:string")
c.Assert(err, IsNil)
}

func (s *UnitedSuite) TestValidation(c *C) {
global = nil

Expand Down
4 changes: 2 additions & 2 deletions timeutil/timeutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type TimeUtilSuite struct{}

var _ = Suite(&TimeUtilSuite{})

func (s *TimeUtilSuite) TestPretyDuration(c *C) {
func (s *TimeUtilSuite) TestPrettyDuration(c *C) {
c.Assert(PrettyDuration(time.Duration(59000000000)), Equals, "59 seconds")
c.Assert(PrettyDuration(120), Equals, "2 minutes")
c.Assert(PrettyDuration(int16(120)), Equals, "2 minutes")
Expand Down Expand Up @@ -61,7 +61,7 @@ func (s *TimeUtilSuite) TestPrettyDurationSimple(c *C) {
c.Assert(PrettyDurationSimple(4523412), Equals, "52 days")
}

func (s *TimeUtilSuite) TestPretyDurationInDays(c *C) {
func (s *TimeUtilSuite) TestPrettyDurationInDays(c *C) {
c.Assert(PrettyDurationInDays("ABC"), Equals, "")
c.Assert(PrettyDurationInDays(120), Equals, "1 day")
c.Assert(PrettyDurationInDays(7200), Equals, "1 day")
Expand Down

0 comments on commit c32ecc2

Please sign in to comment.