Skip to content

Commit

Permalink
generate: Remove only tfplugindocs managed files and subdirectories…
Browse files Browse the repository at this point in the history
… from rendered website directory (#267)

* Introduce exclude-rendered-website-sub-dir flag

* Add "cdktf" to list of allowed directories

* Add Changie entry

* Revert `exclude-rendered-website-sub-dir` flag and only remove files and directories directly managed by `tfplugindocs`

* Merge branch 'main' into SBGoods/skip-subdir
  • Loading branch information
SBGoods committed Jul 6, 2023
1 parent 66368e3 commit 93aa7d3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230629-144119.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'generate: Prevent files and subdirectories in the rendered website directory
that are not directly managed by `tfplugindocs` from being deleted during generation'
time: 2023-06-29T14:41:19.134991-04:00
custom:
Issue: "267"
5 changes: 5 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20230706-111427.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'validate: Add `cdktf` to list of allowed rendered website subdirectories'
time: 2023-07-06T11:14:27.351158-04:00
custom:
Issue: "267"
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mitchellh/cli v1.1.5
github.com/russross/blackfriday v1.6.0
github.com/zclconf/go-cty v1.13.2
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
golang.org/x/text v0.11.0
)

Expand Down Expand Up @@ -40,6 +41,6 @@ require (
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/sys v0.7.0 // indirect
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
Expand Down
37 changes: 36 additions & 1 deletion internal/provider/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
"github.com/mitchellh/cli"
"golang.org/x/exp/slices"
)

var (
Expand Down Expand Up @@ -63,6 +65,16 @@ var (
providerFileTemplate("index.html.markdown"),
providerFileTemplate("index.html.md"),
}

managedWebsiteSubDirectories = []string{
"data-sources",
"guides",
"resources",
}

managedWebsiteFiles = []string{
"index.md",
}
)

type generator struct {
Expand Down Expand Up @@ -416,11 +428,34 @@ func (g *generator) renderMissingDocs(providerName string, providerSchema *tfjso

func (g *generator) renderStaticWebsite(providerName string, providerSchema *tfjson.ProviderSchema) error {
g.infof("cleaning rendered website dir")
err := os.RemoveAll(g.ProviderDocsDir())
dirEntry, err := os.ReadDir(g.ProviderDocsDir())
if err != nil {
return err
}

for _, file := range dirEntry {

// Remove subdirectories managed by tfplugindocs
if file.IsDir() && slices.Contains(managedWebsiteSubDirectories, file.Name()) {
g.infof("removing directory: %q", file.Name())
err = os.RemoveAll(path.Join(g.ProviderDocsDir(), file.Name()))
if err != nil {
return err
}
continue
}

// Remove files managed by tfplugindocs
if !file.IsDir() && slices.Contains(managedWebsiteFiles, file.Name()) {
g.infof("removing file: %q", file.Name())
err = os.RemoveAll(path.Join(g.ProviderDocsDir(), file.Name()))
if err != nil {
return err
}
continue
}
}

shortName := providerShortName(providerName)

g.infof("rendering templated website to static markdown")
Expand Down
1 change: 1 addition & 0 deletions internal/provider/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func validateStaticDocs(ui cli.Ui, dir string) error {
"data-sources",
"guides",
"resources",
"cdktf",
),
checkBlockedExtensions(
".html.md.tmpl",
Expand Down

0 comments on commit 93aa7d3

Please sign in to comment.