From 9cbd7fe69edfd8854735da1cd443420f80173cff Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 8 Jun 2023 09:55:23 +0200 Subject: [PATCH] Extract a lookupKey function that will be useful in the next commit --- pkg/utils/yaml_utils/yaml_utils.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/utils/yaml_utils/yaml_utils.go b/pkg/utils/yaml_utils/yaml_utils.go index 81bc11fca..f38a47264 100644 --- a/pkg/utils/yaml_utils/yaml_utils.go +++ b/pkg/utils/yaml_utils/yaml_utils.go @@ -57,10 +57,8 @@ func updateYamlNode(node *yaml.Node, path []string, value string) error { } key := path[0] - for i := 0; i < len(node.Content)-1; i += 2 { - if node.Content[i].Value == key { - return updateYamlNode(node.Content[i+1], path[1:], value) - } + if _, valueNode := lookupKey(node, key); valueNode != nil { + return updateYamlNode(valueNode, path[1:], value) } // if the key doesn't exist, we'll add it @@ -87,3 +85,13 @@ func updateYamlNode(node *yaml.Node, path []string, value string) error { }, newNode) return updateYamlNode(newNode, path[1:], value) } + +func lookupKey(node *yaml.Node, key string) (*yaml.Node, *yaml.Node) { + for i := 0; i < len(node.Content)-1; i += 2 { + if node.Content[i].Value == key { + return node.Content[i], node.Content[i+1] + } + } + + return nil, nil +}