-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
129 lines (111 loc) · 2.35 KB
/
config.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/FurqanSoftware/pog"
)
type Config struct {
Printd ConfigPrintd
Printer ConfigPrinter
Toph ConfigToph
Debug ConfigDebug
}
func (c *Config) initDefaults() {
c.Printd.initDefaults()
c.Printer.initDefaults()
c.Toph.initDefaults()
c.Debug.initDefaults()
}
type ConfigPrintd struct {
FontSize int
LineHeight float64
MarginTop float64
MarginRight float64
MarginBottom float64
MarginLeft float64
TabSize int
HeaderExtra string
ReduceBlankLines bool
KeepPDF bool
DelayAfter time.Duration
DelayError time.Duration
LogColor bool
Throbber bool
}
func (c *ConfigPrintd) initDefaults() {
c.FontSize = 13
c.LineHeight = 20
c.MarginTop = 50
c.MarginRight = 25
c.MarginBottom = 50
c.MarginLeft = 25
c.TabSize = 4
c.HeaderExtra = ""
c.ReduceBlankLines = false
c.KeepPDF = true
c.DelayAfter = 500 * time.Millisecond
c.DelayError = 5 * time.Second
c.LogColor = true
c.Throbber = true
}
type ConfigPrinter struct {
Name string
PageSize PageSize
}
func (c *ConfigPrinter) initDefaults() {
c.PageSize = PageA4
}
type ConfigToph struct {
BaseURL string
Token string
ContestID string
Timeout time.Duration
}
func (c *ConfigToph) initDefaults() {
c.BaseURL = "https://toph.co"
c.Timeout = 30 * time.Second
}
type ConfigDebug struct {
DontPrint bool
}
func (c *ConfigDebug) initDefaults() {
c.DontPrint = false
}
func parseConfig() (cfg Config, err error) {
cfg.initDefaults()
b, err := os.ReadFile(flagConfig)
if err != nil {
return
}
_, err = toml.Decode(string(b), &cfg)
if err != nil {
return
}
return
}
func validateConfig(cfg Config) {
if cfg.Toph.BaseURL == "" {
pog.Error("Incomplete configuration: missing Toph base URL")
}
if cfg.Toph.Token == "" {
pog.Error("Incomplete configuration: missing token")
}
if cfg.Toph.ContestID == "" {
pog.Error("Incomplete configuration: missing contest ID")
}
}
func logConfigSummary(cfg Config) {
if cfg.Printer.Name == "" {
pog.Info("∟ Printer: ‹System Default›")
} else {
pog.Infof("∟ Printer: %s", cfg.Printer.Name)
}
pog.Infof("∟ Page Size: %s", cfg.Printer.PageSize)
}
type PageSize string
const (
PageA4 PageSize = "A4"
PageLetter PageSize = "letter"
PageLegal PageSize = "legal"
)