Skip to content

Commit

Permalink
feat: add ability to interpolate variables
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Oct 3, 2022
1 parent 708660e commit cb95523
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Execute(version string) {
}

func loadConfig() (*c.Config, error) {
params := c.LoadParam{
params := c.LoadConfigInput{
Path: pathToConfig,
Stage: stage,
}
Expand Down
46 changes: 40 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"bytes"
"fmt"
"html/template"
"io/ioutil"

"github.com/adikari/safebox/v2/store"
Expand All @@ -24,12 +26,12 @@ type Config struct {
Secrets []store.ConfigInput
}

type LoadParam struct {
type LoadConfigInput struct {
Path string
Stage string
}

func Load(param LoadParam) (*Config, error) {
func Load(param LoadConfigInput) (*Config, error) {
yamlFile, err := ioutil.ReadFile(param.Path)

if err != nil {
Expand Down Expand Up @@ -61,31 +63,38 @@ func Load(param LoadParam) (*Config, error) {
return &c, nil
}

func parseConfig(rc rawConfig, c *Config, param LoadParam) {
func parseConfig(rc rawConfig, c *Config, param LoadConfigInput) {
variables := map[string]string{
"stage": param.Stage,
"service": c.Service,
}

for key, value := range rc.Config["defaults"] {
c.Configs = append(c.Configs, store.ConfigInput{
Name: formatPath(param.Stage, c.Service, key),
Value: value,
Value: interpolate(value, variables),
Secret: false,
})
}

for key, value := range rc.Config["shared"] {
c.Configs = append(c.Configs, store.ConfigInput{
Name: formatSharedPath(param.Stage, key),
Value: value,
Value: interpolate(value, variables),
Secret: false,
})
}

for key, value := range rc.Config[param.Stage] {
c.Configs = append(c.Configs, store.ConfigInput{
Name: formatPath(param.Stage, c.Service, key),
Value: value,
Value: interpolate(value, variables),
Secret: false,
})
}

c.Configs = removeDuplicate(c.Configs)

for key, value := range rc.Secret["defaults"] {
c.Secrets = append(c.Secrets, store.ConfigInput{
Name: formatPath(param.Stage, c.Service, key),
Expand All @@ -112,3 +121,28 @@ func formatSharedPath(stage string, key string) string {
func formatPath(stage string, service string, key string) string {
return fmt.Sprintf("/%s/%s/%s", stage, service, key)
}

func interpolate(value string, variables map[string]string) string {
var result bytes.Buffer
tmpl, _ := template.New("interpolate").Parse(value)

tmpl.Execute(&result, variables)
return result.String()
}

func removeDuplicate(input []store.ConfigInput) []store.ConfigInput {
var unique []store.ConfigInput

loop:
for _, v := range input {
for i, u := range unique {
if v.Name == u.Name {
unique[i] = v
continue loop
}
}
unique = append(unique, v)
}

return unique
}
4 changes: 2 additions & 2 deletions example/safebox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ config:
DB_NAME: "database name updated"
API_ENDPOINT: "endpoint"
NEW: "endpoint"
NEW2: "endpoint"
NEW2: "endpoint updated"

prod:
DB_NAME: "production db name"

dev:
DB_NAME: "dev db name"
DB_NAME: "dev db name {{.stage}}"

shared:
SHARED_KEY: "shared key"
Expand Down

0 comments on commit cb95523

Please sign in to comment.