Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to add dependency values as subsection #144

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/helm-docs/command_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
37 changes: 33 additions & 4 deletions pkg/document/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
12 changes: 11 additions & 1 deletion pkg/document/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" . }}`)
Expand Down