Skip to content

Commit 31e02cb

Browse files
committed
short-circuit on error (fixes #141)
1 parent eeb0b11 commit 31e02cb

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

script.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,14 @@ func (p *Pipe) Basename() *Pipe {
214214

215215
// Bytes returns the contents of the pipe as a []byte, or an error.
216216
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)
218221
if err != nil {
219222
p.SetError(err)
220223
}
221-
return res, err
224+
return data, nil
222225
}
223226

224227
// 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 {
405408
//
406409
// ListFiles("*").ExecForEach("touch {{.}}").Wait()
407410
func (p *Pipe) ExecForEach(cmdLine string) *Pipe {
408-
if p.Error() != nil {
409-
return p
410-
}
411411
tpl, err := template.New("").Parse(cmdLine)
412412
if err != nil {
413413
return p.WithError(err)
@@ -467,6 +467,9 @@ func (p *Pipe) ExitStatus() int {
467467
// been fully read. Use [Pipe.Wait] to wait for all concurrent filters to
468468
// complete.
469469
func (p *Pipe) Filter(filter func(io.Reader, io.Writer) error) *Pipe {
470+
if p.Error() != nil {
471+
return p
472+
}
470473
pr, pw := io.Pipe()
471474
q := NewPipe().WithReader(pr)
472475
go func() {
@@ -504,6 +507,9 @@ func (p *Pipe) FilterScan(filter func(string, io.Writer)) *Pipe {
504507
// lines if there are less than n. If n is zero or negative, there is no output
505508
// at all.
506509
func (p *Pipe) First(n int) *Pipe {
510+
if p.Error() != nil {
511+
return p
512+
}
507513
if n <= 0 {
508514
return NewPipe()
509515
}
@@ -639,6 +645,9 @@ func (p *Pipe) JQ(query string) *Pipe {
639645
// if there are less than n. If n is zero or negative, there is no output at
640646
// all.
641647
func (p *Pipe) Last(n int) *Pipe {
648+
if p.Error() != nil {
649+
return p
650+
}
642651
if n <= 0 {
643652
return NewPipe()
644653
}
@@ -691,6 +700,9 @@ func (p *Pipe) Post(URL string) *Pipe {
691700
// bytes read and any error encountered. At end of file, or on a nil pipe, Read
692701
// returns 0, [io.EOF].
693702
func (p *Pipe) Read(b []byte) (int, error) {
703+
if p.Error() != nil {
704+
return 0, p.Error()
705+
}
694706
return p.Reader.Read(b)
695707
}
696708

@@ -742,6 +754,9 @@ func (p *Pipe) SetError(err error) {
742754
// SHA256Sum returns the hex-encoded SHA-256 hash of the entire contents of the
743755
// pipe, or an error.
744756
func (p *Pipe) SHA256Sum() (string, error) {
757+
if p.Error() != nil {
758+
return "", p.Error()
759+
}
745760
hasher := sha256.New()
746761
_, err := io.Copy(hasher, p)
747762
if err != nil {
@@ -788,6 +803,9 @@ func (p *Pipe) Slice() ([]string, error) {
788803
// [Pipe.WithStdout]), or to [os.Stdout] otherwise, and returns the number of
789804
// bytes successfully written, together with any error.
790805
func (p *Pipe) Stdout() (int, error) {
806+
if p.Error() != nil {
807+
return 0, p.Error()
808+
}
791809
n64, err := io.Copy(p.stdout, p)
792810
if err != nil {
793811
return 0, err
@@ -857,6 +875,9 @@ func (p *Pipe) WriteFile(path string) (int64, error) {
857875
}
858876

859877
func (p *Pipe) writeOrAppendFile(path string, mode int) (int64, error) {
878+
if p.Error() != nil {
879+
return 0, p.Error()
880+
}
860881
out, err := os.OpenFile(path, mode, 0666)
861882
if err != nil {
862883
p.SetError(err)

script_unix_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ func ExampleFindFiles() {
112112
// testdata/multiple_files_with_subdirectory/dir/2.txt
113113
}
114114

115+
func ExampleIfExists_exec() {
116+
script.IfExists("./testdata/hello.txt").Exec("echo hello").Stdout()
117+
// Output:
118+
// hello
119+
}
120+
121+
func ExampleIfExists_noExec() {
122+
script.IfExists("doesntexist").Exec("echo hello").Stdout()
123+
// Output:
124+
//
125+
}
126+
115127
func ExampleListFiles() {
116128
script.ListFiles("testdata/multiple_files_with_subdirectory").Stdout()
117129
// Output:

0 commit comments

Comments
 (0)