Better match handling I think?

This commit is contained in:
Dane Everitt
2020-05-17 18:22:06 -07:00
parent c802a3397e
commit bd063682dc
4 changed files with 29 additions and 11 deletions

View File

@@ -9,7 +9,6 @@ import (
"go.uber.org/zap"
"io/ioutil"
"os"
"reflect"
"regexp"
"strconv"
"strings"
@@ -48,13 +47,14 @@ func readFileBytes(path string) ([]byte, error) {
}
// Gets the value of a key based on the value type defined.
func getKeyValue(value []byte) interface{} {
if reflect.ValueOf(value).Kind() == reflect.Bool {
func (cfr *ConfigurationFileReplacement) getKeyValue(value []byte) interface{} {
if cfr.ReplaceWith.Type() == jsonparser.Boolean {
v, _ := strconv.ParseBool(string(value))
return v
}
// Try to parse into an int, if this fails just ignore the error and
// Try to parse into an int, if this fails just ignore the error and continue
// through, returning the string.
if v, err := strconv.Atoi(string(value)); err == nil {
return v
}
@@ -70,7 +70,9 @@ func getKeyValue(value []byte) interface{} {
// configurations per-world (such as Spigot and Bungeecord) where we'll need to make
// adjustments to the bind address for the user.
//
// This does not currently support nested matches. container.*.foo.*.bar will not work.
// This does not currently support nested wildcard matches. For example, foo.*.bar
// will work, however foo.*.bar.*.baz will not, since we'll only be splitting at the
// first wildcard, and not subsequent ones.
func (f *ConfigurationFile) IterateOverJson(data []byte) (*gabs.Container, error) {
parsed, err := gabs.ParseJSON(data)
if err != nil {
@@ -143,7 +145,7 @@ func (cfr *ConfigurationFileReplacement) SetAtPathway(c *gabs.Container, path st
}
}
_, err := c.SetP(getKeyValue(value), path)
_, err := c.SetP(cfr.getKeyValue(value), path)
return err
}

View File

@@ -5,14 +5,16 @@ import (
"encoding/json"
"github.com/beevik/etree"
"github.com/buger/jsonparser"
"github.com/ghodss/yaml"
"github.com/icza/dyno"
"github.com/magiconair/properties"
"github.com/pkg/errors"
"github.com/pterodactyl/wings/config"
"go.uber.org/zap"
"gopkg.in/ini.v1"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"regexp"
"strings"
)
@@ -73,6 +75,11 @@ func (f *ConfigurationFile) UnmarshalJSON(data []byte) error {
return nil
}
// Regex to match paths such as foo[1].bar[2] and convert them into a format that
// gabs can work with, such as foo.1.bar.2 in this case. This is applied when creating
// the struct for the configuration file replacements.
var cfrMatchReplacement = regexp.MustCompile(`\[(\d+)]`)
// Defines a single find/replace instance for a given server configuration file.
type ConfigurationFileReplacement struct {
Match string `json:"match"`
@@ -87,7 +94,9 @@ func (cfr *ConfigurationFileReplacement) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
cfr.Match = m
// See comment on the replacement regex to understand what exactly this is doing.
cfr.Match = cfrMatchReplacement.ReplaceAllString(m, ".$1")
iv, err := jsonparser.GetString(data, "if_value")
// We only check keypath here since match & replace_with should be present on all of
@@ -130,7 +139,6 @@ func (f *ConfigurationFile) Parse(path string, internal bool) error {
f.configuration = mb
}
var err error
switch f.Parser {
@@ -351,10 +359,15 @@ func (f *ConfigurationFile) parseYamlFile(path string) error {
return err
}
i := make(map[string]interface{})
if err := yaml.Unmarshal(b, &i); err != nil {
return err
}
// Unmarshal the yaml data into a JSON interface such that we can work with
// any arbitrary data structure. If we don't do this, I can't use gabs which
// makes working with unknown JSON signficiantly easier.
jsonBytes, err := yaml.YAMLToJSON(b)
jsonBytes, err := json.Marshal(dyno.ConvertMapI2MapS(i))
if err != nil {
return err
}
@@ -367,7 +380,7 @@ func (f *ConfigurationFile) parseYamlFile(path string) error {
}
// Remarshal the JSON into YAML format before saving it back to the disk.
marshaled, err := yaml.JSONToYAML(data.Bytes())
marshaled, err := yaml.Marshal(data.Data())
if err != nil {
return err
}