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

Implement migrate sub-command #314

Merged
merged 19 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/unreleased/FEATURES-20231220-141244.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: FEATURES
body: 'migrate: Added new `migrate` subcommand that migrates existing provider docs using the
rendered website source directories (`website/docs/` or `/docs/`) to a `terraform-plugin-docs`-supported
templates directory.'
time: 2023-12-20T14:12:44.820323-05:00
custom:
Issue: "314"
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The primary way users will interact with this is the `tfplugindocs` CLI tool to

## `tfplugindocs`

The `tfplugindocs` CLI has two main commands, `validate` and `generate` (`generate` is the default).
The `tfplugindocs` CLI has three main commands, `migrate`, `validate` and `generate` (`generate` is the default).
This tool will let you generate documentation for your provider from live example `.tf` files and markdown templates.
It will also export schema information from the provider (using `terraform providers schema -json`),
and sync the schema with the reference documents.
Expand All @@ -28,6 +28,7 @@ Usage: tfplugindocs [--version] [--help] <command> [<args>]
Available commands are:
the generate command is run by default
generate generates a plugin website from code, templates, and examples
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/`).
validate validates a plugin website for the current directory

```
Expand Down Expand Up @@ -59,6 +60,18 @@ $ tfplugindocs validate --help
Usage: tfplugindocs validate [<args>]
```

`migrate` command:

```shell
$ tfplugindocs migrate --help

Usage: tfplugindocs migrate [<args>]

--examples-dir <ARG> examples directory based on provider-dir (default: "examples")
--provider-dir <ARG> relative or absolute path to the root provider code directory when running the command outside the root provider code directory
--templates-dir <ARG> new website templates directory based on provider-dir; files will be migrated to this directory (default: "templates")
Copy link
Member

Choose a reason for hiding this comment

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

At first glance, it wasn't immediately obvious what the input directories were vs. the output directory without reading the help text. Would it be confusing/misleading to name the --templates-dir as --output-dir?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the name should be the same to keep it in parity with the generate command with the same flag. If we changed the flag to something like --output-dir it might not be obvious to the user that they would need to specify generate --template-dir to that directory after migration.

Copy link
Member

Choose a reason for hiding this comment

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

Ah okay that's helpful context! Makes sense to me 👍🏻

```

### How it Works

When you run `tfplugindocs`, by default from the root directory of a provider codebase, the tool takes the following actions:
Expand Down Expand Up @@ -100,6 +113,23 @@ Otherwise, the provider developer can set an arbitrary description like this:
// ...
```

#### Migrate subcommand

The `migrate` subcommand can be used to migrate 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/`). Markdown files in the rendered website
directory will be converted to `tfplugindocs` templates. The legacy `website/` directory will be removed after migration to avoid Terraform Registry
ingress issues.

The `migrate` subcommand takes the following actions:
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
- Determines the rendered website directory based on the `--provider-dir` argument
- Copies the contents of the rendered website directory to the `--tempates-dir` folder (will create this folder if it doesn't exist)
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
- (if the rendered website is using legacy format) Renames `docs/d/` and `docs/r/` subdirectories to `data-sources/` and `resources/` respectively
- Change file suffixes for Markdown files to `.md.tmpl` to create website templates
- Extracts code blocks from website docs to create individual example files in `--examples-dir` (will create this folder if it doesn't exist)
- replace extracted example code in website templates with `tfplugindocs` template code referring to example files.
SBGoods marked this conversation as resolved.
Show resolved Hide resolved
- Copies non-template files to `--templates-dir` folder
- Removes the `website/` directory

### Conventional Paths

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

#### Migration

The `migrate` subcommand assumes the following conventional paths for the rendered website directory:

Legacy website directory structure:

| Path | Description |
|---------------------------------------------------|-----------------------------|
| `website/` | Root of website docs |
| `website/docs/guides` | Root of guides subdirectory |
| `website/docs/index.html.markdown` | Docs index page |
| `website/docs/d/<data source name>.html.markdown` | Data source page |
| `website/docs/r/<resource name>.html.markdown` | Resource page |

Docs website directory structure:

| Path | Description |
|------------------------------------------------------|-----------------------------|
| `docs/` | Root of website docs |
| `docs/guides` | Root of guides subdirectory |
| `docs/index.html.markdown` | Docs index page |
| `docs/data-sources/<data source name>.html.markdown` | Data source page |
| `docs/resources/<resource name>.html.markdown` | Resource page |

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/`,
and `docs/resources/` subdirectories will be converted to `tfplugindocs` templates.

The `website/docs/guides/` and `docs/guides/` subdirectories will be copied as-is to the `--templates-dir` folder.

All other files in the conventional paths will be ignored.

### Templates

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

## Disclaimer

This is still under development: while it's being used for production-ready providers, you might still find bugs
Expand Down
8 changes: 8 additions & 0 deletions cmd/tfplugindocs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ func Test_SchemaJson_GenerateAcceptanceTests(t *testing.T) {
Dir: "testdata/scripts/schema-json/generate",
})
}

func Test_SchemaJson_MigrateAcceptanceTests(t *testing.T) {
t.Parallel()

testscript.Run(t, testscript.Params{
Dir: "testdata/scripts/schema-json/migrate",
})
}
Loading
Loading