pipe : FileListSplit and better errors
This commit is contained in:
parent
6f9f5dbb23
commit
7e6f8dd36c
2 changed files with 36 additions and 21 deletions
28
pipe/pipe.go
28
pipe/pipe.go
|
|
@ -2,6 +2,7 @@ package pipe
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
|
@ -27,3 +28,30 @@ func GetStdin() ([]string, error) {
|
|||
s, _ := strings.CutSuffix(string(data), Newline)
|
||||
return strings.Split(s, Newline), nil
|
||||
}
|
||||
|
||||
// reads files from stdin if present, otherwise from the filename:
|
||||
func FileListSplitMaybeStdin(f string) ([]string, error) {
|
||||
filelist, err := GetStdin()
|
||||
if err != nil {
|
||||
// BORK: don't see a neater way of wrapping these two when we depend on the non-nil in this block:
|
||||
// if i just return FileListSplit(), we lose the first err
|
||||
filelist, err2 := FileListSplit(f)
|
||||
if err2 != nil {
|
||||
return nil, fmt.Errorf("%w: %w", err, err2)
|
||||
}
|
||||
return filelist, nil
|
||||
}
|
||||
return filelist, nil
|
||||
}
|
||||
|
||||
// opens the given filename and splits into lines:
|
||||
func FileListSplit(f string) ([]string, error) {
|
||||
if f == "" {
|
||||
return nil, errors.New("filename is empty")
|
||||
}
|
||||
dat, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error opening file: %w", err)
|
||||
}
|
||||
return strings.Split(string(dat), Newline), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue