cmd/writeACH: add -json to output JSON instead of plaintext ACH file
package main
import (
+ "encoding/json"
"flag"
"fmt"
"github.com/moov-io/ach"
@@ -14,6 +15,9 @@ import (
var (
fPath = flag.String("fPath", "", "File Path")
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
+
+ // output formats
+ flagJson = flag.Bool("json", false, "Output file in json")
)
// main creates an ACH File with 4 batches of SEC Code PPD.
@@ -21,7 +25,14 @@ var (
func main() {
flag.Parse()
- path := filepath.Join(*fPath, time.Now().UTC().Format("200601021504")+".ach")
+ filename := time.Now().UTC().Format("200601021504")
+ if *flagJson {
+ filename += ".json"
+ } else {
+ filename += ".ach"
+ }
+
+ path := filepath.Join(*fPath, filename)
write(path)
}
@@ -105,12 +116,23 @@ func write(path string) {
}
// Write to a file
+ if *flagJson {
+ // Write in JSON format
+ if err := json.NewEncoder(f).Encode(file); err != nil {
+ fmt.Printf("%T: %s", err, err)
+ }
+ } else {
+ // Write in ACH plain text format
w := ach.NewWriter(f)
if err := w.Write(file); err != nil {
fmt.Printf("%T: %s", err, err)
}
w.Flush()
+ }
+
if err := f.Close(); err != nil {
fmt.Println(err.Error())
}
+
+ fmt.Printf("Wrote %s\n", path)
}
