Skip to content

Commit b77e783

Browse files
allow multiple templates
1 parent b6ff99b commit b77e783

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

cmd/helm-docs/command_line.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ func newHelmDocsCommand(run func(cmd *cobra.Command, args []string)) (*cobra.Com
4848
command.PersistentFlags().StringP("log-level", "l", "info", logLevelUsage)
4949
command.PersistentFlags().StringP("output-file", "o", "README.md", "markdown file path relative to each chart directory to which rendered documentation will be written")
5050
command.PersistentFlags().StringP("template-file", "t", "README.md.gotmpl", "gotemplate file path relative to each chart directory from which documentation will be generated")
51-
51+
command.PersistentFlags().StringSliceP("template-files", "T", []string{"README.md.gotmpl"}, "gotemplate file paths relative to each chart directory from which documentation will be generated")
52+
if err := command.PersistentFlags().MarkDeprecated("template-file", "in favor of template-files"); err != nil {
53+
return command, err
54+
}
5255
viper.AutomaticEnv()
5356
viper.SetEnvPrefix("HELM_DOCS")
5457
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

cmd/helm-docs/main.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"strings"
66
"sync"
77

8-
"github.com/norwoodj/helm-docs/pkg/document"
9-
"github.com/norwoodj/helm-docs/pkg/helm"
108
log "github.com/sirupsen/logrus"
119
"github.com/spf13/cobra"
1210
"github.com/spf13/viper"
11+
12+
"github.com/norwoodj/helm-docs/pkg/document"
13+
"github.com/norwoodj/helm-docs/pkg/helm"
1314
)
1415

1516
func retrieveInfoAndPrintDocumentation(chartDirectory string, waitGroup *sync.WaitGroup, dryRun bool) {
@@ -25,15 +26,20 @@ func retrieveInfoAndPrintDocumentation(chartDirectory string, waitGroup *sync.Wa
2526

2627
}
2728

28-
func helmDocs(_ *cobra.Command, _ []string) {
29+
func helmDocs(cmd *cobra.Command, _ []string) {
2930
initializeCli()
3031
chartDirs, err := helm.FindChartDirectories()
31-
3232
if err != nil {
3333
log.Errorf("Error finding chart directories: %s", err)
3434
os.Exit(1)
3535
}
36-
36+
if cmd.PersistentFlags().Changed("template-file") && cmd.PersistentFlags().Changed("template-files") {
37+
log.Errorf("you cannot use both template-file and template-files. consider using just template-files")
38+
} else if cmd.PersistentFlags().Changed("template-files") {
39+
viper.Set("template-type", "template-files")
40+
} else {
41+
viper.Set("template-type", "template-file")
42+
}
3743
log.Infof("Found Chart directories [%s]", strings.Join(chartDirs, ", "))
3844
dryRun := viper.GetBool("dry-run")
3945
waitGroup := sync.WaitGroup{}

pkg/document/template.go

+26-14
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"text/template"
99

1010
"github.com/Masterminds/sprig"
11-
"github.com/norwoodj/helm-docs/pkg/helm"
1211
log "github.com/sirupsen/logrus"
1312
"github.com/spf13/viper"
13+
14+
"github.com/norwoodj/helm-docs/pkg/helm"
1415
)
1516

1617
const defaultDocumentationTemplate = `{{ template "chart.header" . }}
@@ -218,21 +219,32 @@ func getValuesTableTemplates() string {
218219
}
219220

220221
func getDocumentationTemplate(chartDirectory string) (string, error) {
221-
templateFile := viper.GetString("template-file")
222-
templateFileForChart := path.Join(chartDirectory, templateFile)
223-
224-
if _, err := os.Stat(templateFileForChart); os.IsNotExist(err) {
225-
log.Debugf("Did not find template file %s for chart %s, using default template", templateFile, chartDirectory)
226-
return defaultDocumentationTemplate, nil
222+
templateFiles := make([]string, 0)
223+
templateType := viper.GetString("template-type")
224+
if templateType == "template-file" {
225+
templateFiles = append(templateFiles, viper.GetString("template-file"))
226+
} else {
227+
templateFiles = append(templateFiles, viper.GetStringSlice("template-files")...)
227228
}
228-
229-
log.Debugf("Using template file %s for chart %s", templateFile, chartDirectory)
230-
templateContents, err := ioutil.ReadFile(templateFileForChart)
231-
if err != nil {
232-
return "", err
229+
templateFilesForChart := make([]string, 0)
230+
for _, templateFile := range templateFiles {
231+
templateFileForChart := path.Join(chartDirectory, templateFile)
232+
if _, err := os.Stat(templateFileForChart); os.IsNotExist(err) {
233+
log.Debugf("Did not find template file %s for chart %s, using default template", templateFile, chartDirectory)
234+
return defaultDocumentationTemplate, nil
235+
}
236+
templateFilesForChart = append(templateFilesForChart, templateFileForChart)
233237
}
234-
235-
return string(templateContents), nil
238+
log.Debugf("Using template files %s for chart %s", templateFiles, chartDirectory)
239+
allTemplateContents := make([]byte, 0)
240+
for _, templateFileForChart := range templateFilesForChart {
241+
templateContents, err := ioutil.ReadFile(templateFileForChart)
242+
if err != nil {
243+
return "", err
244+
}
245+
allTemplateContents = append(allTemplateContents, templateContents...)
246+
}
247+
return string(allTemplateContents), nil
236248
}
237249

238250
func getDocumentationTemplates(chartDirectory string) ([]string, error) {

0 commit comments

Comments
 (0)