-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
115 lines (92 loc) · 1.94 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
package youtube
import (
"errors"
"fmt"
"log/slog"
"github.com/rubpy/crawly"
"github.com/rubpy/crawly/cclient"
"google.golang.org/api/youtube/v3"
)
//////////////////////////////////////////////////
type config struct {
logger *slog.Logger
client cclient.Client
service *youtube.Service
settings struct {
v CrawlerSettings
ok bool
}
}
var (
NilConfig = errors.New("config is nil")
NilClient = errors.New("client is nil")
NilService = errors.New("service is nil")
)
func validateConfig(cfg *config) error {
if cfg == nil {
return NilConfig
}
if cfg.service == nil {
return NilService
}
return nil
}
func buildCrawlerFromConfig(cfg *config) (cr *Crawler, err error) {
if cfg == nil {
err = NilConfig
return
}
cl := cfg.client
if cl == nil {
logger := cfg.logger
if logger != nil {
logger = logger.WithGroup("client")
}
cl, err = cclient.NewClient(cclient.WithLogger(logger))
if err != nil {
return nil, fmt.Errorf("cclient.NewClient: %w", err)
}
}
svc := cfg.service
if svc == nil {
return nil, NilService
}
cr = &Crawler{
client: cl,
service: svc,
}
cr.Crawler.SetLogger(cfg.logger)
crawly.SetCrawlerHandlers(&cr.Crawler, crawly.CrawlerHandlers{
Order: cr.orderHandler,
Entity: cr.entityHandler,
})
if cfg.settings.ok {
cr.SetSettings(cfg.settings.v)
} else {
cr.SetSettings(DefaultSettings)
}
return cr, nil
}
type ConfigOption func(cfg *config)
//////////////////////////////////////////////////
func WithLogger(logger *slog.Logger) ConfigOption {
return func(cfg *config) {
cfg.logger = logger
}
}
func WithClient(client cclient.Client) ConfigOption {
return func(cfg *config) {
cfg.client = client
}
}
func WithService(service *youtube.Service) ConfigOption {
return func(cfg *config) {
cfg.service = service
}
}
func WithSettings(settings CrawlerSettings) ConfigOption {
return func(cfg *config) {
cfg.settings.v = settings
cfg.settings.ok = true
}
}