44 lines
847 B
Go
44 lines
847 B
Go
package ledger
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/howeyc/ledger"
|
|
)
|
|
|
|
type File string
|
|
|
|
func NewFile(p string) (File, error) {
|
|
f := File(p)
|
|
_, err := f.Deltas()
|
|
return f, err
|
|
}
|
|
|
|
func (file File) Deltas(like ...Like) ([]Delta, error) {
|
|
transactions, err := file.transactions()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]Delta, 0, len(transactions)*2)
|
|
for _, transaction := range transactions {
|
|
for _, acc := range transaction.AccountChanges {
|
|
value, _ := acc.Balance.Float64()
|
|
delta := newDelta(transaction.Date, acc.Name, value)
|
|
if !likes(like).all(delta) {
|
|
continue
|
|
}
|
|
result = append(result, delta)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (file File) transactions() ([]*ledger.Transaction, error) {
|
|
f, err := os.Open(string(file))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
return ledger.ParseLedger(f)
|
|
}
|