71 lines
1.7 KiB
Go
Executable File
71 lines
1.7 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func Upload(config Config, transaction *Transaction) error {
|
|
switch config.Uploader {
|
|
case UploaderTodo:
|
|
panic("DEAD")
|
|
case UploaderLedger:
|
|
return uploadLedger(config, transaction)
|
|
case UploaderPTTodo:
|
|
return uploadPTTodo(config, transaction)
|
|
default:
|
|
return errors.New("not impl: uploader")
|
|
}
|
|
}
|
|
|
|
func uploadPTTodo(config Config, transaction *Transaction) error {
|
|
f, err := os.Create(fmt.Sprintf("%s.todo.%s", config.TodoAddr, uuid.New().String()))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
fmt.Fprintf(f, `- {"todo":%q, "tags":%q}%s`, transaction.Format(), config.TodoTag, "\n")
|
|
return f.Close()
|
|
}
|
|
|
|
func uploadLedger(config Config, transaction *Transaction) error {
|
|
f, err := os.OpenFile(config.TodoAddr, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
amount, _ := strconv.ParseFloat(transaction.Amount, 32)
|
|
amount *= -1
|
|
remote := "Withdrawal:"
|
|
if amount > 0 {
|
|
remote = "Deposit:"
|
|
}
|
|
regexp := regexp.MustCompile(`[0-9a-zA-Z]`)
|
|
for _, substr := range regexp.FindAllString(transaction.Vendor, -1) {
|
|
remote += substr
|
|
}
|
|
fmt.Fprintf(f, "%-50s%-s\n", formatGMailDate(transaction.Date), transaction.Vendor)
|
|
fmt.Fprintf(f, "%-50s%-50s$%.2f\n", "", "AssetAccount:"+transaction.Bank.String()+":"+transaction.Account, amount)
|
|
fmt.Fprintf(f, "%-50s%-s\n", "", remote)
|
|
return nil
|
|
}
|
|
|
|
func formatGMailDate(s string) string {
|
|
for _, format := range []string{
|
|
"[Mon, 2 Jan 2006 15:04:05 -0700 (MST)]",
|
|
"[Mon, 2 Jan 2006 15:04:05 -0700]",
|
|
} {
|
|
time, err := time.Parse(format, s)
|
|
if err == nil {
|
|
return time.Format("2006-01-02")
|
|
}
|
|
}
|
|
return s
|
|
}
|