[#3896bk] Support using configuration param inline with other values

This commit is contained in:
Dane Everitt 2019-12-01 15:27:53 -08:00
parent 1be21b7078
commit 35cdff904f
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
2 changed files with 16 additions and 12 deletions

View File

@ -7,10 +7,20 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"io/ioutil" "io/ioutil"
"os" "os"
"regexp"
"strconv" "strconv"
"strings" "strings"
) )
// Regex to match anything that has a value matching the format of {{ config.$1 }} which
// will cause the program to lookup that configuration value from itself and set that
// value to the configuration one.
//
// This allows configurations to reference values that are node dependent, such as the
// internal IP address used by the daemon, useful in Bungeecord setups for example, where
// it is common to see variables such as "{{config.docker.interface}}"
var configMatchRegex = regexp.MustCompile(`{{\s?config\.([\w.-]+)\s?}}`)
// 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) {
@ -106,7 +116,9 @@ func (f *ConfigurationFile) LookupConfigurationValue(cfr ConfigurationFileReplac
// If there is a match, lookup the value in the configuration for the Daemon. If no key // If there is a match, lookup the value in the configuration for the Daemon. If no key
// is found, just return the string representation, otherwise use the value from the // is found, just return the string representation, otherwise use the value from the
// daemon configuration here. // daemon configuration here.
huntPath := configMatchRegex.ReplaceAllString(cfr.Value, "$1") huntPath := configMatchRegex.ReplaceAllString(
configMatchRegex.FindString(cfr.Value), "$1",
)
var path []string var path []string
// The camel casing is important here, the configuration for the Daemon does not use // The camel casing is important here, the configuration for the Daemon does not use
@ -128,6 +140,8 @@ func (f *ConfigurationFile) LookupConfigurationValue(cfr ConfigurationFileReplac
// is a replace issue at play. // is a replace issue at play.
return []byte(cfr.Value), cfr.ValueType, nil return []byte(cfr.Value), cfr.ValueType, nil
} else { } else {
return match, cfr.ValueType, nil replaced := []byte(configMatchRegex.ReplaceAllString(cfr.Value, string(match)))
return replaced, cfr.ValueType, nil
} }
} }

View File

@ -14,7 +14,6 @@ import (
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
"io/ioutil" "io/ioutil"
"os" "os"
"regexp"
"strings" "strings"
) )
@ -28,15 +27,6 @@ const (
Xml = "xml" Xml = "xml"
) )
// Regex to match anything that has a value matching the format of {{ config.$1 }} which
// will cause the program to lookup that configuration value from itself and set that
// value to the configuration one.
//
// This allows configurations to reference values that are node dependent, such as the
// internal IP address used by the daemon, useful in Bungeecord setups for example, where
// it is common to see variables such as "{{config.docker.interface}}"
var configMatchRegex = regexp.MustCompile(`^{{\s?config\.([\w.-]+)\s?}}$`)
type ConfigurationParser string type ConfigurationParser string
// Defines a configuration file for the server startup. These will be looped over // Defines a configuration file for the server startup. These will be looped over