1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2024-11-21 19:20:05 -05:00
ofxgo/cmd/ofx/ccdownload.go
David Ward 1f657a5d18 Add missing handling for -dryrun flag in command-line client
This flag was only handled for the `download-profile` command. Add
the same handling for all other commands (except `detect-settings`).
2023-02-05 19:52:02 -05:00

80 lines
1.6 KiB
Go

package main
import (
"flag"
"fmt"
"github.com/aclindsa/ofxgo"
"io"
"os"
)
var ccDownloadCommand = command{
Name: "download-cc",
Description: "Download a credit card account statement to a file",
Flags: flag.NewFlagSet("download-cc", flag.ExitOnError),
CheckFlags: ccDownloadCheckFlags,
Do: ccDownload,
}
func init() {
defineServerFlags(ccDownloadCommand.Flags)
ccDownloadCommand.Flags.StringVar(&filename, "filename", "./response.ofx", "The file to save to")
ccDownloadCommand.Flags.StringVar(&acctID, "acctid", "", "AcctID (from `get-accounts` subcommand)")
}
func ccDownloadCheckFlags() bool {
ret := checkServerFlags()
if len(filename) == 0 {
fmt.Println("Error: Filename empty")
return false
}
return ret
}
func ccDownload() {
client, query := newRequest()
uid, err := ofxgo.RandomUID()
if err != nil {
fmt.Println("Error creating uid for transaction:", err)
os.Exit(1)
}
statementRequest := ofxgo.CCStatementRequest{
TrnUID: *uid,
CCAcctFrom: ofxgo.CCAcct{
AcctID: ofxgo.String(acctID),
},
Include: true,
}
query.CreditCard = append(query.CreditCard, &statementRequest)
if dryrun {
printRequest(client, query)
return
}
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)
}
}