From 8c1e6eafab3e1744e80866384c81018f202745af Mon Sep 17 00:00:00 2001 From: Aaron Lindsay Date: Wed, 25 Nov 2020 14:31:37 -0500 Subject: [PATCH] cmd/ofx: Add download-profile command --- cmd/ofx/main.go | 1 + cmd/ofx/profiledownload.go | 76 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 cmd/ofx/profiledownload.go diff --git a/cmd/ofx/main.go b/cmd/ofx/main.go index 26da3d8..f7ee8b7 100644 --- a/cmd/ofx/main.go +++ b/cmd/ofx/main.go @@ -7,6 +7,7 @@ import ( ) var commands = []command{ + profileDownloadCommand, getAccountsCommand, downloadCommand, ccDownloadCommand, diff --git a/cmd/ofx/profiledownload.go b/cmd/ofx/profiledownload.go new file mode 100644 index 0000000..bef401f --- /dev/null +++ b/cmd/ofx/profiledownload.go @@ -0,0 +1,76 @@ +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", "./profile.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) + } +}