@@ -2,15 +2,16 @@ package fix
22
33import (
44 "bytes"
5- "encoding/json"
65 "fmt"
76 "os"
87
98 "github.com/moov-io/ach"
9+ "github.com/moov-io/ach/cmd/achcli/internal/read"
10+ "github.com/moov-io/ach/cmd/achcli/internal/write"
1011)
1112
12- func Perform (path string , validateOpts * ach. ValidateOpts , conf Config ) (string , error ) {
13- file , err := readFile (path , validateOpts )
13+ func Perform (path string , validateOptsPath * string , skipAll * bool , conf Config ) (string , error ) {
14+ file , format , err := read . Filepath (path , validateOptsPath , skipAll )
1415 if err != nil {
1516 return "" , fmt .Errorf ("reading %s failed: %w" , path , err )
1617 }
@@ -38,9 +39,9 @@ func Perform(path string, validateOpts *ach.ValidateOpts, conf Config) (string,
3839 newpath := path + ".fix"
3940
4041 var buf bytes.Buffer
41- err = ach . NewWriter (& buf ). Write ( file )
42+ err = write . File (& buf , file , format )
4243 if err != nil {
43- return "" , fmt .Errorf ("encoding fixed file: %w" , err )
44+ return "" , fmt .Errorf ("encoding fixed file as %s : %w" , format , err )
4445 }
4546
4647 err = os .WriteFile (newpath , buf .Bytes (), 0644 )
@@ -51,33 +52,4 @@ func Perform(path string, validateOpts *ach.ValidateOpts, conf Config) (string,
5152 return newpath , nil
5253}
5354
54- func readFile (path string , validateOpts * ach.ValidateOpts ) (* ach.File , error ) {
55- bs , err := os .ReadFile (path )
56- if err != nil {
57- return nil , err
58- }
59- if json .Valid (bs ) {
60- return readJsonFile (bs , validateOpts )
61- }
62- return readACHFile (bs , validateOpts )
63- }
64-
65- func readACHFile (input []byte , validateOpts * ach.ValidateOpts ) (* ach.File , error ) {
66- r := ach .NewReader (bytes .NewReader (input ))
67- r .SetValidation (validateOpts )
68- f , err := r .Read ()
69- return & f , err
70- }
71-
72- func readJsonFile (input []byte , validateOpts * ach.ValidateOpts ) (* ach.File , error ) {
73- return ach .FileFromJSONWith (input , validateOpts )
74- }
75-
7655type batchHeaderFixer func (bh * ach.BatchHeader ) error
77-
78- // TODO(adam): process file batch by batch
79- // TODO(adam): fix funcs take a file header, batch, entry, etc
80-
81- // TODO(adam): updateEED()
82-
83- // TODO(adam): write output to disk
0 commit comments