From 486707d05f4d535d854243ab6bb07482caadbfa0 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 28 Oct 2024 13:25:59 +0300 Subject: [PATCH 1/3] [knf] Added helper 'Q' --- CHANGELOG.md | 4 ++++ knf/example_test.go | 3 +++ knf/knf.go | 6 ++++++ knf/knf_test.go | 4 ++++ version.go | 2 +- 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90d0e5c7..a03d29aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### [13.9.2](https://kaos.sh/ek/13.9.2) + +- `[knf]` Added helper `Q` + ### [13.9.1](https://kaos.sh/ek/13.9.1) - `[errors]` Fixed bug with extra newline character at the end of `Error` output diff --git a/knf/example_test.go b/knf/example_test.go index a43fb8cd..c848ae75 100644 --- a/knf/example_test.go +++ b/knf/example_test.go @@ -25,6 +25,9 @@ func ExampleGlobal() { // Read string value GetS("main:string") + // Use helper Q to create full property name + GetS(Q("main", "string")) + // Read integer value GetI("main:int") diff --git a/knf/knf.go b/knf/knf.go index 919053e5..84d89365 100644 --- a/knf/knf.go +++ b/knf/knf.go @@ -423,6 +423,12 @@ func Validate(validators Validators) errors.Errors { return global.Validate(validators) } +// Q is a helper to create a valid full property name (section + delimiter +// + property name) +func Q(section, prop string) string { + return section + _SYMBOL_DELIMITER + prop +} + // ////////////////////////////////////////////////////////////////////////////////// // // Add adds given validators and returns new slice diff --git a/knf/knf_test.go b/knf/knf_test.go index 08a33f97..0f591e3c 100644 --- a/knf/knf_test.go +++ b/knf/knf_test.go @@ -747,6 +747,10 @@ func (s *KNFSuite) TestKNFParserExceptions(c *check.C) { c.Assert(err.Error(), check.Equals, "Error at line 3: Unknown property {abcd:test}") } +func (s *KNFSuite) TestHelpers(c *check.C) { + c.Assert(Q("section", "prop"), check.Equals, "section:prop") +} + // ////////////////////////////////////////////////////////////////////////////////// // func (s *KNFSuite) BenchmarkBasic(c *check.C) { diff --git a/version.go b/version.go index fbca6c6a..cc2b131f 100644 --- a/version.go +++ b/version.go @@ -8,4 +8,4 @@ package ek // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "13.9.1" +const VERSION = "13.9.2" From 02adedea940cf87cc5e203b7b130c2d784ed9576 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 28 Oct 2024 16:52:26 +0300 Subject: [PATCH 2/3] [fmtc] Code refactoring --- CHANGELOG.md | 1 + fmtc/cond_wrapper.go | 46 +++++++++++++++++++++----------------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a03d29aa..2aad7ccc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### [13.9.2](https://kaos.sh/ek/13.9.2) - `[knf]` Added helper `Q` +- `[fmtc]` Code refactoring ### [13.9.1](https://kaos.sh/ek/13.9.1) diff --git a/fmtc/cond_wrapper.go b/fmtc/cond_wrapper.go index de730758..cd167888 100644 --- a/fmtc/cond_wrapper.go +++ b/fmtc/cond_wrapper.go @@ -11,15 +11,13 @@ import "io" // ////////////////////////////////////////////////////////////////////////////////// // -type CondWrapper struct { - match bool -} +type CondWrapper bool // ////////////////////////////////////////////////////////////////////////////////// // // If returns wrapper for printing messages if condition is true func If(cond bool) CondWrapper { - return CondWrapper{cond} + return CondWrapper(cond) } // ////////////////////////////////////////////////////////////////////////////////// // @@ -27,7 +25,7 @@ func If(cond bool) CondWrapper { // Print formats using the default formats for its operands and writes to standard // output. func (cw CondWrapper) Print(a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -38,7 +36,7 @@ func (cw CondWrapper) Print(a ...any) (int, error) { // output. Spaces are always added between operands and a newline is appended. It // returns the number of bytes written and any write error encountered. func (cw CondWrapper) Println(a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -48,7 +46,7 @@ func (cw CondWrapper) Println(a ...any) (int, error) { // Printf formats according to a format specifier and writes to standard output. It // returns the number of bytes written and any write error encountered. func (cw CondWrapper) Printf(f string, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -59,7 +57,7 @@ func (cw CondWrapper) Printf(f string, a ...any) (int, error) { // Spaces are added between operands when neither is a string. It returns the // number of bytes written and any write error encountered. func (cw CondWrapper) Fprint(w io.Writer, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -70,7 +68,7 @@ func (cw CondWrapper) Fprint(w io.Writer, a ...any) (int, error) { // Spaces are always added between operands and a newline is appended. It returns // the number of bytes written and any write error encountered. func (cw CondWrapper) Fprintln(w io.Writer, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -80,7 +78,7 @@ func (cw CondWrapper) Fprintln(w io.Writer, a ...any) (int, error) { // Fprintf formats according to a format specifier and writes to w. It returns // the number of bytes written and any write error encountered. func (cw CondWrapper) Fprintf(w io.Writer, f string, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -90,7 +88,7 @@ func (cw CondWrapper) Fprintf(w io.Writer, f string, a ...any) (int, error) { // Sprint formats using the default formats for its operands and returns the // resulting string. Spaces are added between operands when neither is a string. func (cw CondWrapper) Sprint(a ...any) string { - if !cw.match { + if !cw { return "" } @@ -100,7 +98,7 @@ func (cw CondWrapper) Sprint(a ...any) string { // Sprintf formats according to a format specifier and returns the resulting // string. func (cw CondWrapper) Sprintf(f string, a ...any) string { - if !cw.match { + if !cw { return "" } @@ -111,7 +109,7 @@ func (cw CondWrapper) Sprintf(f string, a ...any) string { // resulting string. Spaces are always added between operands and a newline is // appended. func (cw CondWrapper) Sprintln(a ...any) string { - if !cw.match { + if !cw { return "" } @@ -120,7 +118,7 @@ func (cw CondWrapper) Sprintln(a ...any) string { // TPrint removes all content on the current line and prints the new message func (cw CondWrapper) TPrint(a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -129,7 +127,7 @@ func (cw CondWrapper) TPrint(a ...any) (int, error) { // TPrintf removes all content on the current line and prints the new message func (cw CondWrapper) TPrintf(f string, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -139,7 +137,7 @@ func (cw CondWrapper) TPrintf(f string, a ...any) (int, error) { // TPrintln removes all content on the current line and prints the new message // with a new line symbol at the end func (cw CondWrapper) TPrintln(a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -149,7 +147,7 @@ func (cw CondWrapper) TPrintln(a ...any) (int, error) { // LPrint formats using the default formats for its operands and writes to standard // output limited by the text size func (cw CondWrapper) LPrint(maxSize int, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -159,7 +157,7 @@ func (cw CondWrapper) LPrint(maxSize int, a ...any) (int, error) { // LPrintf formats according to a format specifier and writes to standard output // limited by the text size func (cw CondWrapper) LPrintf(maxSize int, f string, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -169,7 +167,7 @@ func (cw CondWrapper) LPrintf(maxSize int, f string, a ...any) (int, error) { // LPrintln formats using the default formats for its operands and writes to standard // output limited by the text size func (cw CondWrapper) LPrintln(maxSize int, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -179,7 +177,7 @@ func (cw CondWrapper) LPrintln(maxSize int, a ...any) (int, error) { // TLPrint removes all content on the current line and prints the new message // limited by the text size func (cw CondWrapper) TLPrint(maxSize int, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -189,7 +187,7 @@ func (cw CondWrapper) TLPrint(maxSize int, a ...any) (int, error) { // TLPrintf removes all content on the current line and prints the new message // limited by the text size func (cw CondWrapper) TLPrintf(maxSize int, f string, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -199,7 +197,7 @@ func (cw CondWrapper) TLPrintf(maxSize int, f string, a ...any) (int, error) { // TPrintln removes all content on the current line and prints the new message // limited by the text size with a new line symbol at the end func (cw CondWrapper) TLPrintln(maxSize int, a ...any) (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -208,7 +206,7 @@ func (cw CondWrapper) TLPrintln(maxSize int, a ...any) (int, error) { // NewLine prints a newline to standard output func (cw CondWrapper) NewLine() (int, error) { - if !cw.match { + if !cw { return 0, nil } @@ -217,7 +215,7 @@ func (cw CondWrapper) NewLine() (int, error) { // Bell prints alert (bell) symbol func (cw CondWrapper) Bell() { - if !cw.match { + if !cw { return } From 5dbeb8140a02e1c11fdb35b535484803e7045070 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 28 Oct 2024 23:16:57 +0300 Subject: [PATCH 3/3] [usage] Code refactoring --- CHANGELOG.md | 1 + usage/usage.go | 48 ++++++++++++++++++++++++++------------------- usage/usage_test.go | 16 ++++++++------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aad7ccc..79b802af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[knf]` Added helper `Q` - `[fmtc]` Code refactoring +- `[usage]` Code refactoring ### [13.9.1](https://kaos.sh/ek/13.9.1) diff --git a/usage/usage.go b/usage/usage.go index 4d46196e..642900cf 100644 --- a/usage/usage.go +++ b/usage/usage.go @@ -178,16 +178,16 @@ func NewInfo(args ...string) *Info { // AddGroup adds new command group func (i *Info) AddGroup(group string) { - if i == nil { + if i == nil || group == "" { return } i.curGroup = group } -// AddCommand adds command (name, description, args) -func (i *Info) AddCommand(a ...string) *Command { - if i == nil || len(a) < 2 { +// AddCommand adds command +func (i *Info) AddCommand(name, desc string, args ...string) *Command { + if i == nil || name == "" || desc == "" { return nil } @@ -198,9 +198,9 @@ func (i *Info) AddCommand(a ...string) *Command { } cmd := &Command{ - Name: a[0], - Desc: a[1], - Args: a[2:], + Name: name, + Desc: desc, + Args: args, Group: group, info: i, } @@ -211,17 +211,17 @@ func (i *Info) AddCommand(a ...string) *Command { } // AddOption adds option (name, description, args) -func (i *Info) AddOption(a ...string) *Option { - if i == nil || len(a) < 2 { +func (i *Info) AddOption(name, desc string, args ...string) *Option { + if i == nil || name == "" || desc == "" { return nil } - long, short := parseOptionName(a[0]) + long, short := parseOptionName(name) opt := &Option{ Long: long, Short: short, - Desc: a[1], - Arg: strings.Join(a[2:], " "), + Desc: desc, + Arg: strings.Join(args, " "), info: i, } @@ -231,25 +231,33 @@ func (i *Info) AddOption(a ...string) *Option { } // AddExample adds example of application usage -func (i *Info) AddExample(a ...string) { - if i == nil || len(a) == 0 { +func (i *Info) AddExample(cmd string, desc ...string) { + if i == nil || cmd == "" { return } - a = append(a, "") + var cmdDesc string + + if len(desc) != 0 { + cmdDesc = desc[0] + } - i.Examples = append(i.Examples, &Example{a[0], a[1], false, i}) + i.Examples = append(i.Examples, &Example{cmd, cmdDesc, false, i}) } // AddRawExample adds example of application usage without command prefix -func (i *Info) AddRawExample(a ...string) { - if i == nil || len(a) == 0 { +func (i *Info) AddRawExample(cmd string, desc ...string) { + if i == nil || cmd == "" { return } - a = append(a, "") + var cmdDesc string + + if len(desc) != 0 { + cmdDesc = desc[0] + } - i.Examples = append(i.Examples, &Example{a[0], a[1], true, i}) + i.Examples = append(i.Examples, &Example{cmd, cmdDesc, true, i}) } // AddSpoiler adds spoiler diff --git a/usage/usage_test.go b/usage/usage_test.go index 817e9711..81a75f9b 100644 --- a/usage/usage_test.go +++ b/usage/usage_test.go @@ -102,27 +102,28 @@ func (s *UsageSuite) TestUsage(c *C) { info.AddSpoiler("This is usage of spoiler with {#240}c{#241}o{#242}l{#243}o{#244}r{#245}s {#246}s{#247}u{#248}p{#249}p{#250}o{#251}r{#252}t{!}") - info.AddCommand() // will be ignored + info.AddCommand("", "") // will be ignored + info.AddCommand("test", "") // will be ignored info.AddCommand("print", "Print command") info.AddGroup("Command group") - info.AddCommand("read") info.AddCommand("read", "Read command") info.AddCommand("read1", "Read command with arguments", "arg1", "arg2") info.AddCommand("read2", "Read command with optional argument and very very very {*b}long{!} and {c}colored{!} description", "?arg") - info.AddOption("t:test") + info.AddOption("", "") // will be ignored + info.AddOption("test", "") // will be ignored info.AddOption("t:test", "Test option ") info.AddOption("test1", "Test option with argument", "arg") info.AddOption("test2", "Test option with optional argument and very very very {*b}long{!} and {c}colored{!} description", "?arg") info.BoundOptions("read", "t:test", "test1") - info.AddExample() // will be ignored + info.AddExample("") // will be ignored info.AddExample("abc") info.AddExample("abc", "Example with very long description that not fits default 88 symbols limit and link https://domain.com/#EC103814B9CCB1E305CE20D6A25E681D3735D2301D5BB631B8DFA0ABB2394A99631B8DFA0ABB2394A99") - info.AddRawExample() // will be ignored + info.AddRawExample("") // will be ignored info.AddRawExample("echo 123 | myapp") info.AddRawExample("echo 123 | myapp", "Example with description") @@ -184,9 +185,10 @@ func (s *UsageSuite) TestVersionInfo(c *C) { func (s *UsageSuite) TestNils(c *C) { var i *Info + c.Assert(func() { i.AddGroup("test") }, NotPanics) - c.Assert(func() { i.AddCommand("test") }, NotPanics) - c.Assert(func() { i.AddOption("test") }, NotPanics) + c.Assert(func() { i.AddCommand("test", "test") }, NotPanics) + c.Assert(func() { i.AddOption("test", "test") }, NotPanics) c.Assert(func() { i.AddExample("test") }, NotPanics) c.Assert(func() { i.AddRawExample("test") }, NotPanics) c.Assert(func() { i.AddSpoiler("test") }, NotPanics)