Skip to content

Commit 435a2cd

Browse files
committed
fix: update some implementations and add more tests
1 parent 46277f8 commit 435a2cd

File tree

17 files changed

+127
-82
lines changed

17 files changed

+127
-82
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
OWNER := dnitsch
33
NAME := configmanager
4-
VERSION := v0.7.1
4+
VERSION := v0.8.1
55
REVISION := $(shell git rev-parse --short HEAD)
66

77
LDFLAGS := -ldflags="-s -w -X \"github.com/$(OWNER)/$(NAME)/cmd/configmanager.Version=$(VERSION)\" -X \"github.com/$(OWNER)/$(NAME)/cmd/configmanager.Revision=$(REVISION)\" -extldflags -static"

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Alternatively you can set the path as stdout which will reduce the need to save
7575
>!Warning! about eval - if you are retrieving secrets from sources you don't control the input of - best to stick wtih the file approach and then delete the file.
7676
7777
```bash
78-
eval "$(AWS_PROFILE=iagadminnonprod configmanager r -t AWSSECRETS#/appxyz/service1-password -t AWSPARAMSTR#/appxyz/service12-settings -p stdout)" && ./.ignore-out.sh
78+
eval "$(configmanager r -t AWSSECRETS#/appxyz/service1-password -t AWSPARAMSTR#/appxyz/service12-settings -p stdout)" && ./.ignore-out.sh
7979
```
8080

8181
The token is made up of 3 parts:
@@ -112,12 +112,10 @@ func replaceTokens(in string, t *v1alpha.CustomFooCrdSpec) error {
112112
m, err := configmanager.Retrieve(tokens, cnf)
113113

114114
if err != nil {
115-
log.Error().Msgf("failed to retrieve vars from tokens. err: %s", err)
116115
return err
117116
}
118117

119118
if err := json.Unmarshal(replaceString(m, in), &t); err != nil {
120-
log.Error().Msgf("failed to unmarshal back to type with replaced vars. err: %s", err)
121119
return err
122120
}
123121
return nil

cmd/configmanager/retrieve.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"fmt"
55

6-
"github.com/dnitsch/configmanager/internal/utils"
76
"github.com/dnitsch/configmanager/pkg/generator"
87
"github.com/spf13/cobra"
98
)
@@ -39,9 +38,9 @@ func init() {
3938
}
4039

