support ledger file append
This commit is contained in:
44
upload.go
44
upload.go
@@ -1,15 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"local/oauth2"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Upload(config Config, transaction *Transaction) error {
|
||||
switch config.Uploader {
|
||||
case UploaderTodo:
|
||||
return uploadTodo(config, transaction)
|
||||
case UploaderLedger:
|
||||
return uploadLedger(config, transaction)
|
||||
default:
|
||||
return errors.New("not impl: uploader")
|
||||
}
|
||||
}
|
||||
|
||||
func uploadTodo(config Config, transaction *Transaction) error {
|
||||
params := url.Values{
|
||||
"list": {config.TodoList},
|
||||
"title": {transaction.Format()},
|
||||
@@ -32,3 +48,31 @@ func Upload(config Config, transaction *Transaction) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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:"
|
||||
}
|
||||
remote += strings.ReplaceAll(transaction.Account, " ", "")
|
||||
fmt.Fprintf(f, "%-50s%-s\n", formatGMailDate(transaction.Date), transaction.Account)
|
||||
fmt.Fprintf(f, "%-50s%-50s$%.2f\n", "", transaction.Bank, amount)
|
||||
fmt.Fprintf(f, "%-50s%-s\n", "", remote)
|
||||
return nil
|
||||
}
|
||||
|
||||
func formatGMailDate(s string) string {
|
||||
time, err := time.Parse("[Mon, 2 Jan 2006 15:04:05 -0700 (MST)]", s)
|
||||
if err != nil {
|
||||
log.Println(s, err)
|
||||
return s
|
||||
}
|
||||
return time.Format("2006-01-02")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user