Skip to content
Open
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
53 changes: 51 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -94,10 +95,10 @@ func readConf() {
}

if node, ok := root["driver"].(string); ok {
driver = node
driver = subEnvVars(node)
}
if node, ok := root["source"].(string); ok {
source = node
source = subEnvVars(node)
}
}

Expand Down Expand Up @@ -300,3 +301,51 @@ func copyFile(dst, src string) (int64, error) {
defer df.Close()
return io.Copy(df, sf)
}


const (
WAITING_FOR_START = 0
WAITING_FOR_MUSTACHE = 1
WAITING_FOR_END = 2
)
func subEnvVars(in string) string {
state := WAITING_FOR_START
var collected *bytes.Buffer

result := new(bytes.Buffer)

for _, c := range in {
switch state {

case WAITING_FOR_START:
switch c {
case '$':
state = WAITING_FOR_MUSTACHE
default:
result.WriteRune(c)
}

case WAITING_FOR_MUSTACHE:
switch c {
case '{':
collected = new(bytes.Buffer)
state = WAITING_FOR_END
default:
result.WriteRune('$')
result.WriteRune(c)
state = WAITING_FOR_START
}

case WAITING_FOR_END:
switch c {
case '}':
v := strings.ToUpper(collected.String())
result.WriteString(os.Getenv(v))
state = WAITING_FOR_START
default:
collected.WriteRune(c)
}
}
}
return result.String()
}
27 changes: 27 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"os"
"testing"
)

func TestEnvVarReplacement(t *testing.T) {
if os.Setenv("FOO", "yakshaving") != nil {
t.Fatalf("can't set environment var for test (foo)")
}
if os.Setenv("BAR", "fleazil") != nil {
t.Fatalf("can't set environment var for test (bar)")
}
if subEnvVars("${foo}") != "yakshaving" {
t.Errorf("failed simple variable substitution")
}
if subEnvVars("${foo}x${bar}") != "yakshavingxfleazil" {
t.Errorf("failed variable substitution combined with concat test")
}
if subEnvVars("$$x${bar}") != "$$xfleazil" {
t.Errorf("failed variable substitution combined with concat test")
}
if subEnvVars("{$}x${bar}$foo") != "{$}xfleazil$foo" {
t.Errorf("failed variable substitution combined with concat test")
}
}
2 changes: 1 addition & 1 deletion dialects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

import (
_ "github.com/lib/pq"
_ "github.com/ziutek/mymysql/godrv"
//"github.com/ziutek/mymysql/godrv"
)

var toRun = []dialectInfo{
Expand Down
1 change: 1 addition & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ func columnsMarkersAndValuesForModel(dialect Dialect, model *Model, markerPos *i
}
return columns, markers, values
}