Skip to content

Commit 068a226

Browse files
SBGoodsbfladaustinvalle
authored
Implement migrate sub-command (#314)
* Add migrate subcommand * Change migrated file extensions to `.md.tmpl` * Finish `migrate` subcommand implementation * Remove `-help` output check * Add changelog entry * Update changelog entry Co-authored-by: Brian Flad <[email protected]> * fix changelog entry * Remove `--old-website-source-dir` flag and support migrating `docs/` subdirectory * Support converting files with any file extension to `.md.tmpl` * Refactor `Migrate()` method * Skip `layout` front matter * Fix linting errors * Add comment to generated templates explaining functionality * Remove `website/` directory at the end of migration * Fix `ineffassign` lint error * Update README.md Co-authored-by: Austin Valle <[email protected]> * Resolve comments from code review --------- Co-authored-by: Brian Flad <[email protected]> Co-authored-by: Austin Valle <[email protected]>
1 parent 8456c0c commit 068a226

File tree

12 files changed

+2413
-3
lines changed

12 files changed

+2413
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: FEATURES
2+
body: 'migrate: Added new `migrate` subcommand that migrates existing provider docs using the
3+
rendered website source directories (`website/docs/` or `/docs/`) to a `terraform-plugin-docs`-supported
4+
templates directory.'
5+
time: 2023-12-20T14:12:44.820323-05:00
6+
custom:
7+
Issue: "314"

README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The primary way users will interact with this is the `tfplugindocs` CLI tool to
55

66
## `tfplugindocs`
77

8-
The `tfplugindocs` CLI has two main commands, `validate` and `generate` (`generate` is the default).
8+
The `tfplugindocs` CLI has three main commands, `migrate`, `validate` and `generate` (`generate` is the default).
99
This tool will let you generate documentation for your provider from live example `.tf` files and markdown templates.
1010
It will also export schema information from the provider (using `terraform providers schema -json`),
1111
and sync the schema with the reference documents.
@@ -28,6 +28,7 @@ Usage: tfplugindocs [--version] [--help] <command> [<args>]
2828
Available commands are:
2929
the generate command is run by default
3030
generate generates a plugin website from code, templates, and examples
31+
migrate migrates website files from either the legacy rendered website directory (`website/docs/r`) or the docs rendered website directory (`docs/resources`) to the tfplugindocs supported structure (`templates/`).
3132
validate validates a plugin website for the current directory
3233

3334
```
@@ -59,6 +60,18 @@ $ tfplugindocs validate --help
5960
Usage: tfplugindocs validate [<args>]
6061
```
6162
63+
`migrate` command:
64+
65+
```shell
66+
$ tfplugindocs migrate --help
67+
68+
Usage: tfplugindocs migrate [<args>]
69+
70+
--examples-dir <ARG> examples directory based on provider-dir (default: "examples")
71+
--provider-dir <ARG> relative or absolute path to the root provider code directory when running the command outside the root provider code directory
72+
--templates-dir <ARG> new website templates directory based on provider-dir; files will be migrated to this directory (default: "templates")
73+
```
74+
6275
### How it Works
6376
6477
When you run `tfplugindocs`, by default from the root directory of a provider codebase, the tool takes the following actions:
@@ -100,6 +113,23 @@ Otherwise, the provider developer can set an arbitrary description like this:
100113
// ...
101114
```
102115

116+
#### Migrate subcommand
117+
118+
The `migrate` subcommand can be used to migrate website files from either the legacy rendered website directory (`website/docs/r`) or the docs
119+
rendered website directory (`docs/resources`) to the `tfplugindocs` supported structure (`templates/`). Markdown files in the rendered website
120+
directory will be converted to `tfplugindocs` templates. The legacy `website/` directory will be removed after migration to avoid Terraform Registry
121+
ingress issues.
122+
123+
The `migrate` subcommand takes the following actions:
124+
1. Determines the rendered website directory based on the `--provider-dir` argument
125+
2. Copies the contents of the rendered website directory to the `--templates-dir` folder (will create this folder if it doesn't exist)
126+
3. (if the rendered website is using legacy format) Renames `docs/d/` and `docs/r/` subdirectories to `data-sources/` and `resources/` respectively
127+
4. Change file suffixes for Markdown files to `.md.tmpl` to create website templates
128+
5. Extracts code blocks from website docs to create individual example files in `--examples-dir` (will create this folder if it doesn't exist)
129+
6. Replace extracted example code in website templates with `codefile`/`tffile` template functions referencing the example files.
130+
7. Copies non-template files to `--templates-dir` folder
131+
8. Removes the `website/` directory
132+
103133
### Conventional Paths
104134

105135
The generation of missing documentation is based on a number of assumptions / conventional paths.
@@ -130,6 +160,37 @@ For examples:
130160
| `examples/resources/<resource name>/resource.tf` | Resource example config |
131161
| `examples/resources/<resource name>/import.sh` | Resource example import command |
132162

163+
#### Migration
164+
165+
The `migrate` subcommand assumes the following conventional paths for the rendered website directory:
166+
167+
Legacy website directory structure:
168+
169+
| Path | Description |
170+
|---------------------------------------------------|-----------------------------|
171+
| `website/` | Root of website docs |
172+
| `website/docs/guides` | Root of guides subdirectory |
173+
| `website/docs/index.html.markdown` | Docs index page |
174+
| `website/docs/d/<data source name>.html.markdown` | Data source page |
175+
| `website/docs/r/<resource name>.html.markdown` | Resource page |
176+
177+
Docs website directory structure:
178+
179+
| Path | Description |
180+
|------------------------------------------------------|-----------------------------|
181+
| `docs/` | Root of website docs |
182+
| `docs/guides` | Root of guides subdirectory |
183+
| `docs/index.html.markdown` | Docs index page |
184+
| `docs/data-sources/<data source name>.html.markdown` | Data source page |
185+
| `docs/resources/<resource name>.html.markdown` | Resource page |
186+
187+
Files named `index` (before the first `.`) in the website docs root directory and files in the `website/docs/d/`, `website/docs/r/`, `docs/data-sources/`,
188+
and `docs/resources/` subdirectories will be converted to `tfplugindocs` templates.
189+
190+
The `website/docs/guides/` and `docs/guides/` subdirectories will be copied as-is to the `--templates-dir` folder.
191+
192+
All other files in the conventional paths will be ignored.
193+
133194
### Templates
134195

135196
The templates are implemented with Go [`text/template`](https://golang.org/pkg/text/template/)
@@ -179,7 +240,7 @@ using the following data fields and functions:
179240
| `tffile` | A special case of the `codefile` function, designed for Terraform files (i.e. `.tf`). |
180241
| `trimspace` | Equivalent to [`strings.TrimSpace`](https://pkg.go.dev/strings#TrimSpace). |
181242
| `upper` | Equivalent to [`strings.ToUpper`](https://pkg.go.dev/strings#ToUpper). |
182-
243+
183244
## Disclaimer
184245

185246
This is still under development: while it's being used for production-ready providers, you might still find bugs

cmd/tfplugindocs/main_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ func Test_SchemaJson_GenerateAcceptanceTests(t *testing.T) {
4646
Dir: "testdata/scripts/schema-json/generate",
4747
})
4848
}
49+
50+
func Test_SchemaJson_MigrateAcceptanceTests(t *testing.T) {
51+
t.Parallel()
52+
53+
testscript.Run(t, testscript.Params{
54+
Dir: "testdata/scripts/schema-json/migrate",
55+
})
56+
}

0 commit comments

Comments
 (0)