4140
func retrieveRun(cmd *cobra.Command, args []string) error {
42-
4341
conf := generator.NewConfig().WithTokenSeparator(tokenSeparator).WithOutputPath(path)
44-
err := utils.GenerateTokens(*conf, tokens)
42+
gv := generator.NewGenerator().WithConfig(conf)
43+
err := gv.GenerateFromCmd(tokens)
4544
if err != nil {
4645
return err
4746
}

cmd/configmanager/root.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,4 @@ func Execute() {
2828

2929
func init() {
3030
configmanagerCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbosity level")
31-
// // Set global log level
32-
// lvl, err := zerolog.ParseLevel(strings.ToLower(os.Getenv("LOG_LEVEL")))
33-
// if err != nil {
34-
// log.Error().Str("StartUpLogger", err.Error())
35-
// return true, nil
36-
// }
37-
// logger := zerolog.New(os.Stderr).With().Timestamp().Logger().Level(lvl)
3831
}

configmanager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import (
66
"github.com/dnitsch/configmanager/pkg/generator"
77
)
88

9+
type ConfigManager interface {
10+
Retrieve(tokens []string, config generator.GenVarsConfig) (generator.ParsedMap, error)
11+
Insert()
12+
}
13+
914
// Retrieve gets a rawMap from a set implementation
1015
// will be empty if no matches found
1116
func Retrieve(tokens []string, config generator.GenVarsConfig) (generator.ParsedMap, error) {
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
package utils
1+
package generator
22

33
import (
44
"fmt"
55

6-
"github.com/dnitsch/configmanager/pkg/generator"
76
"github.com/dnitsch/configmanager/pkg/log"
87
)
98

10-
// GenerateTokens is a helper cmd method to call from retrieveCmd
11-
func GenerateTokens(config generator.GenVarsConfig, tokens []string) error {
12-
gv := generator.NewGenerator()
13-
gv.WithConfig(&config)
9+
// GenerateFromTokens is a helper cmd method to call from retrieveCmd
10+
func (gv *GenVars) GenerateFromCmd(tokens []string) error {
1411
_, err := gv.Generate(tokens)
1512
if err != nil {
1613
log.Errorf("%e", err)
@@ -25,11 +22,11 @@ func GenerateTokens(config generator.GenVarsConfig, tokens []string) error {
2522
log.Errorf("%e", err)
2623
return err
2724
}
28-
log.Infof("Vars written to: %s\n", f)
25+
log.Infof("Vars written to: %s", f)
2926
return nil
3027
}
3128

3229
//UploadTokensWithVals takes in a map of key/value pairs and uploads them
33-
func UploadTokensWithVals(config generator.GenVarsConfig, tokens map[string]string) error {
30+
func (gv *GenVars) UploadTokensWithVals(tokens map[string]string) error {
3431
return fmt.Errorf("notYetImplemented")
3532
}

pkg/generator/cmd_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package generator
2+
3+
import "testing"
4+
5+
func Test_GenerateFromCmd(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
}{}
9+
for _, tt := range tests {
10+
t.Run(tt.name, func(t *testing.T) {
11+
12+
})
13+
}
14+
}

pkg/generator/defaultstrategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ func (implmt *DefaultStrategy) setToken(token string) {
1313
func (implmt *DefaultStrategy) setValue(val string) {
1414
}
1515

16-
func (implmt *DefaultStrategy) getTokenValue(v *genVars) (string, error) {
16+
func (implmt *DefaultStrategy) getTokenValue(v *GenVars) (string, error) {
1717
return "", nil
1818
}

pkg/generator/generator.go

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,27 @@ const (
1515
tokenSeparator = "#"
1616
// keySeparator used for accessing nested
1717
// objects within the retrieved map
18-
keySeparator = "|"
19-
secretMgrPrefix = "AWSSECRETS"
20-
paramStorePrefix = "AWSPARAMSTR"
21-
18+
keySeparator = "|"
19+
//
20+
// secretMgrPrefix = "AWSSECRETS"
21+
// paramStorePrefix = "AWSPARAMSTR"
2222
SecretMgrPrefix = "AWSSECRETS"
2323
ParamStorePrefix = "AWSPARAMSTR"
2424
)
2525

26-
// // VarPrefix maps implementation to prefix
27-
// type VarPrefix struct {
28-
// secretsMgr string
29-
// paramStore string
30-
// }
31-
3226
var (
33-
// varPrefix = VarPrefix{secretsMgr: secretMgrPrefix, paramStore: paramStorePrefix}
3427
// default varPrefix
3528
VarPrefix = map[string]bool{SecretMgrPrefix: true, ParamStorePrefix: true}
3629
)
3730

38-
type genVarsStrategy interface {
39-
getTokenValue(c *genVars) (s string, e error)
40-
setToken(s string)
41-
setValue(s string)
31+
type Generatoriface interface {
32+
Generate(tokens []string) (ParsedMap, error)
33+
ConvertToExportVar()
34+
FlushToFile() (string, error)
4235
}
4336

44-
type genVars struct {
37+
type GenVars struct {
38+
Generatoriface
4539
implementation genVarsStrategy
4640
token string
4741
ctx context.Context
@@ -56,18 +50,18 @@ type genVars struct {
5650
type ParsedMap map[string]any
5751

5852
// NewGenerator returns a new instance
59-
func NewGenerator() *genVars {
53+
func NewGenerator() *GenVars {
6054
defaultStrategy := NewDefatultStrategy()
6155
return newGenVars(defaultStrategy)
6256
}
6357

64-
func newGenVars(e genVarsStrategy) *genVars {
58+
func newGenVars(e genVarsStrategy) *GenVars {
6559
m := ParsedMap{}
6660
defaultConf := GenVarsConfig{
6761
tokenSeparator: tokenSeparator,
6862
keySeparator: keySeparator,
6963
}
70-
return &genVars{
64+
return &GenVars{
7165
implementation: e,
7266
rawMap: m,
7367
// return using default config
@@ -76,7 +70,7 @@ func newGenVars(e genVarsStrategy) *genVars {
7670
}
7771

7872
// WithConfig uses custom config
79-
func (c *genVars) WithConfig(cfg *GenVarsConfig) *genVars {
73+
func (c *GenVars) WithConfig(cfg *GenVarsConfig) *GenVars {
8074
// backwards compatibility
8175
if cfg != nil {
8276
c.config = *cfg
@@ -121,30 +115,13 @@ func (c *GenVarsConfig) WithKeySeparator(keySeparator string) *GenVarsConfig {
121115
return c
122116
}
123117

124-
func (c *genVars) setImplementation(strategy genVarsStrategy) {
125-
c.implementation = strategy
126-
}
127-
128-
func (c *genVars) setToken(s string) {
129-
c.implementation.setToken(s)
130-
}
131-
132-
func (c *genVars) setVaule(s string) {
133-
c.implementation.setValue(s)
134-
}
135-
136-
func (c *genVars) getTokenValue() (string, error) {
137-
log.Info("Strategy implementation")
138-
return c.implementation.getTokenValue(c)
139-
}
140-
141-
func (c *genVars) stripPrefix(in, prefix string) string {
118+
func (c *GenVars) stripPrefix(in, prefix string) string {
142119
return strings.Replace(in, fmt.Sprintf("%s%s", prefix, c.config.tokenSeparator), "", 1)
143120
}
144121

145122
// Generate generates a k/v map of the tokens with their corresponding secret/paramstore values
146123
// the standard pattern of a token should follow a path like
147-
func (c *genVars) Generate(tokens []string) (ParsedMap, error) {
124+
func (c *GenVars) Generate(tokens []string) (ParsedMap, error) {
148125

149126
for _, token := range tokens {
150127
prefix := strings.Split(token, c.config.tokenSeparator)[0]
@@ -160,9 +137,9 @@ func (c *genVars) Generate(tokens []string) (ParsedMap, error) {
160137
return c.rawMap, nil
161138
}
162139

163-
func (c *genVars) retrieveSpecific(prefix, in string) (string, error) {
140+
func (c *GenVars) retrieveSpecific(prefix, in string) (string, error) {
164141
switch prefix {
165-
case secretMgrPrefix:
142+
case SecretMgrPrefix:
166143
// default strategy paramstore
167144
scrtMgr, err := NewSecretsMgr(c.ctx)
168145
if err != nil {
@@ -171,7 +148,7 @@ func (c *genVars) retrieveSpecific(prefix, in string) (string, error) {
171148
c.setImplementation(scrtMgr)
172149
c.setToken(in)
173150
return c.getTokenValue()
174-
case paramStorePrefix:
151+
case ParamStorePrefix:
175152
paramStr, err := NewParamStore(c.ctx)
176153
if err != nil {
177154
return "", err
@@ -199,7 +176,7 @@ func isParsed(res any, trm *ParsedMap) bool {
199176
}
200177

201178
// ConvertToExportVar
202-
func (c *genVars) ConvertToExportVar() {
179+
func (c *GenVars) ConvertToExportVar() {
203180
for k, v := range c.rawMap {
204181
rawKeyToken := strings.Split(k, "/") // assumes a path like token was used
205182
topLevelKey := rawKeyToken[len(rawKeyToken)-1]
@@ -223,7 +200,7 @@ func envVarNormalize(pmap ParsedMap) ParsedMap {
223200
return normalizedMap
224201
}
225202

226-
func (c *genVars) exportVars(exportMap ParsedMap) {
203+
func (c *GenVars) exportVars(exportMap ParsedMap) {
227204

228205
for k, v := range exportMap {
229206
// NOTE: \n line ending is not totally cross platform
@@ -244,7 +221,7 @@ func normalizeKey(k string) string {
244221
return strings.ToUpper(string(r))
245222
}
246223

247-
func (c *genVars) FlushToFile() (string, error) {
224+
func (c *GenVars) FlushToFile() (string, error) {
248225

249226
// moved up to
250227
joinedStr := listToString(c.outString)

pkg/generator/generator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616

1717
type fixture struct {
1818
t *testing.T
19-
c *genVars
19+
c *GenVars
2020
}
2121

2222
func newFixture(t *testing.T) *fixture {

0 commit comments

Comments
 (0)