-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples.go
135 lines (117 loc) · 3.44 KB
/
examples.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
package examples
import (
"context"
"encoding/json"
"fmt"
"github.com/dnitsch/configmanager"
)
const DO_STUFF_WITH_VALS_HERE = "connstring:user@%v:host=%s/someschema..."
// retrieveExample uses the standard Retrieve method on the API
// this will return generator.ParsedMap which can be later used for more complex use cases
func retrieveExample() {
cm := configmanager.New(context.TODO())
cm.Config.WithTokenSeparator("://")
pm, err := cm.Retrieve([]string{"token1", "token2"})
if err != nil {
panic(err)
}
// put in a loop for many config params
// or use the helper methods to return a yaml replaced struct
//
if pwd, ok := pm["token1"]; ok {
if host, ok := pm["token2"]; ok {
fmt.Println(fmt.Sprintf(DO_STUFF_WITH_VALS_HERE, pwd, fmt.Sprintf("%s", host)))
}
}
}
// retrieveStringOut accepts a string as an input
func retrieveStringOut() {
cm := configmanager.New(context.TODO())
// JSON Marshal K8s CRD into
exampleK8sCrdMarshalled := `apiVersion: crd.foo.custom/v1alpha1
kind: CustomFooCrd
metadata:
name: foo
namespace: bar
spec:
name: baz
secret_val: AWSSECRETS#/customfoo/secret-val
owner: [email protected]
`
pm, err := cm.RetrieveWithInputReplaced(exampleK8sCrdMarshalled)
if err != nil {
panic(err)
}
fmt.Println(pm)
}
// ConfigTokenReplace uses configmanager to replace all occurences of
// replaceable tokens inside a []byte
// this is a re-useable method on all controllers
// will just ignore any non specs without tokens
func SpecConfigTokenReplace[T any](inputType T) (*T, error) {
outType := new(T)
rawBytes, err := json.Marshal(inputType)
if err != nil {
return nil, err
}
cm := configmanager.New(context.TODO())
// use custom token separator
cm.Config.WithTokenSeparator("://")
replaced, err := cm.RetrieveWithInputReplaced(string(rawBytes))
if err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(replaced), outType); err != nil {
return nil, err
}
return outType, nil
}
// Example
func exampleRetrieveYamlUnmarshalled() {
type config struct {
DbHost string `yaml:"dbhost"`
Username string `yaml:"user"`
Password string `yaml:"pass"`
}
configMarshalled := `
user: AWSPARAMSTR:///int-test/pocketbase/config|user
pass: AWSPARAMSTR:///int-test/pocketbase/config|pwd
dbhost: AWSPARAMSTR:///int-test/pocketbase/config|host
`
appConf := &config{}
cm := configmanager.New(context.TODO())
// use custom token separator inline with future releases
cm.Config.WithTokenSeparator("://")
err := cm.RetrieveUnmarshalledFromYaml([]byte(configMarshalled), appConf)
if err != nil {
panic(err)
}
fmt.Println(appConf.DbHost)
fmt.Println(appConf.Username)
fmt.Println(appConf.Password)
}
// ### exampleRetrieveYamlMarshalled
func exampleRetrieveYamlMarshalled() {
type config struct {
DbHost string `yaml:"dbhost"`
Username string `yaml:"user"`
Password string `yaml:"pass"`
}
appConf := &config{
DbHost: "AWSPARAMSTR:///int-test/pocketbase/config|host",
Username: "AWSPARAMSTR:///int-test/pocketbase/config|user",
Password: "AWSPARAMSTR:///int-test/pocketbase/config|pwd",
}
cm := configmanager.New(context.TODO())
cm.Config.WithTokenSeparator("://")
err := cm.RetrieveMarshalledYaml(appConf)
if err != nil {
panic(err)
}
if appConf.DbHost == "AWSPARAMSTR:///int-test/pocketbase/config|host" {
panic(fmt.Errorf("value of DbHost should have been replaced with a value from token"))
}
fmt.Println(appConf.DbHost)
fmt.Println(appConf.Username)
fmt.Println(appConf.Password)
}