Skip to content

Commit

Permalink
[passthru] Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jul 23, 2024
1 parent 3e6eb52 commit 88fff62
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 79 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
72 changes: 10 additions & 62 deletions passthru/passthru.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@ 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
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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion passthru/passthru_spinner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,7 +62,6 @@ func Example() {
)

spinner.Done(err == nil)
s.reader.Close()

if err != nil {
terminal.Error(err)
Expand Down
14 changes: 0 additions & 14 deletions passthru/passthru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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{})
Expand All @@ -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{})
Expand Down
4 changes: 2 additions & 2 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 88fff62

Please sign in to comment.