From 88fff62596c79b706c2a1cd5da5a4aaaa10da18a Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 23 Jul 2024 03:07:22 +0300 Subject: [PATCH] [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 }