diff --git a/Gopkg.lock b/Gopkg.lock index 23ae655cae3..9632ab80c49 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -460,6 +460,14 @@ revision = "5c8c8bd35d3832f5d134ae1e1e375b69a4d25242" version = "v1.0.1" +[[projects]] + digest = "1:27885e80e300ed44b6ccaa01b4db40e155e374ed12410ac1aa8f1fe4cc69b2bf" + name = "github.com/krishicks/yaml-patch" + packages = ["."] + pruneopts = "NUT" + revision = "83cc9ac50becbbfafb86a89167f3bc5372e8e530" + version = "v0.0.10" + [[projects]] digest = "1:08c231ec84231a7e23d67e4b58f975e1423695a32467a362ee55a803f9de8061" name = "github.com/mattn/go-colorable" @@ -1200,6 +1208,7 @@ "github.com/google/go-containerregistry/pkg/v1/tarball", "github.com/google/go-github/github", "github.com/karrick/godirwalk", + "github.com/krishicks/yaml-patch", "github.com/mitchellh/go-homedir", "github.com/moby/buildkit/frontend/dockerfile/command", "github.com/moby/buildkit/frontend/dockerfile/parser", diff --git a/vendor/github.com/krishicks/yaml-patch/LICENSE b/vendor/github.com/krishicks/yaml-patch/LICENSE new file mode 100644 index 00000000000..8dada3edaf5 --- /dev/null +++ b/vendor/github.com/krishicks/yaml-patch/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/krishicks/yaml-patch/container.go b/vendor/github.com/krishicks/yaml-patch/container.go new file mode 100644 index 00000000000..bdc22f143de --- /dev/null +++ b/vendor/github.com/krishicks/yaml-patch/container.go @@ -0,0 +1,167 @@ +package yamlpatch + +import ( + "fmt" + "strconv" + "strings" +) + +// Container is the interface for performing operations on Nodes +type Container interface { + Get(key string) (*Node, error) + Set(key string, val *Node) error + Add(key string, val *Node) error + Remove(key string) error +} + +type nodeMap map[interface{}]*Node + +func (n *nodeMap) Set(key string, val *Node) error { + (*n)[key] = val + return nil +} + +func (n *nodeMap) Add(key string, val *Node) error { + (*n)[key] = val + return nil +} + +func (n *nodeMap) Get(key string) (*Node, error) { + return (*n)[key], nil +} + +func (n *nodeMap) Remove(key string) error { + _, ok := (*n)[key] + if !ok { + return fmt.Errorf("Unable to remove nonexistent key: %s", key) + } + + delete(*n, key) + return nil +} + +type nodeSlice []*Node + +func (n *nodeSlice) Set(index string, val *Node) error { + i, err := strconv.Atoi(index) + if err != nil { + return err + } + + sz := len(*n) + if i+1 > sz { + sz = i + 1 + } + + ary := make([]*Node, sz) + + cur := *n + + copy(ary, cur) + + if i >= len(ary) { + return fmt.Errorf("Unable to access invalid index: %d", i) + } + + ary[i] = val + + *n = ary + return nil +} + +func (n *nodeSlice) Add(index string, val *Node) error { + if index == "-" { + *n = append(*n, val) + return nil + } + + i, err := strconv.Atoi(index) + if err != nil { + return err + } + + ary := make([]*Node, len(*n)+1) + + cur := *n + + copy(ary[0:i], cur[0:i]) + ary[i] = val + copy(ary[i+1:], cur[i:]) + + *n = ary + return nil +} + +func (n *nodeSlice) Get(index string) (*Node, error) { + i, err := strconv.Atoi(index) + if err != nil { + return nil, err + } + + if i >= 0 && i <= len(*n)-1 { + return (*n)[i], nil + } + + return nil, fmt.Errorf("Unable to access invalid index: %d", i) +} + +func (n *nodeSlice) Remove(index string) error { + i, err := strconv.Atoi(index) + if err != nil { + return err + } + + cur := *n + + if i >= len(cur) { + return fmt.Errorf("Unable to remove invalid index: %d", i) + } + + ary := make([]*Node, len(cur)-1) + + copy(ary[0:i], cur[0:i]) + copy(ary[i:], cur[i+1:]) + + *n = ary + return nil + +} + +func findContainer(c Container, path *OpPath) (Container, string, error) { + parts, key, err := path.Decompose() + if err != nil { + return nil, "", err + } + + foundContainer := c + + for _, part := range parts { + node, err := foundContainer.Get(decodePatchKey(part)) + if err != nil { + return nil, "", err + } + + if node == nil { + return nil, "", fmt.Errorf("path does not exist: %s", path) + } + + foundContainer = node.Container() + } + + return foundContainer, decodePatchKey(key), nil +} + +// From http://tools.ietf.org/html/rfc6901#section-4 : +// +// Evaluation of each reference token begins by decoding any escaped +// character sequence. This is performed by first transforming any +// occurrence of the sequence '~1' to '/', and then transforming any +// occurrence of the sequence '~0' to '~'. + +var ( + rfc6901Decoder = strings.NewReplacer("~1", "/", "~0", "~") +) + +func decodePatchKey(k string) string { + return rfc6901Decoder.Replace(k) +} diff --git a/vendor/github.com/krishicks/yaml-patch/node.go b/vendor/github.com/krishicks/yaml-patch/node.go new file mode 100644 index 00000000000..4837c8a983e --- /dev/null +++ b/vendor/github.com/krishicks/yaml-patch/node.go @@ -0,0 +1,83 @@ +package yamlpatch + +import "reflect" + +// Node holds a YAML document that has not yet been processed into a NodeMap or +// NodeSlice +type Node struct { + raw *interface{} + container Container +} + +// NewNode returns a new Node. It expects a pointer to an interface{} +func NewNode(raw *interface{}) *Node { + return &Node{ + raw: raw, + } +} + +// MarshalYAML implements yaml.Marshaler, and returns the correct interface{} +// to be marshaled +func (n *Node) MarshalYAML() (interface{}, error) { + if n.container != nil { + return n.container, nil + } + + return *n.raw, nil +} + +// UnmarshalYAML implements yaml.Unmarshaler +func (n *Node) UnmarshalYAML(unmarshal func(interface{}) error) error { + var data interface{} + + err := unmarshal(&data) + if err != nil { + return err + } + + n.raw = &data + return nil +} + +// Empty returns whether the raw value is nil +func (n *Node) Empty() bool { + return *n.raw == nil +} + +// Container returns the node as a Container +func (n *Node) Container() Container { + if n.container != nil { + return n.container + } + + switch rt := (*n.raw).(type) { + case []interface{}: + c := make(nodeSlice, len(rt)) + n.container = &c + + for i := range rt { + c[i] = NewNode(&rt[i]) + } + case map[interface{}]interface{}: + c := make(nodeMap, len(rt)) + n.container = &c + + for k := range rt { + v := rt[k] + c[k] = NewNode(&v) + } + } + + return n.container +} + +// Equal compares the values of the raw interfaces that the YAML was +// unmarshaled into +func (n *Node) Equal(other *Node) bool { + return reflect.DeepEqual(*n.raw, *other.raw) +} + +// Value returns the raw value of the node +func (n *Node) Value() interface{} { + return *n.raw +} diff --git a/vendor/github.com/krishicks/yaml-patch/operation.go b/vendor/github.com/krishicks/yaml-patch/operation.go new file mode 100644 index 00000000000..69353c77d6d --- /dev/null +++ b/vendor/github.com/krishicks/yaml-patch/operation.go @@ -0,0 +1,181 @@ +package yamlpatch + +import ( + "errors" + "fmt" + "strings" +) + +// Op is a type alias +type Op string + +// Ops +const ( + opAdd Op = "add" + opRemove Op = "remove" + opReplace Op = "replace" + opMove Op = "move" + opCopy Op = "copy" + opTest Op = "test" +) + +// OpPath is an RFC6902 'pointer' +type OpPath string + +// Decompose returns the pointer's components: +// "/foo" => [], "foo" +// "/foo/1" => ["foo"], "1" +// "/foo/1/bar" => ["foo", "1"], "bar" +func (p *OpPath) Decompose() ([]string, string, error) { + path := string(*p) + + if !strings.HasPrefix(path, "/") { + return nil, "", fmt.Errorf("operation path is missing leading '/': %s", path) + } + + parts := strings.Split(path, "/")[1:] + + return parts[:len(parts)-1], parts[len(parts)-1], nil +} + +// ContainsExtendedSyntax returns whether the OpPath uses the "key=value" +// format, as in "/foo/name=bar", where /foo points at an array that contains +// an object with a key "name" that has a value "bar" +func (p *OpPath) ContainsExtendedSyntax() bool { + return strings.Contains(string(*p), "=") +} + +// String returns the OpPath as a string +func (p *OpPath) String() string { + return string(*p) +} + +// Operation is an RFC6902 'Operation' +// https://tools.ietf.org/html/rfc6902#section-4 +type Operation struct { + Op Op `yaml:"op,omitempty"` + Path OpPath `yaml:"path,omitempty"` + From OpPath `yaml:"from,omitempty"` + Value *Node `yaml:"value,omitempty"` +} + +// Perform executes the operation on the given container +func (o *Operation) Perform(c Container) error { + var err error + + switch o.Op { + case opAdd: + err = tryAdd(c, o) + case opRemove: + err = tryRemove(c, o) + case opReplace: + err = tryReplace(c, o) + case opMove: + err = tryMove(c, o) + case opCopy: + err = tryCopy(c, o) + case opTest: + err = tryTest(c, o) + default: + err = fmt.Errorf("Unexpected op: %s", o.Op) + } + + return err +} + +func tryAdd(doc Container, op *Operation) error { + con, key, err := findContainer(doc, &op.Path) + if err != nil { + return fmt.Errorf("yamlpatch add operation does not apply: doc is missing path: %s", op.Path) + } + + return con.Add(key, op.Value) +} + +func tryRemove(doc Container, op *Operation) error { + con, key, err := findContainer(doc, &op.Path) + if err != nil { + return fmt.Errorf("yamlpatch remove operation does not apply: doc is missing path: %s", op.Path) + } + + return con.Remove(key) +} + +func tryReplace(doc Container, op *Operation) error { + con, key, err := findContainer(doc, &op.Path) + if err != nil { + return fmt.Errorf("yamlpatch replace operation does not apply: doc is missing path: %s", op.Path) + } + + val, err := con.Get(key) + if val == nil || err != nil { + return fmt.Errorf("yamlpatch replace operation does not apply: doc is missing key: %s", op.Path) + } + + return con.Set(key, op.Value) +} + +func tryMove(doc Container, op *Operation) error { + con, key, err := findContainer(doc, &op.From) + if err != nil { + return fmt.Errorf("yamlpatch move operation does not apply: doc is missing from path: %s", op.From) + } + + val, err := con.Get(key) + if err != nil { + return err + } + + err = con.Remove(key) + if err != nil { + return err + } + + con, key, err = findContainer(doc, &op.Path) + if err != nil { + return fmt.Errorf("yamlpatch move operation does not apply: doc is missing destination path: %s", op.Path) + } + + return con.Set(key, val) +} + +func tryCopy(doc Container, op *Operation) error { + con, key, err := findContainer(doc, &op.From) + if err != nil { + return fmt.Errorf("copy operation does not apply: doc is missing from path: %s", op.From) + } + + val, err := con.Get(key) + if err != nil { + return err + } + + con, key, err = findContainer(doc, &op.Path) + if err != nil { + return fmt.Errorf("copy operation does not apply: doc is missing destination path: %s", op.Path) + } + + return con.Set(key, val) +} + +func tryTest(doc Container, op *Operation) error { + con, key, err := findContainer(doc, &op.Path) + if err != nil { + return fmt.Errorf("test operation does not apply: doc is missing from path: %s", op.From) + } + + val, err := con.Get(key) + if err != nil { + return err + } + + if op.Value.Empty() && val == nil { + return nil + } + + if op.Value.Equal(val) { + return nil + } + + return errors.New("test failed") +} diff --git a/vendor/github.com/krishicks/yaml-patch/patch.go b/vendor/github.com/krishicks/yaml-patch/patch.go new file mode 100644 index 00000000000..910f39eb952 --- /dev/null +++ b/vendor/github.com/krishicks/yaml-patch/patch.go @@ -0,0 +1,60 @@ +package yamlpatch + +import ( + "fmt" + + yaml "gopkg.in/yaml.v2" +) + +// Patch is an ordered collection of operations. +type Patch []Operation + +// DecodePatch decodes the passed YAML document as if it were an RFC 6902 patch +func DecodePatch(bs []byte) (Patch, error) { + var p Patch + + err := yaml.Unmarshal(bs, &p) + if err != nil { + return nil, err + } + + return p, nil +} + +// Apply returns a YAML document that has been mutated per the patch +func (p Patch) Apply(doc []byte) ([]byte, error) { + var iface interface{} + err := yaml.Unmarshal(doc, &iface) + if err != nil { + return nil, fmt.Errorf("failed unmarshaling doc: %s\n\n%s", string(doc), err) + } + + var c Container + c = NewNode(&iface).Container() + + for _, op := range p { + pathfinder := NewPathFinder(c) + if op.Path.ContainsExtendedSyntax() { + paths := pathfinder.Find(string(op.Path)) + if paths == nil { + return nil, fmt.Errorf("could not expand pointer: %s", op.Path) + } + + for _, path := range paths { + newOp := op + newOp.Path = OpPath(path) + err = newOp.Perform(c) + if err != nil { + return nil, err + } + } + } else { + err = op.Perform(c) + if err != nil { + return nil, err + } + } + } + + return yaml.Marshal(c) +} diff --git a/vendor/github.com/krishicks/yaml-patch/pathfinder.go b/vendor/github.com/krishicks/yaml-patch/pathfinder.go new file mode 100644 index 00000000000..06cfb133347 --- /dev/null +++ b/vendor/github.com/krishicks/yaml-patch/pathfinder.go @@ -0,0 +1,109 @@ +package yamlpatch + +import ( + "fmt" + "strings" +) + +// PathFinder can be used to find RFC6902-standard paths given non-standard +// (key=value) pointer syntax +type PathFinder struct { + root Container +} + +// NewPathFinder takes an interface that represents a YAML document and returns +// a new PathFinder +func NewPathFinder(container Container) *PathFinder { + return &PathFinder{ + root: container, + } +} + +// Find expands the given path into all matching paths, returning the canonical +// versions of those matching paths +func (p *PathFinder) Find(path string) []string { + parts := strings.Split(path, "/") + + if parts[1] == "" { + return []string{"/"} + } + + routes := map[string]Container{ + "": p.root, + } + + for _, part := range parts[1:] { + routes = find(decodePatchKey(part), routes) + } + + var paths []string + for k := range routes { + paths = append(paths, k) + } + + return paths +} + +func find(part string, routes map[string]Container) map[string]Container { + matches := map[string]Container{} + + for prefix, container := range routes { + if part == "-" { + for k := range routes { + matches[fmt.Sprintf("%s/-", k)] = routes[k] + } + return matches + } + + if kv := strings.Split(part, "="); len(kv) == 2 { + if newMatches := findAll(prefix, kv[0], kv[1], container); len(newMatches) > 0 { + matches = newMatches + } + continue + } + + if node, err := container.Get(part); err == nil { + path := fmt.Sprintf("%s/%s", prefix, part) + if node == nil { + matches[path] = container + } else { + matches[path] = node.Container() + } + } + } + + return matches +} + +func findAll(prefix, findKey, findValue string, container Container) map[string]Container { + if container == nil { + return nil + } + + if v, err := container.Get(findKey); err == nil && v != nil { + if vs, ok := v.Value().(string); ok && vs == findValue { + return map[string]Container{ + prefix: container, + } + } + } + + matches := map[string]Container{} + + switch it := container.(type) { + case *nodeMap: + for k, v := range *it { + for route, match := range findAll(fmt.Sprintf("%s/%s", prefix, k), findKey, findValue, v.Container()) { + matches[route] = match + } + } + case *nodeSlice: + for i, v := range *it { + for route, match := range findAll(fmt.Sprintf("%s/%d", prefix, i), findKey, findValue, v.Container()) { + matches[route] = match + } + } + } + + return matches +} diff --git a/vendor/github.com/krishicks/yaml-patch/placeholder_wrapper.go b/vendor/github.com/krishicks/yaml-patch/placeholder_wrapper.go new file mode 100644 index 00000000000..cbcc22a8420 --- /dev/null +++ b/vendor/github.com/krishicks/yaml-patch/placeholder_wrapper.go @@ -0,0 +1,51 @@ +package yamlpatch + +import ( + "fmt" + "regexp" +) + +// PlaceholderWrapper can be used to wrap placeholders that make YAML invalid +// in single quotes to make otherwise valid YAML +type PlaceholderWrapper struct { + LeftSide string + RightSide string + unwrappedRegex *regexp.Regexp + wrappedRegex *regexp.Regexp +} + +// NewPlaceholderWrapper returns a new PlaceholderWrapper which knows how to +// wrap and unwrap the provided left and right sides of a placeholder, e.g. {{ +// and }} +func NewPlaceholderWrapper(left, right string) *PlaceholderWrapper { + escapedLeft := regexp.QuoteMeta(left) + escapedRight := regexp.QuoteMeta(right) + unwrappedRegex := regexp.MustCompile(`\s` + escapedLeft + `([^` + escapedRight + `]+)` + escapedRight) + wrappedRegex := regexp.MustCompile(`\s'` + escapedLeft + `([^` + escapedRight + `]+)` + escapedRight + `'`) + + return &PlaceholderWrapper{ + LeftSide: left, + RightSide: right, + unwrappedRegex: unwrappedRegex, + wrappedRegex: wrappedRegex, + } +} + +// Wrap the placeholder in single quotes to make it valid YAML +func (w *PlaceholderWrapper) Wrap(input []byte) []byte { + if !w.unwrappedRegex.Match(input) { + return input + } + + return w.unwrappedRegex.ReplaceAll(input, []byte(fmt.Sprintf(` '%s$1%s'`, w.LeftSide, w.RightSide))) +} + +// Unwrap the single quotes from the placeholder to make it invalid YAML +// (again) +func (w *PlaceholderWrapper) Unwrap(input []byte) []byte { + if !w.wrappedRegex.Match(input) { + return input + } + + return w.wrappedRegex.ReplaceAll(input, []byte(fmt.Sprintf(` %s$1%s`, w.LeftSide, w.RightSide))) +}