From b7aa3a1fcfde71b2af9ae60162ed73d341e44132 Mon Sep 17 00:00:00 2001 From: Jakub Buczak Date: Wed, 11 May 2022 15:21:03 +0000 Subject: [PATCH 1/3] Add option to add dependency values as subsection --- cmd/helm-docs/command_line.go | 1 + pkg/document/model.go | 37 +++++++++++++++++++++++++++++++---- pkg/document/template.go | 12 +++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cmd/helm-docs/command_line.go b/cmd/helm-docs/command_line.go index 087c615..b262e60 100644 --- a/cmd/helm-docs/command_line.go +++ b/cmd/helm-docs/command_line.go @@ -54,6 +54,7 @@ func newHelmDocsCommand(run func(cmd *cobra.Command, args []string)) (*cobra.Com command.PersistentFlags().StringP("badge-style", "b", "flat-square", "badge style to use for charts") command.PersistentFlags().StringP("values-file", "f", "values.yaml", "Path to values file") command.PersistentFlags().BoolP("document-dependency-values", "u", false, "For charts with dependencies, include the dependency values in the chart values documentation") + command.PersistentFlags().BoolP("dependency-values-as-sections", "a", true, "Put dependency values into separate sections") viper.AutomaticEnv() viper.SetEnvPrefix("HELM_DOCS") diff --git a/pkg/document/model.go b/pkg/document/model.go index 763dd8b..d169c71 100644 --- a/pkg/document/model.go +++ b/pkg/document/model.go @@ -26,10 +26,17 @@ type valueRow struct { IsGlobal bool } +type chartValuesSubsection struct { + Title string + TitleLevel string + Values []valueRow +} + type chartTemplateData struct { helm.ChartDocumentationInfo - HelmDocsVersion string - Values []valueRow + HelmDocsVersion string + Values []valueRow + ValuesSubsections []chartValuesSubsection } func sortValueRows(valueRows []valueRow) { @@ -92,6 +99,8 @@ func getUnsortedValueRows(document *yaml.Node, descriptions map[string]helm.Char } func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion string, dependencyValues []DependencyValues) (chartTemplateData, error) { + dependencySectionsEnabled := viper.GetBool("dependency-values-as-sections") + var valuesSubsections []chartValuesSubsection valuesTableRows, err := getUnsortedValueRows(info.ChartValues, info.ChartValuesDescriptions) if err != nil { return chartTemplateData{}, err @@ -112,9 +121,11 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri return chartTemplateData{}, err } - for _, row := range depValuesTableRows { + for i := len(depValuesTableRows) -1; i >= 0; i-- { + var row = depValuesTableRows[i] if row.Key == "global" || strings.HasPrefix(row.Key, "global.") { if seenGlobalKeys[row.Key] { + depValuesTableRows = append(depValuesTableRows[:i], depValuesTableRows[i+1:]...) continue } row.IsGlobal = true @@ -124,16 +135,34 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri } row.Dependency = dep.Prefix - valuesTableRows = append(valuesTableRows, row) } + if dependencySectionsEnabled && len(depValuesTableRows) > 0 { + valuesSubsections = append(valuesSubsections, chartValuesSubsection{ + Title: dep.Prefix, + TitleLevel : "###", + Values: depValuesTableRows, + }) + } else { + valuesTableRows = append(valuesTableRows, depValuesTableRows...) + } + } } sortValueRows(valuesTableRows) + if dependencySectionsEnabled && len(valuesTableRows) > 0 { + var section = chartValuesSubsection{ + Title: "", + TitleLevel: "", + Values: valuesTableRows, + } + valuesSubsections = append( []chartValuesSubsection{section}, valuesSubsections...) + } return chartTemplateData{ ChartDocumentationInfo: info, HelmDocsVersion: helmDocsVersion, Values: valuesTableRows, + ValuesSubsections: valuesSubsections, }, nil } diff --git a/pkg/document/template.go b/pkg/document/template.go index ee0dbd9..ac18e04 100644 --- a/pkg/document/template.go +++ b/pkg/document/template.go @@ -216,7 +216,17 @@ func getValuesTableTemplates() string { valuesSectionBuilder.WriteString("{{ end }}") valuesSectionBuilder.WriteString(`{{ define "chart.valuesSection" }}`) - valuesSectionBuilder.WriteString("{{ if .Values }}") + valuesSectionBuilder.WriteString(` +{{- if .ValuesSubsections }} +{{ template "chart.valuesHeader" . }} + +{{ range $val := .ValuesSubsections }} +{{ $val.TitleLevel }} {{ $val.Title }} + +{{ template "chart.valuesTable" $val }} +{{ end }} +`) + valuesSectionBuilder.WriteString("{{ else if .Values }}") valuesSectionBuilder.WriteString(`{{ template "chart.valuesHeader" . }}`) valuesSectionBuilder.WriteString("\n\n") valuesSectionBuilder.WriteString(`{{ template "chart.valuesTable" . }}`) From 3ba39986c96ebcfa751c7c5473fb5d70d78d05ea Mon Sep 17 00:00:00 2001 From: Jakub Buczak Date: Wed, 11 May 2022 15:31:23 +0000 Subject: [PATCH 2/3] Fix struct memmbers align --- pkg/document/model.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/document/model.go b/pkg/document/model.go index d169c71..40944be 100644 --- a/pkg/document/model.go +++ b/pkg/document/model.go @@ -27,9 +27,9 @@ type valueRow struct { } type chartValuesSubsection struct { - Title string - TitleLevel string - Values []valueRow + Title string + TitleLevel string + Values []valueRow } type chartTemplateData struct { @@ -163,6 +163,6 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri ChartDocumentationInfo: info, HelmDocsVersion: helmDocsVersion, Values: valuesTableRows, - ValuesSubsections: valuesSubsections, + ValuesSubsections: valuesSubsections, }, nil } From db0686057b7936f2194ba850c4cf0adacf40a0f4 Mon Sep 17 00:00:00 2001 From: Jakub Buczak Date: Thu, 12 May 2022 14:17:01 +0000 Subject: [PATCH 3/3] Add dependency prefix to value key This feature wa broken due to missing pointer --- pkg/document/model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/document/model.go b/pkg/document/model.go index 40944be..1d7494b 100644 --- a/pkg/document/model.go +++ b/pkg/document/model.go @@ -122,7 +122,7 @@ func getChartTemplateData(info helm.ChartDocumentationInfo, helmDocsVersion stri } for i := len(depValuesTableRows) -1; i >= 0; i-- { - var row = depValuesTableRows[i] + var row = &depValuesTableRows[i] if row.Key == "global" || strings.HasPrefix(row.Key, "global.") { if seenGlobalKeys[row.Key] { depValuesTableRows = append(depValuesTableRows[:i], depValuesTableRows[i+1:]...)