diff --git a/.changes/unreleased/ENHANCEMENTS-20230629-144119.yaml b/.changes/unreleased/ENHANCEMENTS-20230629-144119.yaml new file mode 100644 index 00000000..16982145 --- /dev/null +++ b/.changes/unreleased/ENHANCEMENTS-20230629-144119.yaml @@ -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" diff --git a/.changes/unreleased/ENHANCEMENTS-20230706-111427.yaml b/.changes/unreleased/ENHANCEMENTS-20230706-111427.yaml new file mode 100644 index 00000000..2f5c35d6 --- /dev/null +++ b/.changes/unreleased/ENHANCEMENTS-20230706-111427.yaml @@ -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" diff --git a/go.mod b/go.mod index d1bf9e04..448b2577 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 ) diff --git a/go.sum b/go.sum index f0b0234e..5ce176d4 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/provider/generate.go b/internal/provider/generate.go index 71fd1f8a..b4c3293f 100644 --- a/internal/provider/generate.go +++ b/internal/provider/generate.go @@ -8,6 +8,7 @@ import ( "fmt" "os" "os/exec" + "path" "path/filepath" "runtime" "strings" @@ -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 ( @@ -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 { @@ -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") diff --git a/internal/provider/validate.go b/internal/provider/validate.go index c2748e80..329e05d1 100644 --- a/internal/provider/validate.go +++ b/internal/provider/validate.go @@ -98,6 +98,7 @@ func validateStaticDocs(ui cli.Ui, dir string) error { "data-sources", "guides", "resources", + "cdktf", ), checkBlockedExtensions( ".html.md.tmpl",