github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/testutil/pkgbuilder/fns.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pkgbuilder
    16  
    17  import "sigs.k8s.io/kustomize/kyaml/yaml"
    18  
    19  // SetFieldPath returns a new FieldPathSetter that updates the property
    20  // given by the path with the given value.
    21  func SetFieldPath(value string, path ...string) FieldPathSetter {
    22  	return FieldPathSetter{
    23  		Value: value,
    24  		Path:  path,
    25  	}
    26  }
    27  
    28  // FieldPathSetter updates the value of the field given by the path.
    29  type FieldPathSetter struct {
    30  	Path []string
    31  
    32  	Value string
    33  }
    34  
    35  func (f FieldPathSetter) Filter(rn *yaml.RNode) (*yaml.RNode, error) {
    36  	n, err := rn.Pipe(yaml.PathGetter{
    37  		Path: f.Path,
    38  	})
    39  	if err != nil {
    40  		return rn, err
    41  	}
    42  
    43  	n.YNode().Value = f.Value
    44  	return rn, nil
    45  }
    46  
    47  // SetAnnotation returns a new AnnotationSetters that sets an annotation
    48  // with the given name and value.
    49  func SetAnnotation(name, value string) AnnotationSetter {
    50  	return AnnotationSetter{
    51  		Name:  name,
    52  		Value: value,
    53  	}
    54  }
    55  
    56  type AnnotationSetter struct {
    57  	Name string
    58  
    59  	Value string
    60  }
    61  
    62  func (a AnnotationSetter) Filter(rn *yaml.RNode) (*yaml.RNode, error) {
    63  	err := rn.PipeE(yaml.AnnotationSetter{
    64  		Key:   a.Name,
    65  		Value: a.Value,
    66  	})
    67  	return rn, err
    68  }