modified parameter injection strategy. see #538

This commit is contained in:
Brad Rydzewski
2014-10-12 13:02:53 -07:00
parent bbc0d37ad1
commit 629547813c
6 changed files with 59 additions and 18 deletions

View File

@@ -0,0 +1,16 @@
package script
import (
"strings"
)
func Inject(script string, params map[string]string) string {
if params == nil {
return script
}
injected := script
for k, v := range params {
injected = strings.Replace(injected, "$$"+k, v, -1)
}
return injected
}

View File

@@ -0,0 +1,32 @@
package script
import (
"github.com/franela/goblin"
"testing"
)
func Test_Inject(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Inject params", func() {
g.It("Should replace vars with $$", func() {
s := "echo $$FOO $BAR"
m := map[string]string{}
m["FOO"] = "BAZ"
g.Assert("echo BAZ $BAR").Equal(Inject(s, m))
})
g.It("Should not replace vars with single $", func() {
s := "echo $FOO $BAR"
m := map[string]string{}
m["FOO"] = "BAZ"
g.Assert(s).Equal(Inject(s, m))
})
g.It("Should not replace vars in nil map", func() {
s := "echo $$FOO $BAR"
g.Assert(s).Equal(Inject(s, nil))
})
})
}

View File

@@ -1,8 +1,6 @@
package script
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
@@ -16,11 +14,11 @@ import (
"github.com/drone/drone/shared/build/repo"
)
func ParseBuild(data string, params map[string]string) (*Build, error) {
func ParseBuild(data string) (*Build, error) {
build := Build{}
// parse the build configuration file
err := yaml.Unmarshal(injectParams([]byte(data), params), &build)
err := yaml.Unmarshal([]byte(data), &build)
return &build, err
}
@@ -30,15 +28,7 @@ func ParseBuildFile(filename string) (*Build, error) {
return nil, err
}
return ParseBuild(string(data), nil)
}
// injectParams injects params into data.
func injectParams(data []byte, params map[string]string) []byte {
for k, v := range params {
data = bytes.Replace(data, []byte(fmt.Sprintf("{{%s}}", k)), []byte(v), -1)
}
return data
return ParseBuild(string(data))
}
// Build stores the configuration details for