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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -27,3 +28,30 @@ func GetStdin() ([]string, error) {
|
||||||
s, _ := strings.CutSuffix(string(data), Newline)
|
s, _ := strings.CutSuffix(string(data), Newline)
|
||||||
return strings.Split(s, Newline), nil
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
29
sort/sort.go
29
sort/sort.go
|
|
@ -33,25 +33,6 @@ func initOpts() options {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// reads files from stdin if present, otherwise from the filename:
|
|
||||||
func fileListSplitMaybeStdin(f string) []string {
|
|
||||||
filelist, err := pipe.GetStdin()
|
|
||||||
if err != nil {
|
|
||||||
filelist = fileListSplit(f)
|
|
||||||
}
|
|
||||||
return filelist
|
|
||||||
}
|
|
||||||
|
|
||||||
// opens the given filename and splits into lines:
|
|
||||||
func fileListSplit(f string) []string {
|
|
||||||
dat, err := os.ReadFile(f)
|
|
||||||
if err != nil {
|
|
||||||
// if this fails all is lost. just exit.
|
|
||||||
log.Fatalf("um %s: error opening file: %s\n%s", CMD, f, err)
|
|
||||||
}
|
|
||||||
return strings.Split(string(dat), pipe.Newline)
|
|
||||||
}
|
|
||||||
|
|
||||||
// record the order of the given filelist.
|
// record the order of the given filelist.
|
||||||
func kMap(kslice []string) map[string]int {
|
func kMap(kslice []string) map[string]int {
|
||||||
m := make(map[string]int, 0)
|
m := make(map[string]int, 0)
|
||||||
|
|
@ -95,8 +76,14 @@ func Sort(args []string) {
|
||||||
flags.Help(CMD, SUMMARY, opts)
|
flags.Help(CMD, SUMMARY, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
sslice := fileListSplitMaybeStdin(opts.Source.Val)
|
sslice, err := pipe.FileListSplitMaybeStdin(opts.Source.Val)
|
||||||
kslice := fileListSplit(opts.Key.Val)
|
if err != nil {
|
||||||
|
log.Fatalf("um %s: %s", CMD, err)
|
||||||
|
}
|
||||||
|
kslice, err := pipe.FileListSplit(opts.Key.Val)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("um %s: %s", CMD, err)
|
||||||
|
}
|
||||||
kmap := kMap(kslice)
|
kmap := kMap(kslice)
|
||||||
out := sort(sslice, kmap)
|
out := sort(sslice, kmap)
|
||||||
if opts.Write.IsSet() {
|
if opts.Write.IsSet() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue