-
Notifications
You must be signed in to change notification settings - Fork 0
/
localreader.go
74 lines (61 loc) · 1.41 KB
/
localreader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package veracity
import (
"bufio"
"bytes"
"io"
"os"
"path/filepath"
"github.com/datatrails/go-datatrails-merklelog/massifs"
)
type ReadOpener struct{}
func (*ReadOpener) Open(name string) (io.ReadCloser, error) {
fpath, err := filepath.Abs(name)
if err != nil {
return nil, err
}
return os.Open(fpath)
}
func NewFileOpener() massifs.Opener {
return &ReadOpener{}
}
type StdinOpener struct {
data []byte
}
func NewStdinOpener() massifs.Opener {
return &StdinOpener{}
}
func (o *StdinOpener) Open(string) (io.ReadCloser, error) {
if len(o.data) > 0 {
return io.NopCloser(bytes.NewReader(o.data)), nil
}
r := bufio.NewReader(os.Stdin)
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
o.data = data
return io.NopCloser(bytes.NewReader(o.data)), nil
}
// Utilities to remove the os dependencies from the MassifReader
type OsDirLister struct{}
func NewDirLister() massifs.DirLister {
return &OsDirLister{}
}
func (*OsDirLister) ListFiles(name string) ([]string, error) {
dpath, err := filepath.Abs(name)
if err != nil {
return nil, err
}
result := []string{}
entries, err := os.ReadDir(dpath)
if err != nil {
return result, err
}
for _, entry := range entries {
// if !entry.IsDir() && entry.Type().IsRegular() && strings.HasSuffix(entry.Name(), massifs.V1MMRMassifExt){
if !entry.IsDir() {
result = append(result, filepath.Join(dpath, entry.Name()))
}
}
return result, nil
}