From aff4b9f32dde3ee75aca260f822bfd17d91aa4a0 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Mon, 26 Feb 2024 00:12:31 -0500 Subject: [PATCH] Improved file-not-found handling (#1508) --- pkg/input/record_reader_csv.go | 3 +- pkg/input/record_reader_csvlite.go | 38 +++++++++---------- pkg/input/record_reader_dkvp_nidx.go | 3 +- pkg/input/record_reader_json.go | 3 +- pkg/input/record_reader_pprint.go | 38 +++++++++---------- pkg/input/record_reader_tsv.go | 38 +++++++++---------- pkg/input/record_reader_xtab.go | 3 +- test/cases/non-windows/file-not-found/csv/cmd | 1 + .../non-windows/file-not-found/csv/experr | 1 + .../non-windows/file-not-found/csv/expout | 0 .../file-not-found/csv/should-fail | 0 .../cases/non-windows/file-not-found/dkvp/cmd | 1 + .../non-windows/file-not-found/dkvp/experr | 1 + .../non-windows/file-not-found/dkvp/expout | 0 .../file-not-found/dkvp/should-fail | 0 test/cases/non-windows/file-not-found/imd/cmd | 1 + .../non-windows/file-not-found/imd/experr | 1 + .../non-windows/file-not-found/imd/expout | 0 .../file-not-found/imd/should-fail | 0 .../cases/non-windows/file-not-found/json/cmd | 1 + .../non-windows/file-not-found/json/experr | 1 + .../non-windows/file-not-found/json/expout | 2 + .../file-not-found/json/should-fail | 0 .../non-windows/file-not-found/jsonl/cmd | 1 + .../non-windows/file-not-found/jsonl/experr | 1 + .../non-windows/file-not-found/jsonl/expout | 0 .../file-not-found/jsonl/should-fail | 0 .../cases/non-windows/file-not-found/nidx/cmd | 1 + .../non-windows/file-not-found/nidx/experr | 1 + .../non-windows/file-not-found/nidx/expout | 0 .../file-not-found/nidx/should-fail | 0 .../non-windows/file-not-found/pprint/cmd | 1 + .../non-windows/file-not-found/pprint/experr | 1 + .../non-windows/file-not-found/pprint/expout | 0 .../file-not-found/pprint/should-fail | 0 test/cases/non-windows/file-not-found/tsv/cmd | 1 + .../non-windows/file-not-found/tsv/experr | 1 + .../non-windows/file-not-found/tsv/expout | 0 .../file-not-found/tsv/should-fail | 0 .../cases/non-windows/file-not-found/xtab/cmd | 1 + .../non-windows/file-not-found/xtab/experr | 1 + .../non-windows/file-not-found/xtab/expout | 0 .../file-not-found/xtab/should-fail | 0 43 files changed, 85 insertions(+), 61 deletions(-) create mode 100644 test/cases/non-windows/file-not-found/csv/cmd create mode 100644 test/cases/non-windows/file-not-found/csv/experr create mode 100644 test/cases/non-windows/file-not-found/csv/expout create mode 100644 test/cases/non-windows/file-not-found/csv/should-fail create mode 100644 test/cases/non-windows/file-not-found/dkvp/cmd create mode 100644 test/cases/non-windows/file-not-found/dkvp/experr create mode 100644 test/cases/non-windows/file-not-found/dkvp/expout create mode 100644 test/cases/non-windows/file-not-found/dkvp/should-fail create mode 100644 test/cases/non-windows/file-not-found/imd/cmd create mode 100644 test/cases/non-windows/file-not-found/imd/experr create mode 100644 test/cases/non-windows/file-not-found/imd/expout create mode 100644 test/cases/non-windows/file-not-found/imd/should-fail create mode 100644 test/cases/non-windows/file-not-found/json/cmd create mode 100644 test/cases/non-windows/file-not-found/json/experr create mode 100644 test/cases/non-windows/file-not-found/json/expout create mode 100644 test/cases/non-windows/file-not-found/json/should-fail create mode 100644 test/cases/non-windows/file-not-found/jsonl/cmd create mode 100644 test/cases/non-windows/file-not-found/jsonl/experr create mode 100644 test/cases/non-windows/file-not-found/jsonl/expout create mode 100644 test/cases/non-windows/file-not-found/jsonl/should-fail create mode 100644 test/cases/non-windows/file-not-found/nidx/cmd create mode 100644 test/cases/non-windows/file-not-found/nidx/experr create mode 100644 test/cases/non-windows/file-not-found/nidx/expout create mode 100644 test/cases/non-windows/file-not-found/nidx/should-fail create mode 100644 test/cases/non-windows/file-not-found/pprint/cmd create mode 100644 test/cases/non-windows/file-not-found/pprint/experr create mode 100644 test/cases/non-windows/file-not-found/pprint/expout create mode 100644 test/cases/non-windows/file-not-found/pprint/should-fail create mode 100644 test/cases/non-windows/file-not-found/tsv/cmd create mode 100644 test/cases/non-windows/file-not-found/tsv/experr create mode 100644 test/cases/non-windows/file-not-found/tsv/expout create mode 100644 test/cases/non-windows/file-not-found/tsv/should-fail create mode 100644 test/cases/non-windows/file-not-found/xtab/cmd create mode 100644 test/cases/non-windows/file-not-found/xtab/experr create mode 100644 test/cases/non-windows/file-not-found/xtab/expout create mode 100644 test/cases/non-windows/file-not-found/xtab/should-fail diff --git a/pkg/input/record_reader_csv.go b/pkg/input/record_reader_csv.go index ab7c1d761d..505020ceee 100644 --- a/pkg/input/record_reader_csv.go +++ b/pkg/input/record_reader_csv.go @@ -65,8 +65,9 @@ func (reader *RecordReaderCSV) Read( ) if err != nil { errorChannel <- err + } else { + reader.processHandle(handle, "(stdin)", &context, readerChannel, errorChannel, downstreamDoneChannel) } - reader.processHandle(handle, "(stdin)", &context, readerChannel, errorChannel, downstreamDoneChannel) } else { for _, filename := range filenames { handle, err := lib.OpenFileForRead( diff --git a/pkg/input/record_reader_csvlite.go b/pkg/input/record_reader_csvlite.go index bfc1887196..5109770df9 100644 --- a/pkg/input/record_reader_csvlite.go +++ b/pkg/input/record_reader_csvlite.go @@ -94,16 +94,16 @@ func (reader *RecordReaderCSVLite) Read( ) if err != nil { errorChannel <- err - return + } else { + reader.processHandle( + handle, + "(stdin)", + &context, + readerChannel, + errorChannel, + downstreamDoneChannel, + ) } - reader.processHandle( - handle, - "(stdin)", - &context, - readerChannel, - errorChannel, - downstreamDoneChannel, - ) } else { for _, filename := range filenames { handle, err := lib.OpenFileForRead( @@ -114,17 +114,17 @@ func (reader *RecordReaderCSVLite) Read( ) if err != nil { errorChannel <- err - return + } else { + reader.processHandle( + handle, + filename, + &context, + readerChannel, + errorChannel, + downstreamDoneChannel, + ) + handle.Close() } - reader.processHandle( - handle, - filename, - &context, - readerChannel, - errorChannel, - downstreamDoneChannel, - ) - handle.Close() } } } diff --git a/pkg/input/record_reader_dkvp_nidx.go b/pkg/input/record_reader_dkvp_nidx.go index 943fbcb5e7..a5509a23d4 100644 --- a/pkg/input/record_reader_dkvp_nidx.go +++ b/pkg/input/record_reader_dkvp_nidx.go @@ -68,8 +68,9 @@ func (reader *RecordReaderDKVPNIDX) Read( ) if err != nil { errorChannel <- err + } else { + reader.processHandle(handle, "(stdin)", &context, readerChannel, errorChannel, downstreamDoneChannel) } - reader.processHandle(handle, "(stdin)", &context, readerChannel, errorChannel, downstreamDoneChannel) } else { for _, filename := range filenames { handle, err := lib.OpenFileForRead( diff --git a/pkg/input/record_reader_json.go b/pkg/input/record_reader_json.go index ecc44e0613..80ce174409 100644 --- a/pkg/input/record_reader_json.go +++ b/pkg/input/record_reader_json.go @@ -45,8 +45,9 @@ func (reader *RecordReaderJSON) Read( ) if err != nil { errorChannel <- err + } else { + reader.processHandle(handle, "(stdin)", &context, readerChannel, errorChannel, downstreamDoneChannel) } - reader.processHandle(handle, "(stdin)", &context, readerChannel, errorChannel, downstreamDoneChannel) } else { for _, filename := range filenames { handle, err := lib.OpenFileForRead( diff --git a/pkg/input/record_reader_pprint.go b/pkg/input/record_reader_pprint.go index 3fa9cd6dab..5cb4bfbad2 100644 --- a/pkg/input/record_reader_pprint.go +++ b/pkg/input/record_reader_pprint.go @@ -98,16 +98,16 @@ func (reader *RecordReaderPprintBarredOrMarkdown) Read( ) if err != nil { errorChannel <- err - return + } else { + reader.processHandle( + handle, + "(stdin)", + &context, + readerChannel, + errorChannel, + downstreamDoneChannel, + ) } - reader.processHandle( - handle, - "(stdin)", - &context, - readerChannel, - errorChannel, - downstreamDoneChannel, - ) } else { for _, filename := range filenames { handle, err := lib.OpenFileForRead( @@ -118,17 +118,17 @@ func (reader *RecordReaderPprintBarredOrMarkdown) Read( ) if err != nil { errorChannel <- err - return + } else { + reader.processHandle( + handle, + filename, + &context, + readerChannel, + errorChannel, + downstreamDoneChannel, + ) + handle.Close() } - reader.processHandle( - handle, - filename, - &context, - readerChannel, - errorChannel, - downstreamDoneChannel, - ) - handle.Close() } } } diff --git a/pkg/input/record_reader_tsv.go b/pkg/input/record_reader_tsv.go index 635dc08407..02a3c4f6ee 100644 --- a/pkg/input/record_reader_tsv.go +++ b/pkg/input/record_reader_tsv.go @@ -76,16 +76,16 @@ func (reader *RecordReaderTSV) Read( ) if err != nil { errorChannel <- err - return + } else { + reader.processHandle( + handle, + "(stdin)", + &context, + readerChannel, + errorChannel, + downstreamDoneChannel, + ) } - reader.processHandle( - handle, - "(stdin)", - &context, - readerChannel, - errorChannel, - downstreamDoneChannel, - ) } else { for _, filename := range filenames { handle, err := lib.OpenFileForRead( @@ -96,17 +96,17 @@ func (reader *RecordReaderTSV) Read( ) if err != nil { errorChannel <- err - return + } else { + reader.processHandle( + handle, + filename, + &context, + readerChannel, + errorChannel, + downstreamDoneChannel, + ) + handle.Close() } - reader.processHandle( - handle, - filename, - &context, - readerChannel, - errorChannel, - downstreamDoneChannel, - ) - handle.Close() } } } diff --git a/pkg/input/record_reader_xtab.go b/pkg/input/record_reader_xtab.go index 8dd88c308e..31294012c2 100644 --- a/pkg/input/record_reader_xtab.go +++ b/pkg/input/record_reader_xtab.go @@ -71,8 +71,9 @@ func (reader *RecordReaderXTAB) Read( ) if err != nil { errorChannel <- err + } else { + reader.processHandle(handle, "(stdin)", &context, readerChannel, errorChannel, downstreamDoneChannel) } - reader.processHandle(handle, "(stdin)", &context, readerChannel, errorChannel, downstreamDoneChannel) } else { for _, filename := range filenames { handle, err := lib.OpenFileForRead( diff --git a/test/cases/non-windows/file-not-found/csv/cmd b/test/cases/non-windows/file-not-found/csv/cmd new file mode 100644 index 0000000000..c0111df1c2 --- /dev/null +++ b/test/cases/non-windows/file-not-found/csv/cmd @@ -0,0 +1 @@ +mlr --csv cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/csv/experr b/test/cases/non-windows/file-not-found/csv/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/csv/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/csv/expout b/test/cases/non-windows/file-not-found/csv/expout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/csv/should-fail b/test/cases/non-windows/file-not-found/csv/should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/dkvp/cmd b/test/cases/non-windows/file-not-found/dkvp/cmd new file mode 100644 index 0000000000..e0a95d06c6 --- /dev/null +++ b/test/cases/non-windows/file-not-found/dkvp/cmd @@ -0,0 +1 @@ +mlr --dkvp cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/dkvp/experr b/test/cases/non-windows/file-not-found/dkvp/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/dkvp/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/dkvp/expout b/test/cases/non-windows/file-not-found/dkvp/expout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/dkvp/should-fail b/test/cases/non-windows/file-not-found/dkvp/should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/imd/cmd b/test/cases/non-windows/file-not-found/imd/cmd new file mode 100644 index 0000000000..53adb3fe5e --- /dev/null +++ b/test/cases/non-windows/file-not-found/imd/cmd @@ -0,0 +1 @@ +mlr --imd cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/imd/experr b/test/cases/non-windows/file-not-found/imd/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/imd/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/imd/expout b/test/cases/non-windows/file-not-found/imd/expout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/imd/should-fail b/test/cases/non-windows/file-not-found/imd/should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/json/cmd b/test/cases/non-windows/file-not-found/json/cmd new file mode 100644 index 0000000000..92c89b17f7 --- /dev/null +++ b/test/cases/non-windows/file-not-found/json/cmd @@ -0,0 +1 @@ +mlr --json cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/json/experr b/test/cases/non-windows/file-not-found/json/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/json/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/json/expout b/test/cases/non-windows/file-not-found/json/expout new file mode 100644 index 0000000000..0d4f101c7a --- /dev/null +++ b/test/cases/non-windows/file-not-found/json/expout @@ -0,0 +1,2 @@ +[ +] diff --git a/test/cases/non-windows/file-not-found/json/should-fail b/test/cases/non-windows/file-not-found/json/should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/jsonl/cmd b/test/cases/non-windows/file-not-found/jsonl/cmd new file mode 100644 index 0000000000..551c0b0a74 --- /dev/null +++ b/test/cases/non-windows/file-not-found/jsonl/cmd @@ -0,0 +1 @@ +mlr --jsonl cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/jsonl/experr b/test/cases/non-windows/file-not-found/jsonl/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/jsonl/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/jsonl/expout b/test/cases/non-windows/file-not-found/jsonl/expout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/jsonl/should-fail b/test/cases/non-windows/file-not-found/jsonl/should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/nidx/cmd b/test/cases/non-windows/file-not-found/nidx/cmd new file mode 100644 index 0000000000..b5b1a23166 --- /dev/null +++ b/test/cases/non-windows/file-not-found/nidx/cmd @@ -0,0 +1 @@ +mlr --nidx cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/nidx/experr b/test/cases/non-windows/file-not-found/nidx/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/nidx/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/nidx/expout b/test/cases/non-windows/file-not-found/nidx/expout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/nidx/should-fail b/test/cases/non-windows/file-not-found/nidx/should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/pprint/cmd b/test/cases/non-windows/file-not-found/pprint/cmd new file mode 100644 index 0000000000..51a3a4d6ec --- /dev/null +++ b/test/cases/non-windows/file-not-found/pprint/cmd @@ -0,0 +1 @@ +mlr --pprint cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/pprint/experr b/test/cases/non-windows/file-not-found/pprint/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/pprint/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/pprint/expout b/test/cases/non-windows/file-not-found/pprint/expout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/pprint/should-fail b/test/cases/non-windows/file-not-found/pprint/should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/tsv/cmd b/test/cases/non-windows/file-not-found/tsv/cmd new file mode 100644 index 0000000000..fbb231b7e8 --- /dev/null +++ b/test/cases/non-windows/file-not-found/tsv/cmd @@ -0,0 +1 @@ +mlr --tsv cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/tsv/experr b/test/cases/non-windows/file-not-found/tsv/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/tsv/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/tsv/expout b/test/cases/non-windows/file-not-found/tsv/expout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/tsv/should-fail b/test/cases/non-windows/file-not-found/tsv/should-fail new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/xtab/cmd b/test/cases/non-windows/file-not-found/xtab/cmd new file mode 100644 index 0000000000..dca5d1681c --- /dev/null +++ b/test/cases/non-windows/file-not-found/xtab/cmd @@ -0,0 +1 @@ +mlr --xtab cat /nonesuch/nope/never diff --git a/test/cases/non-windows/file-not-found/xtab/experr b/test/cases/non-windows/file-not-found/xtab/experr new file mode 100644 index 0000000000..486e326b32 --- /dev/null +++ b/test/cases/non-windows/file-not-found/xtab/experr @@ -0,0 +1 @@ +mlr: open /nonesuch/nope/never: no such file or directory. diff --git a/test/cases/non-windows/file-not-found/xtab/expout b/test/cases/non-windows/file-not-found/xtab/expout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/cases/non-windows/file-not-found/xtab/should-fail b/test/cases/non-windows/file-not-found/xtab/should-fail new file mode 100644 index 0000000000..e69de29bb2