Skip to content

Commit

Permalink
Allowing single word resources to use templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Jun 6, 2022
1 parent ed20132 commit b4b63ec
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/provider/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj
g.infof("rendering %q", rel)
switch relDir {
case "data-sources/":
resName := shortName + "_" + removeAllExt(relFile)
resName := resourceName(shortName, relFile)
resSchema, ok := providerSchema.DataSourceSchemas[resName]
if ok {
tmpl := resourceTemplate(tmplData)
Expand All @@ -429,7 +429,7 @@ func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfj
return nil
}
case "resources/":
resName := shortName + "_" + removeAllExt(relFile)
resName := resourceName(shortName, relFile)
resSchema, ok := providerSchema.ResourceSchemas[resName]
if ok {
tmpl := resourceTemplate(tmplData)
Expand Down
1 change: 1 addition & 0 deletions internal/provider/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"text/template"

tfjson "github.com/hashicorp/terraform-json"

"github.com/hashicorp/terraform-plugin-docs/internal/mdplain"
"github.com/hashicorp/terraform-plugin-docs/internal/tmplfuncs"
"github.com/hashicorp/terraform-plugin-docs/schemamd"
Expand Down
12 changes: 12 additions & 0 deletions internal/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ func removeAllExt(file string) string {
}
}

// resourceName determines whether the shortname and the relFile
// are identical after file extensions have been stripped from the
// latter. This allows single word resources (e.g., http) to use
// templates (e.g., http.md.tmpl).
func resourceName(shortName, relFile string) string {
if shortName == removeAllExt(relFile) {
return shortName
}

return shortName + "_" + removeAllExt(relFile)
}

func writeFile(path string, data string) error {
dir, _ := filepath.Split(path)
err := os.MkdirAll(dir, 0755)
Expand Down

0 comments on commit b4b63ec

Please sign in to comment.