-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.go
70 lines (58 loc) · 1.32 KB
/
tracker.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
package config
import (
"errors"
"github.com/awlsring/texit/internal/pkg/config"
)
type TrackerType string
const (
TrackerTypeInMemory TrackerType = "memory"
TrackerTypeDynamoDb TrackerType = "dynamodb"
)
const (
TrackerAwsAccessKey = "TRACKER_AWS_ACCESS_KEY_ID"
TrackerAwsSecretKey = "TRACKER_AWS_SECRET_ACCESS_KEY"
TrackerAwsRegion = "TRACKER_AWS_REGION"
)
type TrackerConfig struct {
Type TrackerType `yaml:"type"`
AccessKey string `yaml:"accessKey"`
SecretKey string `yaml:"secretKey"`
Region string `yaml:"region"`
}
func NewDefaultTrackerConfig() *TrackerConfig {
return &TrackerConfig{
Type: TrackerTypeInMemory,
}
}
func (c *TrackerConfig) Validate() error {
switch c.Type {
case TrackerTypeInMemory:
return nil
case TrackerTypeDynamoDb:
return c.ValidateDynamoDb()
default:
return errors.New("unknown notifier type")
}
}
func (c *TrackerConfig) ValidateDynamoDb() error {
if c.AccessKey == "" {
val, err := config.AwsAccessKeyFromEnv(TrackerAwsAccessKey)
if err == nil {
c.AccessKey = val
}
}
if c.SecretKey == "" {
val, err := config.SecretKeyFromEnv(TrackerAwsSecretKey)
if err == nil {
c.SecretKey = val
}
}
if c.Region == "" {
val, err := config.RegionFromEnv(TrackerAwsRegion)
if err != nil {
return err
}
c.Region = val
}
return nil
}