pipe : FileListSplit and better errors

This commit is contained in:
bth 2026-01-21 12:31:47 -08:00
commit 7e6f8dd36c
2 changed files with 36 additions and 21 deletions

View file

@ -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
}