Skip to content

Commit

Permalink
Add template context for provider, res, data source docs
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Jul 14, 2020
1 parent 6668ff1 commit eadddf2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
55 changes: 51 additions & 4 deletions internal/provider/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (g *generator) Generate(ctx context.Context) error {
return err
}

err = g.renderStaticWebsite()
err = g.renderStaticWebsite(providerName, providerSchema)
if err != nil {
return err
}
Expand Down Expand Up @@ -319,7 +319,7 @@ func (g *generator) renderMissingDocs(providerName string, providerSchema *tfjso
return nil
}

func (g *generator) renderStaticWebsite() error {
func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfjson.ProviderSchema) error {
g.infof("cleaning rendered website dir")
err := os.RemoveAll(renderedWebsiteDir)
if err != nil {
Expand Down Expand Up @@ -357,7 +357,6 @@ func (g *generator) renderStaticWebsite() error {
if err != nil {
return fmt.Errorf("unable to read file %q: %w", rel, err)
}
tmpl := docTemplate(tmplData)

out, err := os.Create(renderedPath)
if err != nil {
Expand All @@ -366,11 +365,59 @@ func (g *generator) renderStaticWebsite() error {
defer out.Close()

g.infof("rendering %q", rel)

relDir, relFile := filepath.Split(rel)
switch relDir {
case "data-sources":
resName := removeAllExt(relFile)
resSchema, ok := providerSchema.DataSourceSchemas[resName]
if ok {
tmpl := resourceTemplate(tmplData)
render, err := tmpl.Render("Data Source", providerName, resName, "", "", resSchema)
if err != nil {
return fmt.Errorf("unable to render data source template %q: %w", rel, err)
}
_, err = out.WriteString(render)
if err != nil {
return fmt.Errorf("unable to write rendered string: %w", err)
}
return nil
}
case "resources":
resName := removeAllExt(relFile)
resSchema, ok := providerSchema.ResourceSchemas[resName]
if ok {
tmpl := resourceTemplate(tmplData)
render, err := tmpl.Render("Resource", providerName, resName, "", "", resSchema)
if err != nil {
return fmt.Errorf("unable to render resource template %q: %w", rel, err)
}
_, err = out.WriteString(render)
if err != nil {
return fmt.Errorf("unable to write regindered string: %w", err)
}
return nil
}
case "": // provider
if relFile == "index.md.tmpl" {
tmpl := providerTemplate(tmplData)
render, err := tmpl.Render(providerName, "", providerSchema.ConfigSchema)
if err != nil {
return fmt.Errorf("unable to render provider template %q: %w", rel, err)
}
_, err = out.WriteString(render)
if err != nil {
return fmt.Errorf("unable to write rendered string: %w", err)
}
return nil
}
}

tmpl := docTemplate(tmplData)
err = tmpl.Render(out)
if err != nil {
return fmt.Errorf("unable to render template %q: %w", rel, err)
}

return nil
})
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ func resourceShortName(name, providerName string) string {
// return os.Rename(tmp.Name(), dst)
// }

func removeAllExt(file string) string {
for {
ext := filepath.Ext(file)
if ext == "" || ext == file {
return file
}
file = strings.TrimSuffix(file, ext)
}
}

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

0 comments on commit eadddf2

Please sign in to comment.