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..1d7494b 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" . }}`)