-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae80099
commit ec30aac
Showing
12 changed files
with
160 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
telemetry: | ||
logging: | ||
level: INFO # DEBUG, INFO, WARNING, ERROR, FATAL | ||
encoding: json # console, json | ||
|
||
#api: | ||
# http: | ||
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
const directivePattern = `[A-Za-z][A-Za-z0-9+.-]+` | ||
|
||
type RawConfig = map[string]interface{} | ||
|
||
// Expander finds special directives like ${env:ENV_VAR} in the config file and fill them with actual values | ||
type Expander struct{} | ||
|
||
func (e *Expander) Expand(rawConfig RawConfig) (RawConfig, error) { | ||
for configKey, configValue := range rawConfig { | ||
expandedValue, err := e.expandConfigValue(configValue) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rawConfig[configKey] = expandedValue | ||
} | ||
|
||
return rawConfig, nil | ||
} | ||
|
||
func (e *Expander) expandConfigValue(value any) (any, error) { | ||
switch v := value.(type) { | ||
case string: | ||
return e.expandString(v) | ||
case []any: | ||
return e.expandSlice(v) | ||
case map[string]any: | ||
return e.expandMap(v) | ||
default: | ||
// could be int or float | ||
return value, nil | ||
} | ||
} | ||
|
||
func (e *Expander) expandString(value string) (string, error) { | ||
// TODO: implement | ||
return value, nil | ||
} | ||
|
||
func (e *Expander) expandSlice(value []any) ([]any, error) { | ||
slice := make([]any, 0, len(value)) | ||
|
||
for _, vint := range value { | ||
val, err := e.expandConfigValue(vint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
slice = append(slice, val) | ||
} | ||
|
||
return slice, nil | ||
} | ||
|
||
func (e Expander) expandMap(value map[string]any) (map[string]any, error) { | ||
newMap := map[string]any{} | ||
|
||
for mapKey, mapValue := range value { | ||
val, err := e.expandConfigValue(mapValue) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
newMap[mapKey] = val | ||
} | ||
|
||
return newMap, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestExpander_EnvVarExpanded(t *testing.T) { | ||
var testCases = []struct { | ||
name string // test case name (also file name containing config yaml) | ||
}{ | ||
{name: "no-env.yaml"}, | ||
{name: "partial-env.yaml"}, | ||
{name: "all-env.yaml"}, | ||
} | ||
|
||
const valueExtra = "some string" | ||
const valueExtraMapValue = "some map value" | ||
const valueExtraListMapValue = "some list map value" | ||
const valueExtraListElement = "some list value" | ||
|
||
t.Setenv("EXTRA", valueExtra) | ||
t.Setenv("EXTRA_MAP_VALUE_1", valueExtraMapValue+"_1") | ||
t.Setenv("EXTRA_MAP_VALUE_2", valueExtraMapValue+"_2") | ||
t.Setenv("EXTRA_LIST_MAP_VALUE_1", valueExtraListMapValue+"_1") | ||
t.Setenv("EXTRA_LIST_MAP_VALUE_2", valueExtraListMapValue+"_2") | ||
t.Setenv("EXTRA_LIST_VALUE_1", valueExtraListElement+"_1") | ||
t.Setenv("EXTRA_LIST_VALUE_2", valueExtraListElement+"_2") | ||
|
||
for _, test := range testCases { | ||
t.Run(test.name, func(t *testing.T) { | ||
expander := Expander{} | ||
|
||
modifiedConfig, err := expander.Expand() | ||
Check failure on line 33 in pkg/config/expander_test.go GitHub Actions / Vulnerability Check
Check failure on line 33 in pkg/config/expander_test.go GitHub Actions / Vulnerability Check
Check failure on line 33 in pkg/config/expander_test.go GitHub Actions / Vulnerability Check
Check failure on line 33 in pkg/config/expander_test.go GitHub Actions / Tests
Check failure on line 33 in pkg/config/expander_test.go GitHub Actions / Tests
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters