Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/cogs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/Bestowinc/cogs"
)

const cogsVersion = "0.10.1"
const cogsVersion = "0.10.2"
const usage string = `
COGS COnfiguration manaGement S

Expand Down
1 change: 1 addition & 0 deletions examples/4.read_types.cog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ type = "dotenv"
var1 = {path = [], name = "VAR_1"}
var2 = {path = [], name = "VAR_2"}
var3 = {path = [[], ".jsonMap"], type = "json"}
var4 = {path = [], name = "KEY-WITH_DASH"}
33 changes: 32 additions & 1 deletion input.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,9 @@ func (vi *visitor) get(subPath string) (*yaml.Node, error) {
func visitDotenv(cache map[string]interface{}, node *yaml.Node) (err error) {
var strEnv string

// Handle dashes in variable names by creating a mapping
keyMapping := make(map[string]string)

if err = node.Decode(&strEnv); err != nil {
var sliceEnv []string
if err := node.Decode(&sliceEnv); err != nil {
Expand All @@ -325,6 +328,28 @@ func visitDotenv(cache map[string]interface{}, node *yaml.Node) (err error) {
if strings.Contains(line, "\n") {
sliceEnv[x] = regexp.MustCompile(`(?s)=([^"'].+)`).ReplaceAllString(line, `='$1'`)
}

// Find and replace keys with dashes
if strings.Contains(line, "=") {
parts := strings.SplitN(line, "=", 2)
if len(parts) == 2 {
originalKey := parts[0]
if strings.Contains(originalKey, "-") {
sanitizedKey := strings.ReplaceAll(originalKey, "-", "_")
uniqueKey := sanitizedKey
counter := 1
for {
if _, exists := keyMapping[uniqueKey]; !exists {
break
}
uniqueKey = fmt.Sprintf("%s_%d", sanitizedKey, counter)
counter++
}
keyMapping[uniqueKey] = originalKey
sliceEnv[x] = uniqueKey + "=" + parts[1]
}
}
}
}

strEnv = strings.Join(sliceEnv, "\n")
Expand All @@ -334,8 +359,14 @@ func visitDotenv(cache map[string]interface{}, node *yaml.Node) (err error) {
if err != nil {
return err
}

// Restore original keys with dashes
for k, v := range dotenvMap {
cache[k] = v
if originalKey, exists := keyMapping[k]; exists {
cache[originalKey] = v
} else {
cache[k] = v
}
}

return err
Expand Down
2 changes: 2 additions & 0 deletions test_files/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ configMapGenerator:
SOME_MULTILINE_JSON={
"some_key": "some_value",
}
- VAL_WITH_DASH=some-val
- KEY-WITH_DASH=some_val
jsonMap: { "var3": "var3_value" }
complexJsonMap: |
{
Expand Down
Loading