1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2024-11-14 17:20:04 -05:00
ofxgo/cmd/ofx/profiledownload.go
Aaron Lindsay 4c7c48cab7 cmd/ofx: Use 'response.ofx' as the default download file everywhere
Conflicting defaults led to confusing default behavior between
sub-commands.
2020-12-07 13:49:35 -05:00

77 lines
1.6 KiB
Go

package main
import (
"flag"
"fmt"
"github.com/aclindsa/ofxgo"
"io"
"os"
)
var profileDownloadCommand = command{
Name: "download-profile",
Description: "Download a FI profile to a file",
Flags: flag.NewFlagSet("download-profile", flag.ExitOnError),
CheckFlags: downloadProfileCheckFlags,
Do: downloadProfile,
}
func init() {
defineServerFlags(profileDownloadCommand.Flags)
profileDownloadCommand.Flags.StringVar(&filename, "filename", "./response.ofx", "The file to save to")
}
func downloadProfileCheckFlags() bool {
// Assume if the user didn't specify username that we should use anonymous
// values for it and password
if len(username) == 0 {
username = "anonymous00000000000000000000000"
password = "anonymous00000000000000000000000"
}
ret := checkServerFlags()
if len(filename) == 0 {
fmt.Println("Error: Filename empty")
return false
}
return ret
}
func downloadProfile() {
client, query := newRequest()
uid, err := ofxgo.RandomUID()
if err != nil {
fmt.Println("Error creating uid for transaction:", err)
os.Exit(1)
}
profileRequest := ofxgo.ProfileRequest{
TrnUID: *uid,
}
query.Prof = append(query.Prof, &profileRequest)
response, err := client.RequestNoParse(query)
if err != nil {
fmt.Println("Error requesting FI profile:", 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)
}
}