Skip to content

Commit

Permalink
Added basic env var substitution in config for from-file load #201
Browse files Browse the repository at this point in the history
  • Loading branch information
yyyar committed May 26, 2019
1 parent 014b5c3 commit a1cfa5b
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion cmd/from-file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ package cmd
import (
"io/ioutil"
"log"
"os"
"regexp"
"strings"

"github.com/spf13/cobra"
"github.com/yyyar/gobetween/config"
Expand Down Expand Up @@ -42,10 +45,15 @@ var FromFileCmd = &cobra.Command{
}

var cfg config.Config
if err = codec.Decode(string(data), &cfg, format); err != nil {

datastr := mapEnvVars(string(data))

if err = codec.Decode(datastr, &cfg, format); err != nil {
log.Fatal(err)
}

log.Println(cfg)

info.Configuration = struct {
Kind string `json:"kind"`
Path string `json:"path"`
Expand All @@ -54,3 +62,17 @@ var FromFileCmd = &cobra.Command{
start(&cfg)
},
}

//
// mapEnvVars replaces placeholders ${...} with env var value
//
func mapEnvVars(data string) string {

var re = regexp.MustCompile(`\${.*?}`)

vars := re.FindAllString(data, -1)
for _, v := range vars {
data = strings.ReplaceAll(data, v, os.Getenv(v[2:len(v)-1]))
}
return data
}

0 comments on commit a1cfa5b

Please sign in to comment.