@@ -214,11 +214,14 @@ func (p *Pipe) Basename() *Pipe {
214
214
215
215
// Bytes returns the contents of the pipe as a []byte, or an error.
216
216
func (p * Pipe ) Bytes () ([]byte , error ) {
217
- res , err := io .ReadAll (p )
217
+ if p .Error () != nil {
218
+ return nil , p .Error ()
219
+ }
220
+ data , err := io .ReadAll (p )
218
221
if err != nil {
219
222
p .SetError (err )
220
223
}
221
- return res , err
224
+ return data , nil
222
225
}
223
226
224
227
// Close closes the pipe's associated reader. This is a no-op if the reader is
@@ -405,9 +408,6 @@ func (p *Pipe) Exec(cmdLine string) *Pipe {
405
408
//
406
409
// ListFiles("*").ExecForEach("touch {{.}}").Wait()
407
410
func (p * Pipe ) ExecForEach (cmdLine string ) * Pipe {
408
- if p .Error () != nil {
409
- return p
410
- }
411
411
tpl , err := template .New ("" ).Parse (cmdLine )
412
412
if err != nil {
413
413
return p .WithError (err )
@@ -467,6 +467,9 @@ func (p *Pipe) ExitStatus() int {
467
467
// been fully read. Use [Pipe.Wait] to wait for all concurrent filters to
468
468
// complete.
469
469
func (p * Pipe ) Filter (filter func (io.Reader , io.Writer ) error ) * Pipe {
470
+ if p .Error () != nil {
471
+ return p
472
+ }
470
473
pr , pw := io .Pipe ()
471
474
q := NewPipe ().WithReader (pr )
472
475
go func () {
@@ -504,6 +507,9 @@ func (p *Pipe) FilterScan(filter func(string, io.Writer)) *Pipe {
504
507
// lines if there are less than n. If n is zero or negative, there is no output
505
508
// at all.
506
509
func (p * Pipe ) First (n int ) * Pipe {
510
+ if p .Error () != nil {
511
+ return p
512
+ }
507
513
if n <= 0 {
508
514
return NewPipe ()
509
515
}
@@ -639,6 +645,9 @@ func (p *Pipe) JQ(query string) *Pipe {
639
645
// if there are less than n. If n is zero or negative, there is no output at
640
646
// all.
641
647
func (p * Pipe ) Last (n int ) * Pipe {
648
+ if p .Error () != nil {
649
+ return p
650
+ }
642
651
if n <= 0 {
643
652
return NewPipe ()
644
653
}
@@ -691,6 +700,9 @@ func (p *Pipe) Post(URL string) *Pipe {
691
700
// bytes read and any error encountered. At end of file, or on a nil pipe, Read
692
701
// returns 0, [io.EOF].
693
702
func (p * Pipe ) Read (b []byte ) (int , error ) {
703
+ if p .Error () != nil {
704
+ return 0 , p .Error ()
705
+ }
694
706
return p .Reader .Read (b )
695
707
}
696
708
@@ -742,6 +754,9 @@ func (p *Pipe) SetError(err error) {
742
754
// SHA256Sum returns the hex-encoded SHA-256 hash of the entire contents of the
743
755
// pipe, or an error.
744
756
func (p * Pipe ) SHA256Sum () (string , error ) {
757
+ if p .Error () != nil {
758
+ return "" , p .Error ()
759
+ }
745
760
hasher := sha256 .New ()
746
761
_ , err := io .Copy (hasher , p )
747
762
if err != nil {
@@ -788,6 +803,9 @@ func (p *Pipe) Slice() ([]string, error) {
788
803
// [Pipe.WithStdout]), or to [os.Stdout] otherwise, and returns the number of
789
804
// bytes successfully written, together with any error.
790
805
func (p * Pipe ) Stdout () (int , error ) {
806
+ if p .Error () != nil {
807
+ return 0 , p .Error ()
808
+ }
791
809
n64 , err := io .Copy (p .stdout , p )
792
810
if err != nil {
793
811
return 0 , err
@@ -857,6 +875,9 @@ func (p *Pipe) WriteFile(path string) (int64, error) {
857
875
}
858
876
859
877
func (p * Pipe ) writeOrAppendFile (path string , mode int ) (int64 , error ) {
878
+ if p .Error () != nil {
879
+ return 0 , p .Error ()
880
+ }
860
881
out , err := os .OpenFile (path , mode , 0666 )
861
882
if err != nil {
862
883
p .SetError (err )
0 commit comments