-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (86 loc) · 2.24 KB
/
main.go
File metadata and controls
100 lines (86 loc) · 2.24 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"github.com/code-cell/tracking/data"
)
var (
clientsFile string
invoicesFile string
hoursPattern string
fromFile string
invoiceNum string
output string
)
func main() {
flag.StringVar(&clientsFile, "clients", "clients.yaml", "YAML file containing the list of clients")
flag.StringVar(&invoicesFile, "invoices", "invoices.yaml", "YAML file containing the list of invoices")
flag.StringVar(&hoursPattern, "hours", "hours*.yaml", "Pattern to find YAML files containing the list of hours")
flag.StringVar(&fromFile, "from", "from.yaml", "YAML file containing the info about the company generating the invoice")
flag.StringVar(&invoiceNum, "i", "", "Invoice number to generate")
flag.StringVar(&output, "o", "", "Output file (default to <invoice_number>.pdf)")
flag.Parse()
if invoiceNum == "" {
log.Fatal("Please, specify an invoice to generate")
}
if output == "" {
output = fmt.Sprintf("%v.pdf", invoiceNum)
}
clientsRaw, err := ioutil.ReadFile(clientsFile)
if err != nil {
log.Fatal(err)
}
clients := data.ParseClients(string(clientsRaw))
invoicesRaw, err := ioutil.ReadFile(invoicesFile)
if err != nil {
log.Fatal(err)
}
invoices := data.ParseInvoices(string(invoicesRaw))
matches, err := filepath.Glob(hoursPattern)
if err != nil {
log.Fatal(err)
}
hours := []*data.Hour{}
for _, match := range matches {
hoursRaw, err := ioutil.ReadFile(match)
if err != nil {
log.Fatal(err)
}
hours = append(hours, data.ParseHours(string(hoursRaw))...)
}
fromRaw, err := ioutil.ReadFile(fromFile)
if err != nil {
log.Fatal(err)
}
from := data.ParseFrom(string(fromRaw))
var invoice *data.Invoice
for _, i := range invoices {
if i.Number == invoiceNum {
invoice = i
break
}
}
if invoice == nil {
log.Fatal("Invoice not found")
}
var client *data.Client
for _, c := range clients {
if c.Key == invoice.Client {
client = c
break
}
}
if client == nil {
log.Fatal("Client not found")
}
invoiceHours := make([]*data.Hour, 0)
for _, h := range hours {
if h.Client == client.Key && invoice.Contains(h) {
invoiceHours = append(invoiceHours, h)
}
}
generateInvoice(output, from, invoice, client, invoiceHours)
}