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 invDownloadCommand = command{
|
2017-03-22 20:29:08 -04:00
|
|
|
Name: "download-inv",
|
|
|
|
Description: "Download a investment account statement to a file",
|
|
|
|
Flags: flag.NewFlagSet("download-inv", flag.ExitOnError),
|
|
|
|
CheckFlags: invDownloadCheckFlags,
|
|
|
|
Do: invDownload,
|
|
|
|
}
|
|
|
|
|
2017-04-12 21:40:42 -04:00
|
|
|
var brokerID string
|
2017-03-22 20:29:08 -04:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
defineServerFlags(invDownloadCommand.Flags)
|
2020-12-07 13:36:38 -05:00
|
|
|
invDownloadCommand.Flags.StringVar(&filename, "filename", "./response.ofx", "The file to save to")
|
2017-04-12 21:40:42 -04:00
|
|
|
invDownloadCommand.Flags.StringVar(&acctID, "acctid", "", "AcctID (from `get-accounts` subcommand)")
|
|
|
|
invDownloadCommand.Flags.StringVar(&brokerID, "brokerid", "", "BrokerID (from `get-accounts` subcommand)")
|
2017-03-22 20:29:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func invDownloadCheckFlags() bool {
|
|
|
|
ret := checkServerFlags()
|
|
|
|
|
|
|
|
if len(filename) == 0 {
|
|
|
|
fmt.Println("Error: Filename empty")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func invDownload() {
|
2017-04-16 20:50:06 -04:00
|
|
|
client, query := newRequest()
|
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.InvStatementRequest{
|
|
|
|
TrnUID: *uid,
|
|
|
|
InvAcctFrom: ofxgo.InvAcct{
|
2017-04-12 21:40:42 -04:00
|
|
|
BrokerID: ofxgo.String(brokerID),
|
|
|
|
AcctID: ofxgo.String(acctID),
|
2017-03-22 20:29:08 -04:00
|
|
|
},
|
|
|
|
Include: true,
|
|
|
|
IncludeOO: true,
|
|
|
|
IncludePos: true,
|
|
|
|
IncludeBalance: true,
|
|
|
|
Include401K: true,
|
|
|
|
Include401KBal: true,
|
|
|
|
}
|
2017-03-31 11:54:43 -04:00
|
|
|
query.InvStmt = append(query.InvStmt, &statementRequest)
|
2017-03-22 20:29:08 -04:00
|
|
|
|
2023-02-05 19:17:05 -05:00
|
|
|
if dryrun {
|
|
|
|
printRequest(client, query)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|