Skip to content

Commit

Permalink
Merge pull request #4 from turbosquid/do-not-override-envars
Browse files Browse the repository at this point in the history
Ability to filter out commented envars and variables that are already…
  • Loading branch information
mowings authored Mar 22, 2022
2 parents 72651f2 + 65812a6 commit ea7796b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/config_keeper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

// VERSION of application
const VERSION = "2.1.0"
const VERSION = "2.2.0"

// Params holds parameters
type Params struct {
Expand All @@ -29,6 +29,7 @@ type Params struct {
Destination string
FileType string
RequireAllPaths bool
OverrideEnvVar bool
}

func main() {
Expand Down Expand Up @@ -72,6 +73,10 @@ func main() {
}
}

if params.FileType == "env" {
data, err = filterEnv(data, params.OverrideEnvVar)
}

err = writeDestination(params.Destination, data)
if err != nil {
log.Fatalf("Failed to write to destination: %s", err)
Expand All @@ -98,6 +103,7 @@ func parseParams() Params {
flag.StringVar(&params.Servers, "zk", "", "zookeeper servers comma delimited")
flag.BoolVar(&params.RequireAllPaths, "requireall", false, "requireallpaths")
flag.StringVar(&params.FileType, "type", "env", "type of file (env, json, yaml)")
flag.BoolVar(&params.OverrideEnvVar, "override", false, "override system environment variables")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "\n%s %s:\n", binName, VERSION)
fmt.Fprintf(flag.CommandLine.Output(), "\nArguments:\n")
Expand Down Expand Up @@ -148,6 +154,40 @@ func readFile(path string) (string, error) {
return string(data), err
}

func filterEnv(input string, override bool) (string, error) {
data := make(map[string]string)
// Keep keys in slice because map does not preserve order
var keys []string
lines := stringToSlice(input)

for _, line := range lines {
if !isEmptyLine(line) {
key, value, err := parseLine(line)
if err == nil {
// ignore comments
if key[0:1] != "#" {
_, present := os.LookupEnv(key)
// ignore existing environment vars
if override || !present {
keys = append(keys, key)
data[key] = value
} else {
log.Printf("Skipping key %s. Exists in system environment vars.", key)
}
}
} else {
log.Printf("Unable to parse line (%s): %s", err, line)
}
}
}

dataBytes := new(bytes.Buffer)
for _, key := range keys {
fmt.Fprintf(dataBytes, "%s=%s\n", key, data[key])
}
return dataBytes.String(), nil
}

func combineEnv(a string, b string) (string, error) {
data := make(map[string]string)
// Keep keys in slice because map does not preserve order
Expand Down
12 changes: 12 additions & 0 deletions src/config_keeper/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"io/ioutil"
"os"
"strings"
"testing"

Expand All @@ -20,6 +21,17 @@ func TestCombineEnv(t *testing.T) {
assert.Equal(t, string(expected), combined, "The combined should match the expected")
}

func TestFilterEnv(t *testing.T) {
os.Setenv("e", "6")
b, err := ioutil.ReadFile("testdata/b.env")
assert.NoError(t, err)
expected, err := ioutil.ReadFile("testdata/override.env")
assert.NoError(t, err)
filtered, err := filterEnv(string(b), false)
assert.NoError(t, err)
assert.Equal(t, string(expected), filtered, "The filtered should match the expected")
}

func TestCombineJson(t *testing.T) {
a, err := ioutil.ReadFile("testdata/a.json")
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions src/config_keeper/testdata/override.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c=5

0 comments on commit ea7796b

Please sign in to comment.