-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
76 lines (62 loc) · 1.68 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
package goaws
import (
"os"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/pkg/errors"
)
type Config struct {
Provider client.ConfigProvider
}
func NewConfig(options ...func(*aws.Config)) (*Config, error) {
config, err := newConfig(options...)
if err != nil {
return &Config{}, errors.Wrap(err, "unable to create config")
}
return config, nil
}
func newConfig(options ...func(*aws.Config)) (*Config, error) {
awsConfig := &aws.Config{}
for _, opt := range options {
opt(awsConfig)
}
if awsConfig.Region == nil || *awsConfig.Region == "" {
regionEnvVar := os.Getenv("AWS_REGION")
if regionEnvVar == "" {
return &Config{}, errors.New("AWS_REGION environment variable not found")
}
Region(regionEnvVar)(awsConfig)
}
sess, err := session.NewSession(awsConfig)
if err != nil {
return &Config{}, errors.Wrap(err, "unable to create AWS session")
}
return &Config{Provider: sess}, nil
}
func Region(region string) func(*aws.Config) {
return func(c *aws.Config) {
c.Region = aws.String(region)
}
}
func MaxRetries(max int) func(*aws.Config) {
return func(c *aws.Config) {
c.MaxRetries = aws.Int(max)
}
}
func Credentials(id, secret, token string) func(*aws.Config) {
return func(c *aws.Config) {
c.WithCredentials(credentials.NewStaticCredentials(id, secret, token))
}
}
func Endpoint(endpoint string) func(*aws.Config) {
return func(c *aws.Config) {
c.WithEndpoint(endpoint)
}
}
func Debug() func(*aws.Config) {
return func(c *aws.Config) {
c.WithLogLevel(aws.LogDebugWithRequestRetries).WithLogLevel(aws.LogDebugWithRequestErrors)
}
}