Skip to content

Commit a43ccae

Browse files
authored
Merge pull request #54 from DirtyCajunRice/master
[feat] Allow multiple templates
2 parents 31983b5 + b77e783 commit a43ccae

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" . }}
@@ -209,21 +210,32 @@ func getValuesTableTemplates() string {
209210
}
210211

211212
func getDocumentationTemplate(chartDirectory string) (string, error) {
212-
templateFile := viper.GetString("template-file")
213-
templateFileForChart := path.Join(chartDirectory, templateFile)
214-
215-
if _, err := os.Stat(templateFileForChart); os.IsNotExist(err) {
216-
log.Debugf("Did not find template file %s for chart %s, using default template", templateFile, chartDirectory)
217-
return defaultDocumentationTemplate, nil
213+
templateFiles := make([]string, 0)
214+
templateType := viper.GetString("template-type")
215+
if templateType == "template-file" {
216+
templateFiles = append(templateFiles, viper.GetString("template-file"))
217+
} else {
218+
templateFiles = append(templateFiles, viper.GetStringSlice("template-files")...)
218219
}
219-
220-
log.Debugf("Using template file %s for chart %s", templateFile, chartDirectory)
221-
templateContents, err := ioutil.ReadFile(templateFileForChart)
222-
if err != nil {
223-
return "", err
220+
templateFilesForChart := make([]string, 0)
221+
for _, templateFile := range templateFiles {
222+
templateFileForChart := path.Join(chartDirectory, templateFile)
223+
if _, err := os.Stat(templateFileForChart); os.IsNotExist(err) {
224+
log.Debugf("Did not find template file %s for chart %s, using default template", templateFile, chartDirectory)
225+
return defaultDocumentationTemplate, nil
226+
}
227+
templateFilesForChart = append(templateFilesForChart, templateFileForChart)
224228
}
225-
226-
return string(templateContents), nil
229+
log.Debugf("Using template files %s for chart %s", templateFiles, chartDirectory)
230+
allTemplateContents := make([]byte, 0)
231+
for _, templateFileForChart := range templateFilesForChart {
232+
templateContents, err := ioutil.ReadFile(templateFileForChart)
233+
if err != nil {
234+
return "", err
235+
}
236+
allTemplateContents = append(allTemplateContents, templateContents...)
237+
}
238+
return string(allTemplateContents), nil
227239
}
228240

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

0 commit comments

Comments
 (0)