github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/readall/readall.go (about)

     1  package readall
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  
     7  	"github.com/lmorg/murex/builtins/pipes/streams"
     8  )
     9  
    10  // ReadAll is a context aware equivalent to ioutils.ReadAll
    11  func ReadAll(ctx context.Context, r io.Reader) (b []byte, err error) {
    12  	w := streams.NewStdinWithContext(ctx, nil)
    13  
    14  	_, err = w.ReadFrom(r)
    15  	if err != nil {
    16  		return
    17  	}
    18  
    19  	return w.ReadAll()
    20  }
    21  
    22  // WithCancel is a context aware equivalent to ioutils.ReadAll
    23  func WithCancel(ctx context.Context, cancel context.CancelFunc, r io.Reader) (b []byte, err error) {
    24  	w := streams.NewStdinWithContext(ctx, cancel)
    25  
    26  	_, err = w.ReadFrom(r)
    27  	if err != nil {
    28  		return
    29  	}
    30  
    31  	return w.ReadAll()
    32  }