-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
170 lines (137 loc) · 3.48 KB
/
service.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package service
import (
"context"
"errors"
"fmt"
"github.com/alexfalkowski/konfig/provider"
source "github.com/alexfalkowski/konfig/source/configurator"
ce "github.com/alexfalkowski/konfig/source/configurator/errors"
)
var (
// ErrNotFound for service.
ErrNotFound = errors.New("not found")
// ErrInvalidArgument for service.
ErrInvalidArgument = errors.New("invalid argument")
)
// IsNotFoundError for service.
func IsNotFoundError(err error) bool {
return errors.Is(err, ErrNotFound)
}
// IsInvalidArgument for service.
func IsInvalidArgument(err error) bool {
return errors.Is(err, ErrInvalidArgument)
}
type (
// Config for a specific application.
Config struct {
application string
version string
environment string
continent string
country string
command string
kind string
}
// Service for the different transports.
Service struct {
provider *provider.Transformer
config source.Configurator
source *source.Transformer
}
)
// NewService for the different transports.
func NewService(provider *provider.Transformer, config source.Configurator, source *source.Transformer) *Service {
return &Service{provider: provider, config: config, source: source}
}
// GetConfig for service.
func (s *Service) GetConfig(ctx context.Context, cfg *Config) ([]byte, error) {
d, err := s.config.GetConfig(ctx, cfg.application, cfg.version,
cfg.environment, cfg.continent, cfg.country,
cfg.command, cfg.kind)
if err != nil {
if ce.IsNotFoundError(err) {
return nil, fmt.Errorf("%s: %w", cfg, ErrNotFound)
}
return nil, err
}
return s.source.Transform(ctx, cfg.Kind(), d)
}
// GetSecrets for service.
func (s *Service) GetSecrets(ctx context.Context, secs map[string]string) (map[string][]byte, error) {
secrets := make(map[string][]byte, len(secs))
for n, v := range secs {
t, err := s.provider.Transform(ctx, v)
if err != nil {
return nil, err
}
switch c := t.(type) {
case string:
secrets[n] = []byte(c)
default:
secrets[n] = []byte(v)
}
}
return secrets, nil
}
// NewConfig for service.
func NewConfig(app, ver, env, continent, country, cmd, kind string) (*Config, error) {
if continent == "" {
continent = "*"
}
if country == "" {
country = "*"
}
if kind == "" {
kind = "yaml"
}
if app == "" {
return nil, fmt.Errorf("application: %w", ErrInvalidArgument)
}
if ver == "" {
return nil, fmt.Errorf("version: %w", ErrInvalidArgument)
}
if env == "" {
return nil, fmt.Errorf("environment: %w", ErrInvalidArgument)
}
if cmd == "" {
return nil, fmt.Errorf("command: %w", ErrInvalidArgument)
}
c := &Config{
application: app, version: ver, environment: env,
continent: continent, country: country,
command: cmd, kind: kind,
}
return c, nil
}
// Application for config.
func (c *Config) Application() string {
return c.application
}
// Version for config.
func (c *Config) Version() string {
return c.version
}
// Environment for config.
func (c *Config) Environment() string {
return c.environment
}
// Continent for config.
func (c *Config) Continent() string {
return c.continent
}
// Country for config.
func (c *Config) Country() string {
return c.country
}
// Command for config.
func (c *Config) Command() string {
return c.command
}
// Kind for config.
func (c *Config) Kind() string {
return c.kind
}
// String of config.
func (c *Config) String() string {
return fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s", c.application, c.version, c.environment, c.continent, c.country, c.command, c.kind)
}