From 39f98eefe1c06af437eedef05706297f57bae149 Mon Sep 17 00:00:00 2001 From: Jakub Buczak Date: Wed, 11 May 2022 15:06:43 +0000 Subject: [PATCH] Improve code according to mjpitz suggestions Replace `# -- @ignore` with simple `@ignore` --- README.md | 4 +-- .../ignored-values-example/values.yaml | 12 ++++----- pkg/helm/chart_info.go | 26 ++++++++----------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 3123037..8fde04e 100644 --- a/README.md +++ b/README.md @@ -322,10 +322,10 @@ the description for the field. The `@default` comment must follow. See [here](./example-charts/custom-template/values.yaml) for an example. ### Ignoring values -In cases you would like to ignore certain values, you can mark it as ignored: +In cases you would like to ignore certain values, you can mark it with @ignored tag: ```yaml -# -- @ignored +# @ignored service: port: 8080 ``` diff --git a/example-charts/ignored-values-example/values.yaml b/example-charts/ignored-values-example/values.yaml index 199123f..8e376d9 100644 --- a/example-charts/ignored-values-example/values.yaml +++ b/example-charts/ignored-values-example/values.yaml @@ -32,20 +32,20 @@ config: # -- user with access to the database with the same name - {name: hashbash, readwriteDatabases: [hashbash]} - # -- @ignore test + # @ignore test - {name: test, readDatabases: [test]} -# -- @ignore +# @ignore internalConfig: rpcPort: 8080 # -- this should also be ignored generateData: true -# -- @ignore -ignoredConfig: 5 +# @ignore +ignoredConfig: 6 configWithAllValuesIgnored: - # -- @ignore + # @ignore ignoredOne: true - # -- @ignore + # @ignore ignoredTwo: "?" diff --git a/pkg/helm/chart_info.go b/pkg/helm/chart_info.go index 4df10a7..acbea3a 100644 --- a/pkg/helm/chart_info.go +++ b/pkg/helm/chart_info.go @@ -146,23 +146,19 @@ func parseChartRequirementsFile(chartDirectory string, apiVersion string) (Chart return chartRequirements, nil } -func removeIgnored(rootNode *yaml.Node, rootKind yaml.Kind) { - var toDelete []int - for i, node := range rootNode.Content { - if strings.Contains(node.HeadComment, "# -- @ignore") { - toDelete = append(toDelete, i) - if rootKind == yaml.MappingNode { - toDelete = append(toDelete, i+1) - } +func removeIgnored(rootNode *yaml.Node, parentKind yaml.Kind) { + newContent := make([]*yaml.Node , 0, len(rootNode.Content)) + for i := 0; i < len(rootNode.Content); i++ { + node := rootNode.Content[i] + if !strings.Contains(node.HeadComment, "@ignore") { + removeIgnored(node, node.Kind) + newContent = append(newContent, node) + } else if parentKind == yaml.MappingNode { + // for parentKind each yaml key is represented by two nodes + i++ } } - for i := len(toDelete) - 1; i >= 0; i-- { - var d int = toDelete[i] - rootNode.Content = append(rootNode.Content[:d], rootNode.Content[d+1:]...) - } - for _, node := range rootNode.Content { - removeIgnored(node, node.Kind) - } + rootNode.Content = newContent } func parseChartValuesFile(chartDirectory string) (yaml.Node, error) {