-
Notifications
You must be signed in to change notification settings - Fork 0
/
for.go
85 lines (74 loc) · 2.05 KB
/
for.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
package codefly
import (
"context"
"github.com/codefly-dev/core/resources"
"github.com/codefly-dev/core/wool"
)
type Query struct {
module string
service string
endpointName string
endpointApi string
ctx context.Context
withDefaultNetwork bool
}
func For(ctx context.Context) *Query {
q := &Query{ctx: ctx}
if runningService != nil {
q.module = runningService.Module
q.service = runningService.Name
}
return q
}
func (q *Query) Service(s string) *Query {
q.service = s
return q
}
func (q *Query) Module(s string) *Query {
q.module = s
return q
}
func (q *Query) API(name string) *Query {
q.endpointApi = name
return q
}
func (q *Query) Normalize() {
if q.endpointName == "" && q.endpointApi != "" {
q.endpointName = q.endpointApi
}
}
func (q *Query) WithDefaultNetwork() *Query {
q.withDefaultNetwork = true
return q
}
func (q *Query) NetworkInstance() *resources.NetworkInstance {
w := wool.Get(q.ctx).In("NetworkInstance")
q.Normalize()
info := &resources.EndpointInformation{
Module: q.module,
Service: q.service,
API: q.endpointApi,
Name: q.endpointName,
}
instance, err := resources.FindNetworkInstanceInEnvironmentVariables(q.ctx, info, envs)
if err != nil {
if q.withDefaultNetwork {
w.Warn("Cannot find network instance, returning default", wool.Field("info", info), wool.Field("error", err))
return resources.DefaultNetworkInstance(q.endpointApi)
}
return nil
}
return instance
}
func (q *Query) Configuration(key string, name string) (string, error) {
q.Normalize()
unique := resources.ServiceUnique(q.module, q.service)
envKey := resources.ServiceConfigurationKeyFromUnique(unique, key, name)
return resources.FindValueInEnvironmentVariables(q.ctx, envKey, envs)
}
func (q *Query) Secret(key string, name string) (string, error) {
q.Normalize()
unique := resources.ServiceUnique(q.module, q.service)
envKey := resources.ServiceSecretConfigurationKeyFromUnique(unique, key, name)
return resources.FindValueInEnvironmentVariables(q.ctx, envKey, envs)
}