cmd/readACH: add -json to output file back in JSON
package main
import (
+ "encoding/json"
+ "flag"
"fmt"
"log"
"os"
+ "runtime/pprof"
- "flag"
"github.com/moov-io/ach"
- "runtime/pprof"
)
-func main() {
+var (
+ fPath = flag.String("fPath", "201805101354.ach", "File Path")
+ cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
- var fPath = flag.String("fPath", "201805101354.ach", "File Path")
- var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
+ flagJson = flag.Bool("json", false, "Output ACH File in JSON to stdout")
+)
+func main() {
flag.Parse()
+
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
@@ -33,20 +38,31 @@ func main() {
if err != nil {
log.Panicf("Can not open file: %s: \n", err)
}
+
r := ach.NewReader(f)
achFile, err := r.Read()
if err != nil {
fmt.Printf("Issue reading file: %+v \n", err)
}
+
// ensure we have a validated file structure
if achFile.Validate(); err != nil {
fmt.Printf("Could not validate entire read file: %v", err)
}
+
// If you trust the file but it's formating is off building will probably resolve the malformed file.
if achFile.Create(); err != nil {
fmt.Printf("Could not build file with read properties: %v", err)
}
+ // Output file contents
+ if *flagJson {
+ if err := json.NewEncoder(os.Stdout).Encode(achFile); err != nil {
+ fmt.Printf("ERROR: problem writing ACH File to stdout: %v\n", err)
+ os.Exit(1)
+ }
+ } else {
fmt.Printf("total amount debit: %v \n", achFile.Control.TotalDebitEntryDollarAmountInFile)
fmt.Printf("total amount credit: %v \n", achFile.Control.TotalCreditEntryDollarAmountInFile)
}
+}
