Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing single word resources to use templates #147

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions CHANGELOG.md
@@ -1,12 +1,18 @@
# 0.9.1 (Unreleased)

BUG FIXES:

* cmd/tfplugindocs: Allow single word resources to use templates ([147](https://github.com/hashicorp/terraform-plugin-docs/pull/147)).

# 0.9.0 (June 1, 2022)

NEW FEATURES:

* cmd/tflugindocs: Additional CLI arguments `provider-name`, `rendered-provider-name`, `rendered-website-dir`, `examples-dir`, `website-temp-dir`, and `website-source-dir`. These allow to further customise generated doc ([#95](https://github.com/hashicorp/terraform-plugin-docs/pull/95)).
* cmd/tfplugindocs: Additional CLI arguments `provider-name`, `rendered-provider-name`, `rendered-website-dir`, `examples-dir`, `website-temp-dir`, and `website-source-dir`. These allow to further customise generated doc ([#95](https://github.com/hashicorp/terraform-plugin-docs/pull/95)).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those are definitely typos I made 🤦

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. I like that we're keeping an eye out for each other 👍


ENHANCEMENTS:

* cmd/tflugindocs: Implemented usage output (i.e. `--help`) for `generate` and `validate` commands ([#95](https://github.com/hashicorp/terraform-plugin-docs/pull/95)).
* cmd/tfplugindocs: Implemented usage output (i.e. `--help`) for `generate` and `validate` commands ([#95](https://github.com/hashicorp/terraform-plugin-docs/pull/95)).

# 0.8.1 (May 10, 2022)

Expand Down
4 changes: 2 additions & 2 deletions internal/provider/generate.go
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: isn't shortName the Provider shortname here?

In the case of the HTTP provider (through which you identified the issue), I believe the http refers to the resource. Otherwise if, for example, you decide to create a second resource in this provider (say, for the sake of it, https), you won't be able to match it OR it would match the wrong file.

I'll leave a comment in the unit test below.

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
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
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a quick unit test for this?

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