Skip to content

Commit

Permalink
Merge pull request #489 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 13.2.0
  • Loading branch information
andyone authored Jul 23, 2024
2 parents 264d6d7 + bd2da6c commit fc5a6d0
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 104 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

### [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)

- `[env]` Fixed compatibility with Windows
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<p align="center">
<a href="https://kaos.sh/g/ek.v13"><img src=".github/images/godoc.svg"/></a>
<a href="https://kaos.sh/l/ek"><img src="https://kaos.sh/l/210cafc2de7bf4320649.svg" alt="Code Climate Maintainability" /></a>
<a href="https://kaos.sh/b/ek"><img src="https://kaos.sh/b/3649d737-e5b9-4465-9765-b9f4ebec60ec.svg" alt="Codebeat badge" /></a>
<a href="https://kaos.sh/y/ek"><img src="https://kaos.sh/y/e5f84d40af0e4a9ab3ff94852f93007c.svg" alt="Codacy badge" /></a>
<br/>
<a href="https://kaos.sh/w/ek/ci"><img src="https://kaos.sh/w/ek/ci.svg" alt="GitHub Actions CI Status" /></a>
Expand Down
5 changes: 0 additions & 5 deletions ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 0 additions & 5 deletions ek_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions errutil/errutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions errutil/errutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
47 changes: 33 additions & 14 deletions errutil/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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

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
11 changes: 11 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ek

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "13.2.0"

0 comments on commit fc5a6d0

Please sign in to comment.