From fa0ab6f781fe856f2232e356fc10c662bc9c82a7 Mon Sep 17 00:00:00 2001 From: Engin Diri Date: Sun, 16 Jan 2022 11:14:47 +0100 Subject: [PATCH] feat: make the badge style from shields.io configurable --- cmd/helm-docs/command_line.go | 1 + cmd/helm-docs/main.go | 12 +++++++----- pkg/document/generate.go | 3 ++- pkg/document/template.go | 25 +++++++++++++------------ 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/cmd/helm-docs/command_line.go b/cmd/helm-docs/command_line.go index 7c9ed8a..e7dab72 100644 --- a/cmd/helm-docs/command_line.go +++ b/cmd/helm-docs/command_line.go @@ -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") viper.AutomaticEnv() viper.SetEnvPrefix("HELM_DOCS") diff --git a/cmd/helm-docs/main.go b/cmd/helm-docs/main.go index b10c4eb..78e408a 100644 --- a/cmd/helm-docs/main.go +++ b/cmd/helm-docs/main.go @@ -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)) @@ -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") @@ -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{} @@ -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) } } diff --git a/pkg/document/generate.go b/pkg/document/generate.go index 71acbac..e05a019 100644 --- a/pkg/document/generate.go +++ b/pkg/document/generate.go @@ -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 { diff --git a/pkg/document/template.go b/pkg/document/template.go index da3f427..b396bb0 100644 --- a/pkg/document/template.go +++ b/pkg/document/template.go @@ -1,6 +1,7 @@ package document import ( + "fmt" "io/ioutil" "os" "path" @@ -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() @@ -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 { @@ -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(), @@ -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