-
Notifications
You must be signed in to change notification settings - Fork 0
/
initializer.go
72 lines (59 loc) · 1.58 KB
/
initializer.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
package awsutil
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/go-nacelle/nacelle"
)
type (
ServiceInitializer struct {
Logger nacelle.Logger `service:"logger"`
Services nacelle.ServiceContainer `service:"services"`
name string
factory Factory
configs []*aws.Config
}
Factory func(sess *session.Session) interface{}
)
func NewServiceInitializer(name string, factory Factory, configs ...*aws.Config) *ServiceInitializer {
return &ServiceInitializer{
name: name,
factory: factory,
configs: configs,
}
}
func (i *ServiceInitializer) Init(config nacelle.Config) error {
awsConfig, err := i.loadConfig(config)
if err != nil {
return err
}
sess, err := session.NewSession(append(
[]*aws.Config{awsConfig},
i.configs...,
)...)
if err != nil {
return err
}
return i.Services.Set(i.name, i.factory(sess))
}
func (i *ServiceInitializer) loadConfig(config nacelle.Config) (*aws.Config, error) {
serviceTagModifiers := []nacelle.TagModifier{
nacelle.NewFileTagSetter(),
nacelle.NewEnvTagPrefixer(i.name),
nacelle.NewFileTagPrefixer(i.name),
}
globalTagModifiers := []nacelle.TagModifier{
nacelle.NewFileTagSetter(),
nacelle.NewEnvTagPrefixer("aws"),
nacelle.NewFileTagPrefixer("aws"),
}
serviceConfig := &Config{}
if err := config.Load(serviceConfig, serviceTagModifiers...); err != nil {
return nil, err
}
if serviceConfig.IsDefault() {
if err := config.Load(serviceConfig, globalTagModifiers...); err != nil {
return nil, err
}
}
return serviceConfig.ToAWSConfig(i.Logger), nil
}