utility for generating ledger transactions from ofx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.9 KiB

package main
import (
"bufio"
"flag"
"github.com/ochinchina/go-ini"
"io"
"log"
"os"
"sort"
"strings"
"text/template"
)
func matchPartial(keys []ini.Key, givenName string) string {
for _, k := range keys {
v := k.ValueWithDefault("")
if strings.Contains(givenName, k.Name()) {
return v
}
}
return "Expenses"
}
func generateLedgerTransactions(transactions []transaction, w io.Writer) error {
t := template.New("ledger")
_, err := t.Parse(transactionTemplate)
if err != nil {
return err
}
t.Execute(w, transactions)
return nil
}
var (
config = flag.String("config", "", "configuration file containing all your vendor and account mappings")
inputType = flag.String("T", "ofx", "input type of transaction data - supports ofx and xml")
)
func main() {
flag.Parse()
filename := flag.Arg(0)
r, err := os.Open(filename)
if err != nil {
log.Fatalf("failed opening data file: %w", err)
}
defer r.Close()
c, err := os.Open(*config)
if err != nil {
log.Fatalf("failed opening config file: %w", err)
}
defer c.Close()
cfg := ini.NewIni()
cfg.LoadReader(c)
assets, err := cfg.GetSection("Assets")
if err != nil {
log.Fatalf("failed finding assets section of config: %w", err)
}
vendors, err := cfg.GetSection("Vendors")
if err != nil {
log.Fatalf("failed finding vendors section of config: %w", err)
}
var transactions []transaction
if *inputType == "ofx" {
transactions, err = transactionsFromOfx(r, *assets, vendors.Keys())
if err != nil {
log.Fatalf("failed generating transactions from ofx: %w", err)
}
} else if *inputType == "xml" {
transactions, err = transactionsFromXml(r, *assets, vendors.Keys())
if err != nil {
log.Fatalf("failed generating transactions from xml: %w", err)
}
}
w := bufio.NewWriter(os.Stdout)
sort.Sort(byDate(transactions))
err = generateLedgerTransactions(transactions, w)
if err != nil {
log.Fatal(err)
}
err = w.Flush()
if err != nil {
log.Fatal(err)
}
}