diff --git a/CHANGELOG.md b/CHANGELOG.md index 7708270a..cff4d35c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### 12.114.0 + +- `[knf/united]` Added method `CombineSimple` + ### 12.113.1 - `[support/pkgs]` Added compatibility with macOS diff --git a/ek.go b/ek.go index 0181251b..708e222d 100644 --- a/ek.go +++ b/ek.go @@ -21,7 +21,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.113.1" +const VERSION = "12.114.0" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/fmtutil/fmtutil_test.go b/fmtutil/fmtutil_test.go index 9adf3823..c34fee40 100644 --- a/fmtutil/fmtutil_test.go +++ b/fmtutil/fmtutil_test.go @@ -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") @@ -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%") @@ -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") @@ -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") diff --git a/knf/united/example_test.go b/knf/united/example_test.go index 4baf0ff0..249335c1 100644 --- a/knf/united/example_test.go +++ b/knf/united/example_test.go @@ -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" @@ -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{} diff --git a/knf/united/united.go b/knf/united/united.go index 57ab3d70..5395bf9e 100644 --- a/knf/united/united.go +++ b/knf/united/united.go @@ -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 { @@ -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 { diff --git a/knf/united/united_test.go b/knf/united/united_test.go index 71189fef..373f9d0d 100644 --- a/knf/united/united_test.go +++ b/knf/united/united_test.go @@ -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 diff --git a/timeutil/timeutil_test.go b/timeutil/timeutil_test.go index 9617f1a8..d5f921b6 100644 --- a/timeutil/timeutil_test.go +++ b/timeutil/timeutil_test.go @@ -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") @@ -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")