@@ -6,44 +6,49 @@ package main
6
6
7
7
import (
8
8
"fmt"
9
+ "io"
9
10
"os"
10
11
11
12
"github.com/moov-io/base/log"
12
13
"github.com/moov-io/fed"
13
14
"github.com/moov-io/fed/pkg/download"
14
15
)
15
16
16
- func fedACHDataFile (logger log.Logger ) * os. File {
17
+ func fedACHDataFile (logger log.Logger ) io. Reader {
17
18
if file , err := attemptFileDownload (logger , "fedach" ); file != nil {
18
19
return file
19
20
} else if err != nil {
20
21
panic (fmt .Sprintf ("problem downloading fedach: %v" , err ))
21
22
}
22
23
23
24
path := readDataFilepath ("FEDACH_DATA_PATH" , "./data/FedACHdir.txt" )
25
+ logger .Logf ("search: loading %s for ACH data" , path )
26
+
24
27
file , err := os .Open (path )
25
28
if err != nil {
26
29
panic (fmt .Sprintf ("problem opening %s: %v" , path , err ))
27
30
}
28
31
return file
29
32
}
30
33
31
- func fedWireDataFile (logger log.Logger ) * os. File {
34
+ func fedWireDataFile (logger log.Logger ) io. Reader {
32
35
if file , err := attemptFileDownload (logger , "fedwire" ); file != nil {
33
36
return file
34
37
} else if err != nil {
35
38
panic (fmt .Sprintf ("problem downloading fedwire: %v" , err ))
36
39
}
37
40
38
41
path := readDataFilepath ("FEDWIRE_DATA_PATH" , "./data/fpddir.txt" )
42
+ logger .Logf ("search: loading %s for Wire data" , path )
43
+
39
44
file , err := os .Open (path )
40
45
if err != nil {
41
46
panic (fmt .Sprintf ("problem opening %s: %v" , path , err ))
42
47
}
43
48
return file
44
49
}
45
50
46
- func attemptFileDownload (logger log.Logger , listName string ) (* os. File , error ) {
51
+ func attemptFileDownload (logger log.Logger , listName string ) (io. Reader , error ) {
47
52
routingNumber := os .Getenv ("FRB_ROUTING_NUMBER" )
48
53
downloadCode := os .Getenv ("FRB_DOWNLOAD_CODE" )
49
54
@@ -71,14 +76,17 @@ func readDataFilepath(env, fallback string) string {
71
76
72
77
// readFEDACHData opens and reads FedACHdir.txt then runs ACHDictionary.Read() to
73
78
// parse and define ACHDictionary properties
74
- func (s * searcher ) readFEDACHData (file * os. File ) error {
79
+ func (s * searcher ) readFEDACHData (reader io. Reader ) error {
75
80
if s .logger != nil {
76
81
s .logger .Logf ("Read of FED data" )
77
82
}
78
- defer file .Close ()
83
+
84
+ if closer , ok := reader .(io.Closer ); ok {
85
+ defer closer .Close ()
86
+ }
79
87
80
88
s .ACHDictionary = fed .NewACHDictionary ()
81
- if err := s .ACHDictionary .Read (file ); err != nil {
89
+ if err := s .ACHDictionary .Read (reader ); err != nil {
82
90
return fmt .Errorf ("ERROR: reading FedACHdir.txt %v" , err )
83
91
}
84
92
@@ -91,14 +99,17 @@ func (s *searcher) readFEDACHData(file *os.File) error {
91
99
92
100
// readFEDWIREData opens and reads fpddir.txt then runs WIREDictionary.Read() to
93
101
// parse and define WIREDictionary properties
94
- func (s * searcher ) readFEDWIREData (file * os. File ) error {
102
+ func (s * searcher ) readFEDWIREData (reader io. Reader ) error {
95
103
if s .logger != nil {
96
104
s .logger .Logf ("Read of FED data" )
97
105
}
98
- defer file .Close ()
106
+
107
+ if closer , ok := reader .(io.Closer ); ok {
108
+ defer closer .Close ()
109
+ }
99
110
100
111
s .WIREDictionary = fed .NewWIREDictionary ()
101
- if err := s .WIREDictionary .Read (file ); err != nil {
112
+ if err := s .WIREDictionary .Read (reader ); err != nil {
102
113
return fmt .Errorf ("ERROR: reading fpddir.txt %v" , err )
103
114
}
104
115
0 commit comments