Skip to content

Commit

Permalink
Merge pull request #121 from dirien/badge-style
Browse files Browse the repository at this point in the history
feat: make the badge style from shields.io configurable
  • Loading branch information
norwoodj authored Apr 3, 2022
2 parents 76c2280 + 372c61e commit 6754e5a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
1 change: 1 addition & 0 deletions cmd/helm-docs/command_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func newHelmDocsCommand(run func(cmd *cobra.Command, args []string)) (*cobra.Com
command.PersistentFlags().StringP("output-file", "o", "README.md", "markdown file path relative to each chart directory to which rendered documentation will be written")
command.PersistentFlags().StringP("sort-values-order", "s", document.AlphaNumSortOrder, fmt.Sprintf("order in which to sort the values table (\"%s\" or \"%s\")", document.AlphaNumSortOrder, document.FileSortOrder))
command.PersistentFlags().StringSliceP("template-files", "t", []string{"README.md.gotmpl"}, "gotemplate file paths relative to each chart directory from which documentation will be generated")
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")

viper.AutomaticEnv()
Expand Down
12 changes: 7 additions & 5 deletions cmd/helm-docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/norwoodj/helm-docs/pkg/helm"
)

func retrieveInfoAndPrintDocumentation(chartDirectory string, chartSearchRoot string, templateFiles []string, waitGroup *sync.WaitGroup, dryRun bool) {
func retrieveInfoAndPrintDocumentation(chartDirectory string, chartSearchRoot string, templateFiles []string, waitGroup *sync.WaitGroup, badgeStyle string, dryRun bool) {
defer waitGroup.Done()
chartDocumentationInfo, err := helm.ParseChartInformation(path.Join(chartSearchRoot, chartDirectory))

Expand All @@ -23,11 +23,11 @@ func retrieveInfoAndPrintDocumentation(chartDirectory string, chartSearchRoot st
return
}

document.PrintDocumentation(chartDocumentationInfo, chartSearchRoot, templateFiles, dryRun, version)
document.PrintDocumentation(chartDocumentationInfo, chartSearchRoot, templateFiles, dryRun, version, badgeStyle)

}

func helmDocs(cmd *cobra.Command, _ []string) {
func helmDocs(_ *cobra.Command, _ []string) {
initializeCli()

chartSearchRoot := viper.GetString("chart-search-root")
Expand Down Expand Up @@ -56,6 +56,8 @@ func helmDocs(cmd *cobra.Command, _ []string) {
templateFiles := viper.GetStringSlice("template-files")
log.Debugf("Rendering from optional template files [%s]", strings.Join(templateFiles, ", "))

badgeStyle := viper.GetString("badge-style")

dryRun := viper.GetBool("dry-run")
waitGroup := sync.WaitGroup{}

Expand All @@ -64,9 +66,9 @@ func helmDocs(cmd *cobra.Command, _ []string) {

// On dry runs all output goes to stdout, and so as to not jumble things, generate serially
if dryRun {
retrieveInfoAndPrintDocumentation(c, fullChartSearchRoot, templateFiles, &waitGroup, dryRun)
retrieveInfoAndPrintDocumentation(c, fullChartSearchRoot, templateFiles, &waitGroup, badgeStyle, dryRun)
} else {
go retrieveInfoAndPrintDocumentation(c, fullChartSearchRoot, templateFiles, &waitGroup, dryRun)
go retrieveInfoAndPrintDocumentation(c, fullChartSearchRoot, templateFiles, &waitGroup, badgeStyle, dryRun)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/document/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ func getOutputFile(chartDirectory string, dryRun bool) (*os.File, error) {
return f, err
}

func PrintDocumentation(chartDocumentationInfo helm.ChartDocumentationInfo, chartSearchRoot string, templateFiles []string, dryRun bool, helmDocsVersion string) {
func PrintDocumentation(chartDocumentationInfo helm.ChartDocumentationInfo, chartSearchRoot string, templateFiles []string, dryRun bool, helmDocsVersion string, badgeStyle string) {
log.Infof("Generating README Documentation for chart %s", chartDocumentationInfo.ChartDirectory)

chartDocumentationTemplate, err := newChartDocumentationTemplate(
chartDocumentationInfo,
chartSearchRoot,
templateFiles,
badgeStyle,
)

if err != nil {
Expand Down
25 changes: 13 additions & 12 deletions pkg/document/template.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package document

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -62,31 +63,31 @@ func getDeprecatedTemplate() string {
return deprecatedTemplateBuilder.String()
}

func getVersionTemplates() string {
func getVersionTemplates(badgeStyle string) string {
versionBuilder := strings.Builder{}
versionBuilder.WriteString(`{{ define "chart.version" }}{{ .Version }}{{ end }}\n`)
versionBuilder.WriteString(`{{ define "chart.versionBadge" }}`)
versionBuilder.WriteString(`![Version: {{ .Version }}](https://img.shields.io/badge/Version-{{ .Version | replace "-" "--" }}-informational?style=flat-square) `)
versionBuilder.WriteString(fmt.Sprintf(`![Version: {{ .Version }}](https://img.shields.io/badge/Version-{{ .Version | replace "-" "--" }}-informational?style=%s) `, badgeStyle))
versionBuilder.WriteString("{{ end }}")

return versionBuilder.String()
}

func getTypeTemplate() string {
func getTypeTemplate(badgeStyle string) string {
typeBuilder := strings.Builder{}
typeBuilder.WriteString(`{{ define "chart.type" }}{{ .Type }}{{ end }}\n`)
typeBuilder.WriteString(`{{ define "chart.typeBadge" }}`)
typeBuilder.WriteString("{{ if .Type }}![Type: {{ .Type }}](https://img.shields.io/badge/Type-{{ .Type }}-informational?style=flat-square) {{ end }}")
typeBuilder.WriteString(fmt.Sprintf("{{ if .Type }}![Type: {{ .Type }}](https://img.shields.io/badge/Type-{{ .Type }}-informational?style=%s) {{ end }}", badgeStyle))
typeBuilder.WriteString("{{ end }}")

return typeBuilder.String()
}

func getAppVersionTemplate() string {
func getAppVersionTemplate(badgeStyle string) string {
appVersionBuilder := strings.Builder{}
appVersionBuilder.WriteString(`{{ define "chart.appVersion" }}{{ .AppVersion }}{{ end }}\n`)
appVersionBuilder.WriteString(`{{ define "chart.appVersionBadge" }}`)
appVersionBuilder.WriteString(`{{ if .AppVersion }}![AppVersion: {{ .AppVersion }}](https://img.shields.io/badge/AppVersion-{{ .AppVersion | replace "-" "--" }}-informational?style=flat-square) {{ end }}`)
appVersionBuilder.WriteString(fmt.Sprintf(`{{ if .AppVersion }}![AppVersion: {{ .AppVersion }}](https://img.shields.io/badge/AppVersion-{{ .AppVersion | replace "-" "--" }}-informational?style=%s) {{ end }}`, badgeStyle))
appVersionBuilder.WriteString("{{ end }}")

return appVersionBuilder.String()
Expand Down Expand Up @@ -281,7 +282,7 @@ func getDocumentationTemplate(chartDirectory string, chartSearchRoot string, tem
return string(allTemplateContents), nil
}

func getDocumentationTemplates(chartDirectory string, chartSearchRoot string, templateFiles []string) ([]string, error) {
func getDocumentationTemplates(chartDirectory string, chartSearchRoot string, templateFiles []string, badgeStyle string) ([]string, error) {
documentationTemplate, err := getDocumentationTemplate(chartDirectory, chartSearchRoot, templateFiles)

if err != nil {
Expand All @@ -293,11 +294,11 @@ func getDocumentationTemplates(chartDirectory string, chartSearchRoot string, te
getNameTemplate(),
getHeaderTemplate(),
getDeprecatedTemplate(),
getAppVersionTemplate(),
getAppVersionTemplate(badgeStyle),
getBadgesTemplates(),
getDescriptionTemplate(),
getVersionTemplates(),
getTypeTemplate(),
getVersionTemplates(badgeStyle),
getTypeTemplate(badgeStyle),
getSourceLinkTemplates(),
getRequirementsTableTemplates(),
getValuesTableTemplates(),
Expand All @@ -308,10 +309,10 @@ func getDocumentationTemplates(chartDirectory string, chartSearchRoot string, te
}, nil
}

func newChartDocumentationTemplate(chartDocumentationInfo helm.ChartDocumentationInfo, chartSearchRoot string, templateFiles []string) (*template.Template, error) {
func newChartDocumentationTemplate(chartDocumentationInfo helm.ChartDocumentationInfo, chartSearchRoot string, templateFiles []string, badgeStyle string) (*template.Template, error) {
documentationTemplate := template.New(chartDocumentationInfo.ChartDirectory)
documentationTemplate.Funcs(sprig.TxtFuncMap())
goTemplateList, err := getDocumentationTemplates(chartDocumentationInfo.ChartDirectory, chartSearchRoot, templateFiles)
goTemplateList, err := getDocumentationTemplates(chartDocumentationInfo.ChartDirectory, chartSearchRoot, templateFiles, badgeStyle)

if err != nil {
return nil, err
Expand Down

0 comments on commit 6754e5a

Please sign in to comment.