Skip to content

Commit

Permalink
Merge pull request #10 from malud/fix-env-map
Browse files Browse the repository at this point in the history
Split environment key values on first equal sign
  • Loading branch information
Marcel Ludwig authored Sep 3, 2020
2 parents 1f9edee + c3c0118 commit e3ea5c7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 15 deletions.
11 changes: 9 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 2 additions & 11 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,18 @@ import (
"flag"
"io/ioutil"
"os"
"strings"

"github.com/malud/temgo/temgo"
)

var envVars = make(temgo.EnvVars)
var inlineFlag = flag.String("i", "", "-i filename")
var strictFlag = flag.Bool("s", false, "-s")

func init() {
for _, e := range os.Environ() {
str := strings.Split(e, "=")
envVars[str[0]] = str[1]
}

func main() {
if !flag.Parsed() {
flag.Parse()
}
}

func main() {
var rw *bufio.ReadWriter
var file *os.File
if *inlineFlag != "" {
Expand All @@ -49,7 +40,7 @@ func replace(rw *bufio.ReadWriter, file *os.File) {
must(rw.Flush())
}

tg := temgo.New(envVars, *strictFlag)
tg := temgo.New(temgo.NewEnvVars(), *strictFlag)
if tg.ContainsVariable(bytes) {
str, err := tg.ReplaceVariables(bytes)
must(err)
Expand Down
5 changes: 5 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
)

func TestReplace(t *testing.T) {
const urlEnvExample = "https://example.com/?query=param"
os.Setenv("TEMGO_TEST", urlEnvExample)
defer os.Unsetenv("TEMGO_TEST")

wd, _ := os.Getwd()
testCases := []struct {
in []byte
Expand All @@ -20,6 +24,7 @@ func TestReplace(t *testing.T) {
{[]byte("foo {{ NOT_SET }} bar"), "foo {{ NOT_SET }} bar"},
{[]byte("foo {{invalid }} bar"), "foo {{invalid }} bar"},
{[]byte("foo {{ PWD }}!"), fmt.Sprintf("foo %s!", wd)},
{[]byte(`foo "{{ TEMGO_TEST }}"!`), fmt.Sprintf("foo %q!", urlEnvExample)},
}

for _, testCase := range testCases {
Expand Down
20 changes: 20 additions & 0 deletions temgo/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package temgo

import (
"os"
"strings"
)

type EnvVars map[string]string

func NewEnvVars() EnvVars {
envVars := make(EnvVars)
for _, e := range os.Environ() {
idx := strings.IndexByte(e, '=')
if idx == -1 {
continue
}
envVars[e[:idx]] = e[idx+1:]
}
return envVars
}
31 changes: 31 additions & 0 deletions temgo/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package temgo

import (
"os"
"reflect"
"testing"
)

func TestNewEnvVars(t *testing.T) {
want := EnvVars{
"TEMGO_TEST_NORMAL": "someValue",
"TEMGO_TEST_EMPTY": "",
"TEMGO_TEST_MORE_EQUALS": "https://example.com/?query1=param1&query2=param2&query1=param3#fragment",
}

for k, v := range want {
if err := os.Setenv(k, v); err != nil {
t.Fatal(err)
}
defer func() {
if err := os.Unsetenv(k); err != nil {
t.Fatal(err)
}
}()
}
expected := NewEnvVars()

if got := NewEnvVars(); !reflect.DeepEqual(got, expected) {
t.Errorf("\nNewEnvVars() = %v,\nwant %v", got, expected)
}
}
2 changes: 0 additions & 2 deletions temgo/temgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"strings"
)

type EnvVars map[string]string

type Temgo struct {
envVars EnvVars
pattern *regexp.Regexp
Expand Down

0 comments on commit e3ea5c7

Please sign in to comment.