From 3b9b9adc0da18b16a6fdeeac2375b6c0948a3862 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sun, 21 Jul 2024 15:28:22 +0300 Subject: [PATCH 1/4] [errutil] Add method 'Wrap' --- CHANGELOG.md | 4 ++++ errutil/errutil.go | 5 +++++ errutil/errutil_test.go | 10 +++++++++ errutil/example_test.go | 47 +++++++++++++++++++++++++++++------------ 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 009047e7..91181ea2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### [13.2.0](https://kaos.sh/ek/13.2.0) + +- `[errutil]` Added method `Wrap` + ### [13.1.0](https://kaos.sh/ek/13.1.0) - `[env]` Fixed compatibility with Windows diff --git a/errutil/errutil.go b/errutil/errutil.go index a501dbb7..21e2f846 100644 --- a/errutil/errutil.go +++ b/errutil/errutil.go @@ -37,6 +37,11 @@ func NewErrors(capacity ...int) *Errors { return &Errors{capacity: size} } +// Wrap wraps slice of errors into Errors struct +func Wrap(errs []error) *Errors { + return &Errors{errors: errs} +} + // Chain executes functions in chain and if one of them return error // this function stop chain execution and return this error func Chain(funcs ...func() error) error { diff --git a/errutil/errutil_test.go b/errutil/errutil_test.go index 659ce518..1e447289 100644 --- a/errutil/errutil_test.go +++ b/errutil/errutil_test.go @@ -174,3 +174,13 @@ func (s *ErrSuite) TestChain(c *C) { c.Assert(Chain(f1, f2, f3), NotNil) c.Assert(Chain(f1, f3), IsNil) } + +func (s *ErrSuite) TestWrap(c *C) { + errs := Wrap([]error{ + errors.New("Error 1"), + errors.New("Error 2"), + }) + + c.Assert(errs.Num(), Equals, 2) + c.Assert(errs.Last(), DeepEquals, errors.New("Error 2")) +} diff --git a/errutil/example_test.go b/errutil/example_test.go index 9f9fd8e4..ecda5e77 100644 --- a/errutil/example_test.go +++ b/errutil/example_test.go @@ -13,20 +13,6 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // -func ExampleChain() { - f1 := func() error { return nil } - f2 := func() error { return nil } - f3 := func() error { return fmt.Errorf("Error 3") } - f4 := func() error { return fmt.Errorf("Error 4") } - - err := Chain(f1, f2, f3, f4) - - fmt.Println(err.Error()) - - // Output: - // Error 3 -} - func ExampleErrors() { f1 := func() error { return nil } f2 := func() error { return nil } @@ -58,6 +44,39 @@ func ExampleErrors() { // Has errors: true } +func ExampleChain() { + f1 := func() error { return nil } + f2 := func() error { return nil } + f3 := func() error { return fmt.Errorf("Error 3") } + f4 := func() error { return fmt.Errorf("Error 4") } + + err := Chain(f1, f2, f3, f4) + + fmt.Println(err.Error()) + + // Output: + // Error 3 +} + +func ExampleWrap() { + e := []error{ + fmt.Errorf("Error 1"), + fmt.Errorf("Error 2"), + fmt.Errorf("Error 3"), + } + + errs := Wrap(e) + + fmt.Printf("Last error text: %v\n", errs.Last().Error()) + fmt.Printf("Number of errors: %d\n", errs.Num()) + fmt.Printf("Has errors: %t\n", errs.HasErrors()) + + // Output: + // Last error text: Error 3 + // Number of errors: 3 + // Has errors: true +} + func ExampleErrors_Add() { var myErrs Errors From 3e6eb52cd1a39ceb174c70b0a931549c8077ba68 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sun, 21 Jul 2024 15:29:36 +0300 Subject: [PATCH 2/4] Version bump --- ek.go | 5 ----- ek_windows.go | 5 ----- version.go | 11 +++++++++++ 3 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 version.go diff --git a/ek.go b/ek.go index bf3c4a27..9dda1ff2 100644 --- a/ek.go +++ b/ek.go @@ -20,11 +20,6 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // -// VERSION is current ek package version -const VERSION = "13.1.0" - -// ////////////////////////////////////////////////////////////////////////////////// // - // worthless is used as dependency fix func worthless() { linenoise.Clear() diff --git a/ek_windows.go b/ek_windows.go index 154e0f60..271110da 100644 --- a/ek_windows.go +++ b/ek_windows.go @@ -13,11 +13,6 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // -// VERSION is current ek package version -const VERSION = "0.0.0" - -// ////////////////////////////////////////////////////////////////////////////////// // - // worthless is used as dependency fix func worthless() { bcrypt.Cost(nil) diff --git a/version.go b/version.go new file mode 100644 index 00000000..d4e66f61 --- /dev/null +++ b/version.go @@ -0,0 +1,11 @@ +package ek + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2024 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +// VERSION is current ek package version +const VERSION = "13.2.0" From 88fff62596c79b706c2a1cd5da5a4aaaa10da18a Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 23 Jul 2024 03:07:22 +0300 Subject: [PATCH 3/4] [passthru] Improvements --- CHANGELOG.md | 2 + passthru/passthru.go | 72 +++++-------------------------- passthru/passthru_spinner_test.go | 3 +- passthru/passthru_test.go | 14 ------ progress/progress.go | 4 +- 5 files changed, 16 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91181ea2..0189edd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ### [13.2.0](https://kaos.sh/ek/13.2.0) - `[errutil]` Added method `Wrap` +- `[passthru]` `Reader` now implements only `io.Reader` interface, not `io.ReadCloser` +- `[passthru]` `Writer` now implements only `io.Writer` interface, not `io.WriteCloser` ### [13.1.0](https://kaos.sh/ek/13.1.0) diff --git a/passthru/passthru.go b/passthru/passthru.go index dfd3efe2..25501fba 100644 --- a/passthru/passthru.go +++ b/passthru/passthru.go @@ -25,10 +25,9 @@ type Reader struct { Calculator *Calculator Update func(n int) - r io.ReadCloser - current int64 - total int64 - isClosed *atomic.Bool + r io.Reader + current int64 + total int64 } // Writer is pass-thru Writer @@ -36,10 +35,9 @@ type Writer struct { Calculator *Calculator Update func(n int) - w io.WriteCloser - current int64 - total int64 - isClosed *atomic.Bool + w io.Writer + current int64 + total int64 } // Calculator calculates pass-thru speed and remaining time @@ -64,21 +62,13 @@ var ( // ////////////////////////////////////////////////////////////////////////////////// // // NewReader creates new passthru reader -func NewReader(reader io.ReadCloser, total int64) *Reader { - return &Reader{ - r: reader, - total: total, - isClosed: &atomic.Bool{}, - } +func NewReader(reader io.Reader, total int64) *Reader { + return &Reader{r: reader, total: total} } // NewWriter creates new passthru writer -func NewWriter(writer io.WriteCloser, total int64) *Writer { - return &Writer{ - w: writer, - total: total, - isClosed: &atomic.Bool{}, - } +func NewWriter(writer io.Writer, total int64) *Writer { + return &Writer{w: writer, total: total} } // NewCalculator creates new Calculator struct @@ -160,27 +150,6 @@ func (r *Reader) Speed() (float64, time.Duration) { return r.Calculator.Calculate(atomic.LoadInt64(&r.current)) } -// Close closes the reader -func (r *Reader) Close() error { - if r == nil { - return ErrNilReader - } - - atomic.StoreInt64(&r.current, 0) - r.isClosed.Store(true) - - return r.r.Close() -} - -// IsClosed returns true if reader is closed -func (r *Reader) IsClosed() bool { - if r == nil { - return true - } - - return r.isClosed.Load() -} - // ////////////////////////////////////////////////////////////////////////////////// // // Write implements the standard Write interface @@ -251,27 +220,6 @@ func (w *Writer) Speed() (float64, time.Duration) { return w.Calculator.Calculate(atomic.LoadInt64(&w.current)) } -// Close closes the writer -func (w *Writer) Close() error { - if w == nil { - return ErrNilWriter - } - - atomic.StoreInt64(&w.current, 0) - w.isClosed.Store(true) - - return w.w.Close() -} - -// IsClosed returns true if writer is closed -func (w *Writer) IsClosed() bool { - if w == nil { - return true - } - - return w.isClosed.Load() -} - // ////////////////////////////////////////////////////////////////////////////////// // // Calculate calculates speed and remaining time diff --git a/passthru/passthru_spinner_test.go b/passthru/passthru_spinner_test.go index c765ee07..6b1fabc8 100644 --- a/passthru/passthru_spinner_test.go +++ b/passthru/passthru_spinner_test.go @@ -48,6 +48,8 @@ func Example() { return } + defer resp.Body.Close() + r := passthru.NewReader(resp.Body, resp.ContentLength) s := &DLSpinner{file: filename, reader: r} s.reader.Update = s.Update @@ -60,7 +62,6 @@ func Example() { ) spinner.Done(err == nil) - s.reader.Close() if err != nil { terminal.Error(err) diff --git a/passthru/passthru_test.go b/passthru/passthru_test.go index 6e672898..f41c5693 100644 --- a/passthru/passthru_test.go +++ b/passthru/passthru_test.go @@ -53,8 +53,6 @@ func (s *PassthruSuite) TestNil(c *C) { c.Assert(rs, Equals, 0.0) c.Assert(rr, Equals, time.Duration(0)) - c.Assert(r.Close(), Equals, ErrNilReader) - c.Assert(r.IsClosed(), Equals, true) c.Assert(func() { r.SetTotal(1) }, NotPanics) var w *Writer @@ -69,8 +67,6 @@ func (s *PassthruSuite) TestNil(c *C) { c.Assert(ws, Equals, 0.0) c.Assert(wr, Equals, time.Duration(0)) - c.Assert(w.Close(), Equals, ErrNilWriter) - c.Assert(w.IsClosed(), Equals, true) c.Assert(func() { w.SetTotal(1) }, NotPanics) var cl *Calculator @@ -99,16 +95,11 @@ func (s *PassthruSuite) TestReader(c *C) { r.SetTotal(2000) c.Assert(r.Total(), Equals, int64(2000)) - c.Assert(r.IsClosed(), Equals, false) rs, _ := r.Speed() c.Assert(rs, Not(Equals), 0.0) - c.Assert(r.Close(), IsNil) - c.Assert(r.Current(), Equals, int64(0)) - c.Assert(r.IsClosed(), Equals, true) - r = NewReader(&DummyReader{setError: true}, 1000) _, err = r.Read([]byte{}) @@ -132,16 +123,11 @@ func (s *PassthruSuite) TestWriter(c *C) { w.SetTotal(2000) c.Assert(w.Total(), Equals, int64(2000)) - c.Assert(w.IsClosed(), Equals, false) ws, _ := w.Speed() c.Assert(ws, Not(Equals), 0.0) - c.Assert(w.Close(), IsNil) - c.Assert(w.Current(), Equals, int64(0)) - c.Assert(w.IsClosed(), Equals, true) - w = NewWriter(&DummyWriter{setError: true}, 1000) _, err = w.Write([]byte{}) diff --git a/progress/progress.go b/progress/progress.go index 2d1f49ec..ab2f9034 100644 --- a/progress/progress.go +++ b/progress/progress.go @@ -311,7 +311,7 @@ func (b *Bar) IsStarted() bool { } // Reader creates and returns pass thru-proxy reader -func (b *Bar) Reader(r io.ReadCloser) io.ReadCloser { +func (b *Bar) Reader(r io.Reader) io.Reader { if b == nil { return nil } @@ -324,7 +324,7 @@ func (b *Bar) Reader(r io.ReadCloser) io.ReadCloser { } // Writer creates and returns pass-thru proxy reader -func (b *Bar) Writer(w io.WriteCloser) io.WriteCloser { +func (b *Bar) Writer(w io.Writer) io.Writer { if b == nil { return nil } From bd2da6ce323ad3e9657becd3b11a9fd630bbcef4 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 23 Jul 2024 03:07:50 +0300 Subject: [PATCH 4/4] Remove codebeat badge --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 503728a6..80225257 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,6 @@

Code Climate Maintainability - Codebeat badge Codacy badge
GitHub Actions CI Status