-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (59 loc) · 1.54 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
package main
import (
"fmt"
"os"
"time"
"github.com/kpym/lol/app"
"github.com/kpym/lol/builder"
"github.com/kpym/lol/builder/laton"
"github.com/kpym/lol/builder/ytotech"
"github.com/kpym/lol/log"
"github.com/spf13/pflag"
)
// Error checking
func check(logger log.Logger, err error) {
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
}
func main() {
var err error
var params builder.Parameters
// init the flags
pflag.Usage = app.Help
pflag.CommandLine.SortFlags = false
app.InitFlags()
// get parameters from flags, envs and config file
err = app.GetParameters(¶ms)
check(params.Log, err)
// get the files content based on params.Patterns
files, err := app.GetFiles(params)
check(params.Log, err)
// build the pdf
var compiler builder.Builder
switch params.Service {
case "ytotech":
compiler = ytotech.NewBuilder()
case "laton":
compiler = laton.NewBuilder()
default:
check(params.Log, fmt.Errorf("Unknown service %s", params.Service))
}
req := builder.Request{Parameters: params, Files: files}
params.Log.Info("Send request with the following parameters:\n%s", req.String())
sendtime := time.Now()
pdf, err := compiler.BuildPDF(req)
params.Log.Info("Answer received in %1.1f seconds.", time.Since(sendtime).Seconds())
check(params.Log, err)
// write the pdf
if params.Output != "" {
params.Log.Info("Write %s.", params.Output)
err = os.WriteFile(params.Output, pdf, 0644)
check(params.Log, err)
} else {
params.Log.Info("Write to stdout.")
_, err = os.Stdout.Write(pdf)
check(params.Log, err)
}
}