[#3896bk] Better support for XML documents
This commit is contained in:
parent
35cdff904f
commit
e1435bfe8f
|
@ -21,6 +21,17 @@ import (
|
||||||
// it is common to see variables such as "{{config.docker.interface}}"
|
// it is common to see variables such as "{{config.docker.interface}}"
|
||||||
var configMatchRegex = regexp.MustCompile(`{{\s?config\.([\w.-]+)\s?}}`)
|
var configMatchRegex = regexp.MustCompile(`{{\s?config\.([\w.-]+)\s?}}`)
|
||||||
|
|
||||||
|
// Regex to support modifying XML inline variable data using the config tools. This means
|
||||||
|
// you can pass a replacement of Root.Property='[value="testing"]' to get an XML node
|
||||||
|
// matching:
|
||||||
|
//
|
||||||
|
// <Root>
|
||||||
|
// <Property value="testing"/>
|
||||||
|
// </Root>
|
||||||
|
//
|
||||||
|
// noinspection RegExpRedundantEscape
|
||||||
|
var xmlValueMatchRegex = regexp.MustCompile(`^\[([\w]+)='(.*)'\]$`)
|
||||||
|
|
||||||
// Gets the []byte representation of a configuration file to be passed through to other
|
// Gets the []byte representation of a configuration file to be passed through to other
|
||||||
// handler functions. If the file does not currently exist, it will be created.
|
// handler functions. If the file does not currently exist, it will be created.
|
||||||
func readFileBytes(path string) ([]byte, error) {
|
func readFileBytes(path string) ([]byte, error) {
|
||||||
|
|
|
@ -162,9 +162,16 @@ func (f *ConfigurationFile) parseXmlFile(path string) error {
|
||||||
|
|
||||||
// Iterate over the elements we found and update their values.
|
// Iterate over the elements we found and update their values.
|
||||||
for _, element := range doc.FindElements(path) {
|
for _, element := range doc.FindElements(path) {
|
||||||
|
if xmlValueMatchRegex.Match(value) {
|
||||||
|
k := xmlValueMatchRegex.ReplaceAllString(string(value), "$1")
|
||||||
|
v := xmlValueMatchRegex.ReplaceAllString(string(value), "$2")
|
||||||
|
|
||||||
|
element.CreateAttr(k, v)
|
||||||
|
} else {
|
||||||
element.SetText(string(value))
|
element.SetText(string(value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// If you don't truncate the file you'll end up duplicating the data in there (or just appending
|
// If you don't truncate the file you'll end up duplicating the data in there (or just appending
|
||||||
// to the end of the file. We don't want to do that.
|
// to the end of the file. We don't want to do that.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user