/
naming.go
75 lines (60 loc) · 1.65 KB
/
naming.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
package sqs
import (
"fmt"
"strings"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/cloud/aws"
)
type QueueNameSettingsAware interface {
GetAppId() cfg.AppId
GetClientName() string
GetQueueId() string
IsFifoEnabled() bool
}
type QueueNameSettings struct {
AppId cfg.AppId
ClientName string
FifoEnabled bool
QueueId string
}
func (s QueueNameSettings) GetAppId() cfg.AppId {
return s.AppId
}
func (s QueueNameSettings) GetClientName() string {
return s.ClientName
}
func (s QueueNameSettings) IsFifoEnabled() bool {
return s.FifoEnabled
}
func (s QueueNameSettings) GetQueueId() string {
return s.QueueId
}
type QueueNamingSettings struct {
Pattern string `cfg:"pattern,nodecode" default:"{project}-{env}-{family}-{group}-{queueId}"`
}
func GetQueueName(config cfg.Config, queueSettings QueueNameSettingsAware) (string, error) {
if len(queueSettings.GetClientName()) == 0 {
return "", fmt.Errorf("the client name shouldn't be empty")
}
namingKey := fmt.Sprintf("%s.naming", aws.GetClientConfigKey("sqs", queueSettings.GetClientName()))
namingSettings := &QueueNamingSettings{}
config.UnmarshalKey(namingKey, namingSettings)
name := namingSettings.Pattern
appId := queueSettings.GetAppId()
values := map[string]string{
"project": appId.Project,
"env": appId.Environment,
"family": appId.Family,
"group": appId.Group,
"app": appId.Application,
"queueId": queueSettings.GetQueueId(),
}
for key, val := range values {
templ := fmt.Sprintf("{%s}", key)
name = strings.ReplaceAll(name, templ, val)
}
if queueSettings.IsFifoEnabled() {
name = name + FifoSuffix
}
return name, nil
}