-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
77 lines (63 loc) · 1.69 KB
/
main.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
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
package main
import (
"os"
"time"
"github.com/jinzhu/now"
"github.com/relistan/billmonger/invoice"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/relistan/rubberneck.v1"
)
const (
sansFont = "Helvetica"
serifFont = "Times"
)
type CliConfig struct {
ConfigFile *string
BillingDate *string
OutputDir *string
}
func checkImageFile(config *invoice.BillingConfig) error {
_, err := os.Stat(config.Business.ImageFile)
return err
}
func main() {
cli := CliConfig{
ConfigFile: kingpin.Flag("config-file", "The YAML config file to use").Short('c').Default("billing.yaml").String(),
BillingDate: kingpin.Flag("billing-date", "The date to assume the bill is written on").
Short('b').Default(time.Now().Format("2006-01-02")).String(),
OutputDir: kingpin.Flag("output-dir", "The output directory to use. Overridden by config file.").Short('o').Default(".").String(),
}
kingpin.Parse()
// Make sure the supplied time is a valid one
_, err := now.Parse(*cli.BillingDate)
if err != nil {
println(err.Error())
os.Exit(1)
}
config, err := invoice.ParseConfig(*cli.ConfigFile, *cli.BillingDate, *cli.OutputDir)
if err != nil {
println(err.Error())
os.Exit(1)
}
// Print the config
printer := rubberneck.NewDefaultPrinter()
printer.PrintWithLabel("Settings ("+*cli.ConfigFile+")", config)
// Pick up some defaults where needed
if config.Business.SansFont == "" {
config.Business.SansFont = sansFont
}
if config.Business.SerifFont == "" {
config.Business.SerifFont = serifFont
}
err = checkImageFile(config)
if err != nil {
println(err.Error())
os.Exit(1)
}
bill := invoice.NewBill(config)
err = bill.RenderToFile()
if err != nil {
println(err.Error())
os.Exit(1)
}
}