1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2024-10-30 21:40:04 -04:00
ofxgo/cmd/ofx/bankdownload.go

87 lines
2.0 KiB
Go
Raw Normal View History

2017-03-22 20:29:08 -04:00
package main
import (
"flag"
"fmt"
"github.com/aclindsa/ofxgo"
"io"
"os"
)
2017-04-16 20:50:06 -04:00
var downloadCommand = command{
2017-03-22 20:29:08 -04:00
Name: "download-bank",
Description: "Download a bank account statement to a file",
Flags: flag.NewFlagSet("download-bank", flag.ExitOnError),
CheckFlags: downloadCheckFlags,
Do: download,
}
2017-04-12 21:40:42 -04:00
var filename, bankID, acctID, acctType string
2017-03-22 20:29:08 -04:00
func init() {
defineServerFlags(downloadCommand.Flags)
downloadCommand.Flags.StringVar(&filename, "filename", "./download.ofx", "The file to save to")
2017-04-12 21:40:42 -04:00
downloadCommand.Flags.StringVar(&bankID, "bankid", "", "BankID (from `get-accounts` subcommand)")
downloadCommand.Flags.StringVar(&acctID, "acctid", "", "AcctID (from `get-accounts` subcommand)")
2017-03-22 20:29:08 -04:00
downloadCommand.Flags.StringVar(&acctType, "accttype", "CHECKING", "AcctType (from `get-accounts` subcommand)")
}
func downloadCheckFlags() bool {
ret := checkServerFlags()
if len(filename) == 0 {
fmt.Println("Error: Filename empty")
return false
}
return ret
}
func download() {
2017-04-16 20:50:06 -04:00
client, query := newRequest()
2017-03-22 20:29:08 -04:00
acctTypeEnum, err := ofxgo.NewAcctType(acctType)
if err != nil {
fmt.Println("Error parsing accttype:", err)
os.Exit(1)
}
2017-03-22 20:29:08 -04:00
uid, err := ofxgo.RandomUID()
if err != nil {
fmt.Println("Error creating uid for transaction:", err)
os.Exit(1)
}
statementRequest := ofxgo.StatementRequest{
TrnUID: *uid,
BankAcctFrom: ofxgo.BankAcct{
2017-04-12 21:40:42 -04:00
BankID: ofxgo.String(bankID),
AcctID: ofxgo.String(acctID),
AcctType: acctTypeEnum,
2017-03-22 20:29:08 -04:00
},
Include: true,
}
query.Bank = append(query.Bank, &statementRequest)
2017-03-22 20:29:08 -04:00
response, err := client.RequestNoParse(query)
if err != nil {
fmt.Println("Error requesting account statement:", err)
os.Exit(1)
}
defer response.Body.Close()
file, err := os.Create(filename)
if err != nil {
fmt.Println("Error creating file to write to:", err)
os.Exit(1)
}
defer file.Close()
_, err = io.Copy(file, response.Body)
if err != nil {
fmt.Println("Error writing response to file:", err)
os.Exit(1)
}
}