Skip to content

Commit

Permalink
Merge pull request #45 from huoyinghao/master
Browse files Browse the repository at this point in the history
feature: support not exists field load
  • Loading branch information
huoyinghao committed Nov 8, 2023
2 parents 537b557 + 8c1849f commit 1cafc62
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func applyUpdate(chart *chart.Chart, a *RewriteAction) error {

newData, err := yamlops2.UpdateMap(data, a.GetPathToMap(), "", nil, value)
if err != nil {
if strings.Contains(err.Error(), "has no field") {
newData, err = yamlops2.InsertMap(data, a.GetPathToMap(), []byte(a.GetKey()), []byte(a.Value))
if err != nil {
return err
}
chart.Raw[valuesIndex].Data = newData
return nil
}
return fmt.Errorf("failed to apply modification to %s: %w", chart.Name(), err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,29 @@ import (
//
// Update a Chart's values.yaml to set a specific image's reference:
//
// UpdateMap(doc, ".images.postgresql", "", nil, map[string]string{
// "registry": "docker.io",
// "repository": "bitnami/postgresql",
// "tag": "11.6.0-debian-10-r5",
// })
// UpdateMap(doc, ".images.postgresql", "", nil, map[string]string{
// "registry": "docker.io",
// "repository": "bitnami/postgresql",
// "tag": "11.6.0-debian-10-r5",
// })
//
// Update a Chart's values.yaml to replace any uses of a general purpose image:
//
// UpdateMap(doc, "", "",map[string]string{
// "repository": "bitnami/minideb",
// }, map[string]string{
// "registry": "custom.images.org",
// "repository": "custom-general-purpose",
// "tag": "1.2.3",
// })
// UpdateMap(doc, "", "",map[string]string{
// "repository": "bitnami/minideb",
// }, map[string]string{
// "registry": "custom.images.org",
// "repository": "custom-general-purpose",
// "tag": "1.2.3",
// })
//
// Update a Chart's dependencies to rewrite a chart registry:
//
// UpdateMap(doc, "", ".dependencies", map[string]string{
// "repository": "https://charts.bitnami.com/bitnami",
// }, map[string]string{
// "repository": "custom.charts.org",
// })
//
// UpdateMap(doc, "", ".dependencies", map[string]string{
// "repository": "https://charts.bitnami.com/bitnami",
// }, map[string]string{
// "repository": "custom.charts.org",
// })
func UpdateMap(doc []byte, pathSpec, selectorFilter string, selectors, values map[string]string) ([]byte, error) {
root, err := parse(doc)
if err != nil {
Expand Down Expand Up @@ -146,6 +145,27 @@ func UpdateMap(doc []byte, pathSpec, selectorFilter string, selectors, values ma
return doc, nil
}

func InsertMap(doc []byte, pathSpec string, key, value []byte) ([]byte, error) {
root, err := parse(doc)
if err != nil {
return nil, err
}
node := SearchNodes(root, ".", NodeHasPath(pathSpec))[pathSpec]
if node == nil {
return nil, fmt.Errorf("test error")
}
lastContent := node.Content[len(node.Content)-1]
// %s: %s\n
lines := make([]byte, node.Column)
lines[0] = '\n'
for i := 1; i < node.Column; i++ {
lines[i] = ' '
}
lines = append(append(append(append(lines, key...), ':'), ' '), value...)
doc = append(doc[:lastContent.IndexEnd], append(lines, doc[lastContent.IndexEnd:]...)...)
return doc, nil
}

// scanNewLine is a copy of `bufio.ScanLines` that does not strip any '\r'.
func scanNewLine(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
Expand Down

0 comments on commit 1cafc62

Please sign in to comment.