Skip to content

Commit

Permalink
Sectioned values are default if provied
Browse files Browse the repository at this point in the history
  • Loading branch information
Haepaxlog committed Nov 8, 2023
1 parent 7d54b35 commit d1a32aa
Showing 1 changed file with 51 additions and 64 deletions.
115 changes: 51 additions & 64 deletions pkg/document/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,33 @@ func getValuesTableTemplates() string {
valuesSectionBuilder.WriteString(`{{ define "chart.valuesHeader" }}## Values{{ end }}`)

valuesSectionBuilder.WriteString(`{{ define "chart.valuesTable" }}`)
valuesSectionBuilder.WriteString("{{ if .Sections.Sections }}")
valuesSectionBuilder.WriteString("{{ range .Sections.Sections }}")
valuesSectionBuilder.WriteString("\n")
valuesSectionBuilder.WriteString("\n### {{ .SectionName }}\n")
valuesSectionBuilder.WriteString("| Key | Type | Default | Description |\n")
valuesSectionBuilder.WriteString("|-----|------|---------|-------------|\n")
valuesSectionBuilder.WriteString(" {{- range .SectionItems }}")
valuesSectionBuilder.WriteString("\n| {{ .Key }} | {{ .Type }} | {{ if .Default }}{{ .Default }}{{ else }}{{ .AutoDefault }}{{ end }} | {{ if .Description }}{{ .Description }}{{ else }}{{ .AutoDescription }}{{ end }} |")
valuesSectionBuilder.WriteString(" {{- end }}")
valuesSectionBuilder.WriteString("{{- end }}")
valuesSectionBuilder.WriteString("{{ if .Sections.DefaultSection.SectionItems}}")
valuesSectionBuilder.WriteString("\n")
valuesSectionBuilder.WriteString("\n### {{ .Sections.DefaultSection.SectionName }}\n")
valuesSectionBuilder.WriteString("| Key | Type | Default | Description |\n")
valuesSectionBuilder.WriteString("|-----|------|---------|-------------|\n")
valuesSectionBuilder.WriteString(" {{- range .Sections.DefaultSection.SectionItems }}")
valuesSectionBuilder.WriteString("\n| {{ .Key }} | {{ .Type }} | {{ if .Default }}{{ .Default }}{{ else }}{{ .AutoDefault }}{{ end }} | {{ if .Description }}{{ .Description }}{{ else }}{{ .AutoDescription }}{{ end }} |")
valuesSectionBuilder.WriteString(" {{- end }}")
valuesSectionBuilder.WriteString("{{ end }}")
valuesSectionBuilder.WriteString("{{ else }}")
valuesSectionBuilder.WriteString("| Key | Type | Default | Description |\n")
valuesSectionBuilder.WriteString("|-----|------|---------|-------------|\n")
valuesSectionBuilder.WriteString(" {{- range .Values }}")
valuesSectionBuilder.WriteString("\n| {{ .Key }} | {{ .Type }} | {{ if .Default }}{{ .Default }}{{ else }}{{ .AutoDefault }}{{ end }} | {{ if .Description }}{{ .Description }}{{ else }}{{ .AutoDescription }}{{ end }} |")
valuesSectionBuilder.WriteString(" {{- end }}")
valuesSectionBuilder.WriteString("{{ end }}")
valuesSectionBuilder.WriteString("{{ end }}")

valuesSectionBuilder.WriteString(`{{ define "chart.valuesSection" }}`)
valuesSectionBuilder.WriteString("{{ if .Values }}")
Expand Down Expand Up @@ -243,6 +264,9 @@ func getValuesTableTemplates() string {
{{ end }}
{{ define "chart.valuesTableHtml" }}
{{ if .Sections.Sections }}
{{- range .Sections.Sections }}
<h3>{{- .SectionName }}</h3>
<table>
<thead>
<th>Key</th>
Expand All @@ -251,7 +275,7 @@ func getValuesTableTemplates() string {
<th>Description</th>
</thead>
<tbody>
{{- range .Values }}
{{- range .SectionItems }}
<tr>
<td>{{ .Key }}</td>
<td>{{ .Type }}</td>
Expand All @@ -261,65 +285,29 @@ func getValuesTableTemplates() string {
{{- end }}
</tbody>
</table>
{{ end }}
{{ define "chart.valuesSectionHtml" }}
{{ if .Values }}
{{ template "chart.valuesHeader" . }}
{{ template "chart.valuesTableHtml" . }}
{{ end }}
{{ end }}
`)

return valuesSectionBuilder.String()
}

func getValuesTableSectionedTemplates() string {
valuesSectionBuilder := strings.Builder{}
valuesSectionBuilder.WriteString(`{{ define "chart.valuesSectionedHeader" }}## Values{{ end }}`)

valuesSectionBuilder.WriteString(`{{ define "chart.valuesSectionedTable" }}`)
valuesSectionBuilder.WriteString("{{ range .Sections }}")
valuesSectionBuilder.WriteString("\n")
valuesSectionBuilder.WriteString("\n### {{ .SectionName }}\n")
valuesSectionBuilder.WriteString("| Key | Type | Default | Description |\n")
valuesSectionBuilder.WriteString("|-----|------|---------|-------------|\n")
valuesSectionBuilder.WriteString(" {{- range .SectionItems }}")
valuesSectionBuilder.WriteString("\n| {{ .Key }} | {{ .Type }} | {{ if .Default }}{{ .Default }}{{ else }}{{ .AutoDefault }}{{ end }} | {{ if .Description }}{{ .Description }}{{ else }}{{ .AutoDescription }}{{ end }} |")
valuesSectionBuilder.WriteString(" {{- end }}")
valuesSectionBuilder.WriteString("{{ end }}")
valuesSectionBuilder.WriteString("{{ end }}")

valuesSectionBuilder.WriteString(`{{ define "chart.valuesSectionedSection" }}`)
valuesSectionBuilder.WriteString("{{ if .Values }}")
valuesSectionBuilder.WriteString(`{{ template "chart.valuesSectionedHeader" . }}`)
valuesSectionBuilder.WriteString("\n\n")
valuesSectionBuilder.WriteString(`{{ template "chart.valuesSectionedTable" . }}`)
valuesSectionBuilder.WriteString("{{ end }}")
valuesSectionBuilder.WriteString("{{ end }}")

// For HTML tables
valuesSectionBuilder.WriteString(`
{{ define "chart.valueDefaultColumnRender" }}
{{- $defaultValue := (default .Default .AutoDefault) -}}
{{- $notationType := .NotationType }}
{{- if (and (hasPrefix "` + "`" + `" $defaultValue) (hasSuffix "` + "`" + `" $defaultValue) ) -}}
{{- $defaultValue = (toPrettyJson (fromJson (trimAll "` + "`" + `" (default .Default .AutoDefault) ) ) ) -}}
{{- $notationType = "json" }}
{{- end -}}
<pre lang="{{ $notationType }}">
{{- if (eq $notationType "tpl" ) }}
{{ .Key }}: |
{{- $defaultValue | nindent 2 }}
{{- else }}
{{ $defaultValue }}
{{- end }}
</pre>
{{ if .Sections.DefaultSection.SectionItems }}
<h3>{{- .Sections.DefaultSection.SectionName }}</h3>
<table>
<thead>
<th>Key</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</thead>
<tbody>
{{- range .Sections.DefaultSection.SectionItems }}
<tr>
<td>{{ .Key }}</td>
<td>{{ .Type }}</td>
<td>{{ template "chart.valueDefaultColumnRender" . }}</td>
<td>{{ if .Description }}{{ .Description }}{{ else }}{{ .AutoDescription }}{{ end }}</td>
</tr>
{{- end }}
</tbody>
</table>
{{ end }}
{{ define "chart.valuesSectionedTableHtml" }}
{{- range .Sections }}
<h3>{{- .SectionName }}</h3>
{{ else }}
<table>
<thead>
<th>Key</th>
Expand All @@ -328,7 +316,7 @@ func getValuesTableSectionedTemplates() string {
<th>Description</th>
</thead>
<tbody>
{{- range .SectionItems }}
{{- range .Values }}
<tr>
<td>{{ .Key }}</td>
<td>{{ .Type }}</td>
Expand All @@ -338,13 +326,13 @@ func getValuesTableSectionedTemplates() string {
{{- end }}
</tbody>
</table>
{{- end }}
{{ end }}
{{ end }}
{{ define "chart.valuesSectionedSectionHtml" }}
{{ define "chart.valuesSectionHtml" }}
{{ if .Sections }}
{{ template "chart.valuesSectionedHeader" . }}
{{ template "chart.valuesSectionedTableHtml" . }}
{{ template "chart.valuesHeader" . }}
{{ template "chart.valuesTableHtml" . }}
{{ end }}
{{ end }}
`)
Expand Down Expand Up @@ -428,7 +416,6 @@ func getDocumentationTemplates(chartDirectory string, chartSearchRoot string, te
getSourceLinkTemplates(),
getRequirementsTableTemplates(),
getValuesTableTemplates(),
getValuesTableSectionedTemplates(),
getHomepageTemplate(),
getMaintainersTemplate(),
getHelmDocsVersionTemplates(),
Expand Down

0 comments on commit d1a32aa

Please sign in to comment.