Skip to content

Commit

Permalink
Merge pull request #54 from DirtyCajunRice/master
Browse files Browse the repository at this point in the history
[feat] Allow multiple templates
  • Loading branch information
norwoodj committed Oct 3, 2020
2 parents 31983b5 + b77e783 commit a43ccae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
5 changes: 4 additions & 1 deletion cmd/helm-docs/command_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ func newHelmDocsCommand(run func(cmd *cobra.Command, args []string)) (*cobra.Com
command.PersistentFlags().StringP("log-level", "l", "info", logLevelUsage)
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("template-file", "t", "README.md.gotmpl", "gotemplate file path relative to each chart directory from which documentation will be generated")

command.PersistentFlags().StringSliceP("template-files", "T", []string{"README.md.gotmpl"}, "gotemplate file paths relative to each chart directory from which documentation will be generated")
if err := command.PersistentFlags().MarkDeprecated("template-file", "in favor of template-files"); err != nil {
return command, err
}
viper.AutomaticEnv()
viper.SetEnvPrefix("HELM_DOCS")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
Expand Down
16 changes: 11 additions & 5 deletions cmd/helm-docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"strings"
"sync"

"github.com/norwoodj/helm-docs/pkg/document"
"github.com/norwoodj/helm-docs/pkg/helm"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/norwoodj/helm-docs/pkg/document"
"github.com/norwoodj/helm-docs/pkg/helm"
)

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

}

func helmDocs(_ *cobra.Command, _ []string) {
func helmDocs(cmd *cobra.Command, _ []string) {
initializeCli()
chartDirs, err := helm.FindChartDirectories()

if err != nil {
log.Errorf("Error finding chart directories: %s", err)
os.Exit(1)
}

if cmd.PersistentFlags().Changed("template-file") && cmd.PersistentFlags().Changed("template-files") {
log.Errorf("you cannot use both template-file and template-files. consider using just template-files")
} else if cmd.PersistentFlags().Changed("template-files") {
viper.Set("template-type", "template-files")
} else {
viper.Set("template-type", "template-file")
}
log.Infof("Found Chart directories [%s]", strings.Join(chartDirs, ", "))
dryRun := viper.GetBool("dry-run")
waitGroup := sync.WaitGroup{}
Expand Down
40 changes: 26 additions & 14 deletions pkg/document/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
"text/template"

"github.com/Masterminds/sprig"
"github.com/norwoodj/helm-docs/pkg/helm"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"

"github.com/norwoodj/helm-docs/pkg/helm"
)

const defaultDocumentationTemplate = `{{ template "chart.header" . }}
Expand Down Expand Up @@ -209,21 +210,32 @@ func getValuesTableTemplates() string {
}

func getDocumentationTemplate(chartDirectory string) (string, error) {
templateFile := viper.GetString("template-file")
templateFileForChart := path.Join(chartDirectory, templateFile)

if _, err := os.Stat(templateFileForChart); os.IsNotExist(err) {
log.Debugf("Did not find template file %s for chart %s, using default template", templateFile, chartDirectory)
return defaultDocumentationTemplate, nil
templateFiles := make([]string, 0)
templateType := viper.GetString("template-type")
if templateType == "template-file" {
templateFiles = append(templateFiles, viper.GetString("template-file"))
} else {
templateFiles = append(templateFiles, viper.GetStringSlice("template-files")...)
}

log.Debugf("Using template file %s for chart %s", templateFile, chartDirectory)
templateContents, err := ioutil.ReadFile(templateFileForChart)
if err != nil {
return "", err
templateFilesForChart := make([]string, 0)
for _, templateFile := range templateFiles {
templateFileForChart := path.Join(chartDirectory, templateFile)
if _, err := os.Stat(templateFileForChart); os.IsNotExist(err) {
log.Debugf("Did not find template file %s for chart %s, using default template", templateFile, chartDirectory)
return defaultDocumentationTemplate, nil
}
templateFilesForChart = append(templateFilesForChart, templateFileForChart)
}

return string(templateContents), nil
log.Debugf("Using template files %s for chart %s", templateFiles, chartDirectory)
allTemplateContents := make([]byte, 0)
for _, templateFileForChart := range templateFilesForChart {
templateContents, err := ioutil.ReadFile(templateFileForChart)
if err != nil {
return "", err
}
allTemplateContents = append(allTemplateContents, templateContents...)
}
return string(allTemplateContents), nil
}

func getDocumentationTemplates(chartDirectory string) ([]string, error) {
Expand Down

0 comments on commit a43ccae

Please sign in to comment.