Skip to content

Commit

Permalink
🔥 Remove unnecessary channel
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 14, 2022
1 parent 50f60ba commit b019b7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
13 changes: 1 addition & 12 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,12 @@ func run(cmd *cobra.Command, args []string) (err error) {
fmt.Println("::set-output name=filename::" + filename)
}

ch := make(chan error, 1)
var w io.WriteCloser
switch conf.OutputFormat {
case sqlformat.Gzip, sqlformat.Custom:
w = f
close(ch)
case sqlformat.Plain:
w = gzips.NewDecompressWriter(f, ch)
if err != nil {
return err
}
w = gzips.NewDecompressWriter(f)
}
defer func(w io.WriteCloser) {
_ = w.Close()
Expand All @@ -148,12 +143,6 @@ func run(cmd *cobra.Command, args []string) (err error) {
return err
}

// Check errors in channel
err = <-ch
if err != nil {
return err
}

// Close file
err = f.Close()
if err != nil {
Expand Down
20 changes: 15 additions & 5 deletions internal/gzips/decompress_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@ import (
"io"
)

func NewDecompressWriter(dest io.Writer, ch chan error) io.WriteCloser {
func NewDecompressWriter(dest io.Writer) io.WriteCloser {
r, w := io.Pipe()
go func() {
defer func(r *io.PipeReader) {
_ = r.Close()
}(r)

gzr, err := gzip.NewReader(r)
if err != nil {
_ = r.CloseWithError(err)
return
}
defer func(gzr *gzip.Reader) {
_ = gzr.Close()
}(gzr)

_, err = io.Copy(dest, gzr)
ch <- err
if err != nil {
_ = r.CloseWithError(err)
return
}

err = gzr.Close()
if err != nil {
_ = r.CloseWithError(err)
return
}
}()
return w
}

0 comments on commit b019b7a

Please sign in to comment.