-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinvoice-to-csv.go
44 lines (37 loc) · 1 KB
/
finvoice-to-csv.go
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
39
40
41
42
43
44
package main
import (
"log"
"os"
"path/filepath"
"github.com/joneskoo/finvoice-to-csv/finvoice"
)
func main() {
// The directory containing the invoice files is provided as a command-line argument
if len(os.Args) < 2 {
log.Fatal("Please provide the directory path as an argument")
}
dirPath := os.Args[1]
// Read the invoice files from the directory
files, err := os.ReadDir(dirPath)
if err != nil {
log.Fatal("Failed to read directory:", err)
}
// Initialize finvoice CSV writer
csvWriter := finvoice.NewCSVWriter(os.Stdout)
defer csvWriter.Flush()
// Process each invoice file
for _, file := range files {
if filepath.Ext(file.Name()) == ".xml" {
// Open the invoice file
filePath := filepath.Join(dirPath, file.Name())
inv, err := finvoice.FromFile(filePath)
if err != nil {
log.Fatal("Failed to parse file:", err)
}
// Write the InvoiceRow fields to the CSV file
if err := csvWriter.Write(inv); err != nil {
log.Fatal("Failed to write CSV:", err)
}
}
}
}