~mendelmaleh/pfin

ref: 1f051309601a4da2841fade1e72cb120bccab370 pfin/parser/util/util.go -rw-r--r-- 642 bytes
1f051309Mendel E Add license 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package util

import (
	"os"
	"path/filepath"
	"sort"

	"git.sr.ht/~mendelmaleh/pfin"
)

func ParseDir(path string, parsefn func([]byte, *[]pfin.Transaction) error) ([]pfin.Transaction, error) {
	var txns []pfin.Transaction

	if path[len(path)-1] != filepath.Separator {
		path += string(filepath.Separator)
	}

	matches, err := filepath.Glob(path + "*.csv")
	if err != nil {
		return txns, err
	}

	// sort oldest first
	sort.Strings(matches)

	for _, f := range matches {
		file, err := os.ReadFile(f)
		if err != nil {
			return txns, err
		}

		if err := parsefn(file, &txns); err != nil {
			return txns, err
		}
	}

	return txns, nil
}