Merge pull request #228 from crawford/sub
env: add support for escaping environment substitutions
This commit is contained in:
commit
17f8733121
@ -3,6 +3,7 @@ package initialize
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/coreos-cloudinit/system"
|
"github.com/coreos/coreos-cloudinit/system"
|
||||||
@ -62,9 +63,18 @@ func (e *Environment) SetSSHKeyName(name string) {
|
|||||||
e.sshKeyName = name
|
e.sshKeyName = name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply goes through the map of substitutions and replaces all instances of
|
||||||
|
// the keys with their respective values. It supports escaping substitutions
|
||||||
|
// with a leading '\'.
|
||||||
func (e *Environment) Apply(data string) string {
|
func (e *Environment) Apply(data string) string {
|
||||||
for key, val := range e.substitutions {
|
for key, val := range e.substitutions {
|
||||||
data = strings.Replace(data, key, val, -1)
|
matchKey := strings.Replace(key, `$`, `\$`, -1)
|
||||||
|
replKey := strings.Replace(key, `$`, `$$`, -1)
|
||||||
|
|
||||||
|
// "key" -> "val"
|
||||||
|
data = regexp.MustCompile(`([^\\]|^)`+matchKey).ReplaceAllString(data, `${1}`+val)
|
||||||
|
// "\key" -> "key"
|
||||||
|
data = regexp.MustCompile(`\\`+matchKey).ReplaceAllString(data, replKey)
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
@ -55,6 +55,24 @@ ExecStop=/usr/bin/echo $unknown`,
|
|||||||
"$private_ipv4\nfoobar",
|
"$private_ipv4\nfoobar",
|
||||||
"5.6.7.8\nfoobar",
|
"5.6.7.8\nfoobar",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
// Escaping substitutions
|
||||||
|
map[string]string{"$private_ipv4": "127.0.0.1"},
|
||||||
|
`\$private_ipv4
|
||||||
|
$private_ipv4
|
||||||
|
addr: \$private_ipv4
|
||||||
|
\\$private_ipv4`,
|
||||||
|
`$private_ipv4
|
||||||
|
127.0.0.1
|
||||||
|
addr: $private_ipv4
|
||||||
|
\$private_ipv4`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// No substitutions with escaping
|
||||||
|
nil,
|
||||||
|
"\\$test\n$test",
|
||||||
|
"\\$test\n$test",
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
|
|
||||||
env := NewEnvironment("./", "./", "./", "", "", tt.subs)
|
env := NewEnvironment("./", "./", "./", "", "", tt.subs)
|
||||||
|
Loading…
Reference in New Issue
Block a user